Example usage for java.nio ByteBuffer wrap

List of usage examples for java.nio ByteBuffer wrap

Introduction

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

Prototype

public static ByteBuffer wrap(byte[] array) 

Source Link

Document

Creates a new byte buffer by wrapping the given byte array.

Usage

From source file:Main.java

/** create AdvertiseDate for iBeacon */
public static AdvertiseData createLightOnAdvertiseData() {
    byte[] manufacturerData = new byte[23];
    ByteBuffer bb = ByteBuffer.wrap(manufacturerData);
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.put((byte) 0x4c);
    bb.put((byte) 0x69);
    bb.put((byte) 0x67);
    bb.put((byte) 0x68);
    bb.put((byte) 0x74);
    bb.put((byte) 0x4f);
    bb.put((byte) 0x6e);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);

    AdvertiseData.Builder builder = new AdvertiseData.Builder();
    builder.addManufacturerData(0x006d, manufacturerData);
    AdvertiseData adv = builder.build();
    return adv;// w  ww.j a  v  a  2s  .c om
}

From source file:info.gehrels.flockDBClient.ByteHelper.java

static ByteBuffer asByteBufferOrNull(long... destinationIds) {
    ByteBuffer buf = null;// ww  w .  j  a v a2s  .c  om
    if (isNotEmpty(destinationIds)) {
        buf = ByteBuffer.wrap(new byte[destinationIds.length * (Long.SIZE / 8)]);
        buf.order(ByteOrder.LITTLE_ENDIAN);
        for (long destinationId : destinationIds) {
            buf.putLong(destinationId);
        }
        buf.rewind();
    }
    return buf;
}

From source file:com.google.step2.util.EncodingUtil.java

public static String getUtf8String(byte[] data) {
    return UTF8.decode(ByteBuffer.wrap(data)).toString();
}

From source file:com.scf.utils.UUIDUtilies.java

protected static String base64Uuid(UUID uuid) {

    ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
    bb.putLong(uuid.getMostSignificantBits());
    bb.putLong(uuid.getLeastSignificantBits());

    return Base64.encodeBase64URLSafeString(bb.array());
}

From source file:Main.java

static ByteBuffer createMsg(int state, long leader, long zxid, long epoch) {
    byte requestBytes[] = new byte[28];
    ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes);

    /*//  w w w.  j av a  2 s.  co m
     * Building notification packet to send
     */

    requestBuffer.clear();
    requestBuffer.putInt(state);
    requestBuffer.putLong(leader);
    requestBuffer.putLong(zxid);
    requestBuffer.putLong(epoch);

    return requestBuffer;
}

From source file:Main.java

public static ByteBuffer copyBinary(final ByteBuffer orig) {
    if (orig == null) {
        return null;
    }/*from  w  w  w.jav a 2 s . c  om*/
    ByteBuffer copy = ByteBuffer.wrap(new byte[orig.remaining()]);
    if (orig.hasArray()) {
        System.arraycopy(orig.array(), orig.arrayOffset() + orig.position(), copy.array(), 0, orig.remaining());
    } else {
        orig.slice().get(copy.array());
    }

    return copy;
}

From source file:Main.java

/**
 * Convert the given byte array to 'long'
 * //w  ww .ja  v  a2s  .co m
 * @param b
 *            An 8-byte array
 * @return long value of the array contents
 */
public static long convertByteArrayToLong(byte[] b) {
    int capacity = Long.SIZE / EIGHT;
    if (b.length > capacity) {
        return -1;
    }
    return ByteBuffer.wrap(b).getLong();
}

From source file:Main.java

private static String getBytesAsString(byte[] bytes) {
    if (bytes == null)
        return "null";
    try {/* ww w . java2  s . c  o  m*/
        CharsetDecoder d = Charset.forName("US-ASCII").newDecoder();
        CharBuffer r = d.decode(ByteBuffer.wrap(bytes));
        return r.toString();
    } catch (Exception e) {
        return Base64.encodeToString(bytes, Base64.NO_WRAP);
    }
}

From source file:Main.java

public static byte[] getBytePixels(final Bitmap bit) {
    if (bit == null) {
        return null;
    }//  www  .  j a  v  a  2s  .co m
    final byte[] pixels = new byte[bit.getWidth() * bit.getHeight() * 4];
    final ByteBuffer buf = ByteBuffer.wrap(pixels);
    buf.order(ByteOrder.nativeOrder());
    bit.copyPixelsToBuffer(buf);
    return pixels;
}

From source file:Main.java

/**
 * Convert the given byte array to 'int'
 * /*from   ww  w  .j av  a  2 s.  c o  m*/
 * @param b
 *            A 4-byte array
 * 
 * @return int value of the array contents
 */
public static int convertBigEndianByteArrayToInt(byte[] b) {
    int capacity = Integer.SIZE / EIGHT;
    if (b.length > capacity) {
        return -1;
    }
    return ByteBuffer.wrap(b).getInt();
}