Example usage for java.nio ByteBuffer array

List of usage examples for java.nio ByteBuffer array

Introduction

In this page you can find the example usage for java.nio ByteBuffer array.

Prototype

public final byte[] array() 

Source Link

Document

Returns the byte array which this buffer is based on, if there is one.

Usage

From source file:com.intellectualcrafters.plot.uuid.UUIDFetcher.java

@SuppressWarnings("unused")
public static byte[] toBytes(final UUID uuid) {
    final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
    byteBuffer.putLong(uuid.getMostSignificantBits());
    byteBuffer.putLong(uuid.getLeastSignificantBits());
    return byteBuffer.array();
}

From source file:Main.java

public final static int countP(final ByteBuffer bb1, final ByteBuffer bb2) {
    final int end1 = bb1.limit();
    final int end2 = bb2.limit();
    final int offset1 = bb1.position();
    final int offset2 = bb2.position();
    final byte[] array1 = bb1.array();
    final byte[] array2 = bb2.array();
    return count(array1, offset1, end1, array2, offset2, end2);
}

From source file:com.openteach.diamond.network.waverider.master.MasterState.java

public static MasterState fromByteBuffer(ByteBuffer buffer) {
    ByteArrayInputStream bin = null;
    ObjectInputStream oin = null;
    try {/*from  w  ww . j  av  a  2s .  co m*/
        bin = new ByteArrayInputStream(buffer.array(), Packet.getHeaderSize() + Command.getHeaderSize(),
                buffer.remaining());
        oin = new ObjectInputStream(bin);
        return (MasterState) oin.readObject();
    } catch (IOException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        logger.error(e);
        throw new RuntimeException(e);
    } finally {
        try {
            if (oin != null) {
                oin.close();
            }
            if (bin != null) {
                bin.close();
            }
        } catch (IOException e) {
            logger.error(e);
        }
    }
}

From source file:com.liveramp.commons.util.BytesUtils.java

public static String bytesToHexString(ByteBuffer b) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < b.remaining(); ++i) {
        final int v = b.array()[b.arrayOffset() + b.position() + i] & 0xff;
        if (i > 0) {
            result.append(" ");
        }/* w w  w.ja  va 2 s.  c  o m*/
        if (v < 16) {
            result.append("0");
        }
        result.append(Integer.toString(v, 16));
    }
    return result.toString();
}

From source file:ch.cyberduck.core.http.DelayedHttpMultipartEntity.java

private static ByteArrayBuffer encode(final Charset charset, final String input) {
    final ByteBuffer encoded = charset.encode(CharBuffer.wrap(input));
    final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining());
    bab.append(encoded.array(), encoded.position(), encoded.remaining());
    return bab;//www .j  av a 2 s .c o  m
}

From source file:Main.java

public static byte[] getAESkey(byte[] authKey, byte[] msgKey, boolean isOut) {
    int x = 0;/*from   www . java  2  s  . c om*/
    if (!isOut)
        x = 8;

    ByteBuffer tempA = ByteBuffer.allocate(msgKey.length + 32);
    tempA.put(msgKey);
    tempA.put(authKey, x, 32);
    byte[] a = getSHA1hash(tempA.array());

    ByteBuffer tempB = ByteBuffer.allocate(msgKey.length + 32);
    tempB.put(authKey, 32 + x, 16);
    tempB.put(msgKey);
    tempB.put(authKey, 48 + x, 16);
    byte[] b = getSHA1hash(tempB.array());

    ByteBuffer tempC = ByteBuffer.allocate(msgKey.length + 32);
    tempC.put(authKey, 64 + x, 32);
    tempC.put(msgKey);
    byte[] c = getSHA1hash(tempC.array());

    ByteBuffer key = ByteBuffer.allocate(8 + 12 + 12);
    key.put(a, 0, 8);
    key.put(b, 8, 12);
    key.put(c, 4, 12);

    return key.array();
}

From source file:com.pinterest.deployservice.common.CommonUtils.java

/**
 * TODO figure out how to use guava to achive this
 *
 * @return base64 encoded shorten UUID, e.g. 11YozyYYTvKmuUXpRDvoJA
 *//*from  w w w.  jav a  2 s.com*/
public static String getBase64UUID() {
    UUID uuid = UUID.randomUUID();
    ByteBuffer buffer = ByteBuffer.wrap(new byte[16]);
    buffer.putLong(uuid.getMostSignificantBits());
    buffer.putLong(uuid.getLeastSignificantBits());
    String base64 = BaseEncoding.base64Url().omitPadding().encode(buffer.array());
    if (base64.charAt(0) == '_') {
        return 'x' + base64.substring(1);
    }
    if (base64.charAt(0) == '-') {
        return 'X' + base64.substring(1);
    }
    return base64;
}

From source file:Main.java

public static byte[] getAESiv(byte[] authKey, byte[] msgKey, boolean isOut) {
    int x = 0;//from  w  w  w.  j a v  a  2 s .c o  m
    if (!isOut)
        x = 8;

    ByteBuffer tempA = ByteBuffer.allocate(msgKey.length + 32);
    tempA.put(msgKey);
    tempA.put(authKey, x, 32);
    byte[] a = getSHA1hash(tempA.array());

    ByteBuffer tempB = ByteBuffer.allocate(msgKey.length + 32);
    tempB.put(authKey, 32 + x, 16);
    tempB.put(msgKey);
    tempB.put(authKey, 48 + x, 16);
    byte[] b = getSHA1hash(tempB.array());

    ByteBuffer tempC = ByteBuffer.allocate(msgKey.length + 32);
    tempC.put(authKey, 64 + x, 32);
    tempC.put(msgKey);
    byte[] c = getSHA1hash(tempC.array());

    ByteBuffer tempD = ByteBuffer.allocate(msgKey.length + 32);
    tempD.put(msgKey);
    tempD.put(authKey, 96 + x, 32);
    byte[] d = getSHA1hash(tempD.array());

    ByteBuffer iv = ByteBuffer.allocate(12 + 8 + 4 + 8);
    iv.put(a, 8, 12);
    iv.put(b, 0, 8);
    iv.put(c, 16, 4);
    iv.put(d, 0, 8);

    return iv.array();
}

From source file:Main.java

public static byte[] padLeft(byte[] tgt, int len, byte padding) {
    if (tgt.length >= len) {
        return tgt;
    }// w w w.j a v a  2 s . co m
    ByteBuffer buffer = ByteBuffer.allocate(len);
    byte[] paddings = new byte[len - tgt.length];
    Arrays.fill(paddings, padding);

    buffer.put(paddings);
    buffer.put(tgt);

    return buffer.array();
}

From source file:io.card.development.recording.ManifestEntry.java

private static byte[] decompress(byte[] compressed) {
    /*//w  w  w.  j av a  2s  .c  o  m
     * DO NOT EVER USE THIS METHOD IN PRODUCTION This is horribly inefficient, but written only
     * for testing purposes.
     */

    Bitmap b = BitmapFactory.decodeByteArray(compressed, 0, compressed.length);
    ByteBuffer bb = ByteBuffer.allocate(b.getWidth() * b.getHeight() * 4);
    b.copyPixelsToBuffer(bb);
    b.recycle();

    byte[] ba = bb.array();
    byte[] singleChannel = new byte[ba.length / 4]; // 4 channels
    for (int i = 0; i < singleChannel.length; i++) {
        singleChannel[i] = ba[i * 4 + 1];
    }

    return singleChannel;
}