Example usage for java.nio ByteBuffer order

List of usage examples for java.nio ByteBuffer order

Introduction

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

Prototype

Endianness order

To view the source code for java.nio ByteBuffer order.

Click Source Link

Document

The byte order of this buffer, default is BIG_ENDIAN .

Usage

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

/**
 * Converts a given integer to byte, in little endian
 * @param i An integer to be converted to byte
 * @return A byte array stores byte representation of given integer
 *//*from  w ww. ja v  a  2  s.  c  om*/
public static byte[] intToByte(int i) {
    ByteBuffer b = ByteBuffer.allocate(4);
    b.order(ByteOrder.LITTLE_ENDIAN);
    b.putInt(i);
    byte buf[] = b.array();
    return buf;
}

From source file:Main.java

private static short stream2Short(byte[] stream, int offset) {
    ByteBuffer buffer = ByteBuffer.allocate(2);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.put(stream[offset]);/*from  w w w.j  a v  a2  s .co  m*/
    buffer.put(stream[offset + 1]);
    return buffer.getShort(0);
}

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 a v a2  s .c  o m*/
    return output;
}

From source file:Main.java

public static Short byteArrayToShort(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }// w w w . j  a v a  2  s  .c om
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static int byteArrayToIntLE(byte[] bytes) throws IllegalArgumentException {
    if (bytes.length != (Integer.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to an int");
    }/*from   ww w  .  ja  v  a 2s .  c  om*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

public static Short byteArrayToShortLE(byte[] bytes) {
    if (bytes.length != (Short.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to a short");
    }//from   ww  w  . ja  v  a 2s  . c  o m
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static int byteArrayToInt(byte[] bytes) throws IllegalArgumentException {
    if (bytes.length != (Integer.SIZE / Byte.SIZE)) {
        throw new IllegalArgumentException("Incorrect array size to convert to an int");
    }/*from  w w  w. j av a 2 s  . c o  m*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.BIG_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

private static IntBuffer newIntBuffer(int size) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(size);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.asIntBuffer();
}

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);
}