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

public static byte[] floatTobytes(float inFloat) {
    byte[] outBytes = new byte[4];
    ByteBuffer.wrap(outBytes).putFloat(inFloat);
    return outBytes;
}

From source file:Main.java

public static String bytesToStringUTFNIO(byte[] bytes) {
    CharBuffer cBuffer = ByteBuffer.wrap(bytes).asCharBuffer();
    return cBuffer.toString();
}

From source file:Main.java

public static UUID getUuidFromByteArrayBigEndian(byte[] bytes) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    long high = bb.getLong();
    long low = bb.getLong();
    UUID uuid = new UUID(high, low);
    return uuid;//from  w ww .  j  a  v a 2  s.  com
}

From source file:Main.java

public static float[] bytesToFloats(byte[] floatsBytes) {
    float[] floats = new float[floatsBytes.length / 4];
    ByteBuffer.wrap(floatsBytes).asFloatBuffer().get(floats);
    return floats;
}

From source file:Main.java

public static byte[] shortArr2byteArr(final short[] pcm, final int length) {
    byte[] dst = new byte[length * 2];
    ByteBuffer buf = ByteBuffer.wrap(dst);
    buf.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(pcm, 0, length);
    return dst;//from  w w w .j  a v a2 s . co  m
}

From source file:Main.java

private static ByteBuffer buffer(int len) {
    byte[] b = new byte[len];
    Random r = new Random();
    r.nextBytes(b);/*from  w ww . j  a  va2 s . co m*/
    return ByteBuffer.wrap(b);
}

From source file:Main.java

private static Rect getRectByByte(byte[] org) {
    Rect rect = new Rect();
    ByteBuffer byteBuffer = ByteBuffer.wrap(org).order(ByteOrder.nativeOrder());
    rect.set(byteBuffer.getInt(3 * 4), byteBuffer.getInt(5 * 4), byteBuffer.getInt(4 * 4),
            byteBuffer.getInt(6 * 4));// ww w.  j  a  va 2 s.  com
    return rect;
}

From source file:Main.java

/**
 * Converts a byte array to an int./*from w  w w  . java  2s  . c o  m*/
 *
 * @param bytes the bytes
 * @return the int from byte array
 */
public static int getIntFromByteArray(final byte[] bytes) {
    return ByteBuffer.wrap(bytes).getInt();
}

From source file:Main.java

public static short[] byteToShortArray(byte[] data) {
    short[] shorts = new short[data.length / BYTES_PER_SHORT];
    ByteBuffer.wrap(data).order(ByteOrder.nativeOrder()).asShortBuffer().get(shorts);
    return shorts;
}

From source file:Main.java

public static int fromArray(byte[] payload) {
    ByteBuffer buffer = ByteBuffer.wrap(payload);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getInt();
}