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

private static String formatInts(byte[] data) {
    IntBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();

    StringBuilder sb = new StringBuilder(bb.capacity() * 3);

    while (bb.remaining() > 0) {
        sb.append(bb.get());/*from w  ww. j  a  v a  2  s . c  o m*/
        sb.append(',');
        sb.append('\n');
    }

    return sb.toString();
}

From source file:Main.java

private static String formatFloats(byte[] data) {
    FloatBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asFloatBuffer();

    StringBuilder sb = new StringBuilder(bb.capacity() * 3);

    while (bb.remaining() > 0) {
        sb.append(String.format("%.4f", bb.get()));
        sb.append(',');
        sb.append('\n');
    }/*from w  w  w.ja  v  a 2 s . c o m*/

    return sb.toString();
}

From source file:Main.java

private static String formatShorts(byte[] data, boolean unsigned) {
    ShortBuffer bb = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();

    StringBuilder sb = new StringBuilder(bb.capacity() * 3);

    while (bb.remaining() > 0) {
        if (unsigned) {
            sb.append(bb.get() & 0xffff);
        } else {/*from www.  j  a v a 2 s.  c o  m*/
            sb.append(bb.get());
        }
        sb.append(',');
        sb.append('\n');
    }

    return sb.toString();
}

From source file:Main.java

public static AdvertiseData createAllOffAdvertiseData() {
    byte[] manufacturerData = new byte[23];
    ByteBuffer bb = ByteBuffer.wrap(manufacturerData);
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.put((byte) 0x41);
    bb.put((byte) 0x6c);
    bb.put((byte) 0x6c);
    bb.put((byte) 0x4f);
    bb.put((byte) 0x66);
    bb.put((byte) 0x66);
    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);
    bb.put((byte) 0x00);

    AdvertiseData.Builder builder = new AdvertiseData.Builder();
    builder.addManufacturerData(0x006d, manufacturerData);
    AdvertiseData adv = builder.build();
    return adv;//w  w w.  ja va2 s. co m
}

From source file:Main.java

public static int byteArrayToLeInt(byte[] b) {
    final ByteBuffer bb = ByteBuffer.wrap(b);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    return bb.getInt();
}

From source file:Main.java

public static ByteBuffer someData() {
    byte[] bytes = new byte[10];
    deterministicDataSequence.nextBytes(bytes);
    return ByteBuffer.wrap(bytes);
}

From source file:Main.java

public static ByteBuffer lotsOfData() {
    byte[] bytes = new byte[150];
    deterministicDataSequence.nextBytes(bytes);
    return ByteBuffer.wrap(bytes);
}

From source file:Main.java

/** create AdvertiseDate for iBeacon */
public static AdvertiseData createEmployeeIDAdvertiseData(byte[] data, int length) {
    byte[] manufacturerData = new byte[23];
    ByteBuffer bb = ByteBuffer.wrap(manufacturerData);
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.put((byte) 0x4d);
    bb.put((byte) 0x44);
    bb.put((byte) 0x43);
    bb.put(data);/*from   w ww.  j  a va2 s .  c om*/
    for (int i = 0; i < length; i++) {
        bb.put((byte) 0x00);
    }

    AdvertiseData.Builder builder = new AdvertiseData.Builder();
    builder.addManufacturerData(0x006d, manufacturerData);
    AdvertiseData adv = builder.build();
    return adv;
}

From source file:Main.java

/** create AdvertiseDate for iBeacon */
public static AdvertiseData createAllOnAdvertiseData() {
    byte[] manufacturerData = new byte[23];
    ByteBuffer bb = ByteBuffer.wrap(manufacturerData);
    bb.order(ByteOrder.BIG_ENDIAN);
    bb.put((byte) 0x41);
    bb.put((byte) 0x6c);
    bb.put((byte) 0x6c);
    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);
    bb.put((byte) 0x00);
    bb.put((byte) 0x00);

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

From source file:Main.java

public static String decodeUnicode(String str) {
    Charset set = Charset.forName("UTF-16");
    Pattern p = Pattern.compile("\\\\u([0-9a-fA-F]{4})");
    Matcher m = p.matcher(str);/*w w w .  j  a  va2  s . c  o  m*/
    int start = 0;
    int start2;
    StringBuffer sb = new StringBuffer();
    while (m.find(start)) {
        start2 = m.start();
        if (start2 > start) {
            String seg = str.substring(start, start2);
            sb.append(seg);
        }
        String code = m.group(1);
        int i = Integer.valueOf(code, 16);
        byte[] bb = new byte[4];
        bb[0] = (byte) ((i >> 8) & 0xFF);
        bb[1] = (byte) (i & 0xFF);
        ByteBuffer b = ByteBuffer.wrap(bb);
        sb.append(String.valueOf(set.decode(b)).trim());
        start = m.end();
    }
    start2 = str.length();
    if (start2 > start) {
        String seg = str.substring(start, start2);
        sb.append(seg);
    }
    return sb.toString();
}