Example usage for java.nio ByteOrder LITTLE_ENDIAN

List of usage examples for java.nio ByteOrder LITTLE_ENDIAN

Introduction

In this page you can find the example usage for java.nio ByteOrder LITTLE_ENDIAN.

Prototype

ByteOrder LITTLE_ENDIAN

To view the source code for java.nio ByteOrder LITTLE_ENDIAN.

Click Source Link

Document

This constant represents little endian.

Usage

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.  jav a2 s  . c om*/
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getShort();
}

From source file:Main.java

public static long ReadlittleEndianLong(InputStream dis) throws IOException {
    byte[] bytes = new byte[8];
    int re = dis.read(bytes);
    if (re == -1) {
        return -1;
    }/*from w ww  . ja  v a2s.  c o m*/
    ByteBuffer bytebuffer = ByteBuffer.wrap(bytes);
    bytebuffer.order(ByteOrder.LITTLE_ENDIAN);
    long result = bytebuffer.getLong();
    return result;
}

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   www . ja v a 2s . c o m
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    return buffer.getInt();
}

From source file:Main.java

public static byte[] uuidToBytes(String uuidStr) {
    UUID uuid = stringToUuid(uuidStr);

    ByteBuffer bb = ByteBuffer.allocate(16);
    bb.order(ByteOrder.LITTLE_ENDIAN);
    bb.putLong(uuid.getLeastSignificantBits());
    bb.putLong(uuid.getMostSignificantBits());
    return bb.array();
}

From source file:Main.java

public static Buffer fillBuffer(float[] array) {
    // Convert to floats because OpenGL doesnt work on doubles, and manually
    // casting each input value would take too much time.
    ByteBuffer bb = ByteBuffer.allocateDirect(4 * array.length); // each
    // float// ww w  . ja  v a 2s  . c o  m
    // takes 4
    // bytes
    bb.order(ByteOrder.LITTLE_ENDIAN);
    for (float d : array)
        bb.putFloat(d);
    bb.rewind();

    return bb;

}

From source file:Main.java

public static byte[] toArray(int value) {
    ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putInt(value);//from  w  w  w  .  ja v a 2s.  c  o  m
    return buffer.array();
}

From source file:Main.java

/**
 * Convert from array of byte to array of short.
 * @param byteArray byte array/*w  w  w .j  a va  2 s  .  c o  m*/
 * @return short array
 */
public static short[] byteToShort(final byte[] byteArray) {
    short[] shortOut = new short[byteArray.length / 2];
    ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortOut);
    return shortOut;
}

From source file:Main.java

/**
 * Convert from singed 16-bit PCM to 32-bit float PCM.
 * @param byteArray byte array/*  w w  w .  j  a  v a  2s .  co  m*/
 * @return byte array
 */
public static byte[] convert16BitTo32Bit(final byte[] byteArray) {
    float[] audioDataF = shortToFloat(byteToShort(byteArray));
    for (int i = 0; i < audioDataF.length; i++) {
        audioDataF[i] /= 32768.0;
    }

    FloatBuffer fb = FloatBuffer.wrap(audioDataF);
    ByteBuffer byteBuffer = ByteBuffer.allocate(fb.capacity() * 4);
    byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
    byteBuffer.asFloatBuffer().put(fb);
    return byteBuffer.array();
}

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());/*  www .j  a v a2 s.  c om*/
        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   www.j a va 2s.  co m

    return sb.toString();
}