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 float[] byteToFloatArrayBigEndian(byte[] data) {
    float[] floats = new float[data.length / BYTES_PER_FLOAT];
    ByteBuffer.wrap(data).order(ByteOrder.BIG_ENDIAN).asFloatBuffer().get(floats);
    return floats;
}

From source file:Main.java

public static byte[] long2bytearray(long l) {
    //TODO optimize with needing to create a ByteBuffer object

    byte b[] = new byte[8];

    ByteBuffer buf = ByteBuffer.wrap(b);
    buf.putLong(l);/*from   w ww  . j a v  a2 s  .c  o m*/
    return b;
}

From source file:Main.java

private static String decode(Charset charset, byte[] b) {
    if (b == null) {
        return null;
    }/*w w w. j ava 2 s  .  c  o m*/
    final CharBuffer cb = charset.decode(ByteBuffer.wrap(b));
    return new String(cb.array(), 0, cb.length());
}

From source file:Main.java

public static byte[] downSample(final byte[] data) {
    final byte[] output = new byte[data.length / 2];
    int length = data.length;
    ByteBuffer bb = ByteBuffer.wrap(data);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    for (int i = 0; i < length; i += 2) {
        output[i / 2] = (byte) (((bb.getShort() * 128.0) / 32768.0) + 128.0);
    }/*from   w  w  w  .  j  ava2 s .c om*/
    return output;
}

From source file:Main.java

public static int byteArrayToShort(byte[] bytes, int offset) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    return bb.getShort(offset);
}

From source file:Main.java

public static int byteArrayToInt(byte[] bytes, int offset) {
    ByteBuffer bb = ByteBuffer.wrap(bytes);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    return bb.getInt(offset);
}

From source file:Main.java

public static int readInt(InputStream inputStream) throws IOException {
    byte[] bytesToRead = new byte[4];
    inputStream.read(bytesToRead);/*from   w  ww .ja  v a2  s. c  om*/

    ByteBuffer buffer = ByteBuffer.wrap(bytesToRead);
    buffer.order(ByteOrder.LITTLE_ENDIAN);

    return buffer.getInt();
}

From source file:Main.java

public static String readMAString(ByteBuffer in) {
    int tmp = in.getInt();
    byte[] dst = new byte[tmp];
    in.get(dst);/*w ww.ja  v  a  2 s.  com*/
    String ret = Charset.forName("UTF-8").decode(ByteBuffer.wrap(dst)).toString();
    return ret;
}

From source file:Main.java

public static float toFloatEx(byte[] b, int pos) {
    try {//from  w  w  w.j  a  v  a  2  s  . c  om
        byte[] byteTmp = new byte[4];
        for (int i = 0; i < 4; i++) {
            byteTmp[i] = b[pos + i];
        }
        ByteBuffer bb = ByteBuffer.wrap(byteTmp);

        FloatBuffer fb = bb.asFloatBuffer();

        return fb.get();
    } catch (Exception e) {
        e.printStackTrace();
        return 0.0f;
    }

}

From source file:Main.java

public static String readFile(String path, Charset encoding) throws IOException {
    byte[] encoded = Files.readAllBytes(Paths.get(path));
    return encoding.decode(ByteBuffer.wrap(encoded)).toString();
}