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 FloatBuffer createFloatBuffer(int floatCount) {
    ByteBuffer data = ByteBuffer.allocateDirect(floatCount * 4);
    data.order(ByteOrder.nativeOrder());
    FloatBuffer p1 = data.asFloatBuffer();
    return p1;//  w  w w  . j av a2  s. c o m
}

From source file:Main.java

public static ShortBuffer createShortBuffer(int shortCount) {
    ByteBuffer data = ByteBuffer.allocateDirect(shortCount * 4);
    data.order(ByteOrder.nativeOrder());
    ShortBuffer p1 = data.asShortBuffer();
    return p1;/*  w w  w .  j  av  a2 s  .co m*/
}

From source file:Main.java

public static FloatBuffer makeFloatBufferFromArray(float[] arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(arr);/*from www  .  ja v  a2s  . c o  m*/
    fb.position(0);
    return fb;
}

From source file:Main.java

public static FloatBuffer float2Buffer(float[] a) {
    FloatBuffer floatBuffer;/*from ww  w . j a v  a  2s. co m*/
    ByteBuffer bb = ByteBuffer.allocateDirect(a.length * 4);
    bb.order(ByteOrder.nativeOrder());
    floatBuffer = bb.asFloatBuffer();
    floatBuffer.put(a);
    floatBuffer.position(0);
    return floatBuffer;
}

From source file:Main.java

/**
 * Convert from array of byte to array of short.
 * @param byteArray byte array/*w w  w . j  ava  2  s  .co 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

public static FloatBuffer toFloatBuffer(float[] v) {
    ByteBuffer buff = ByteBuffer.allocateDirect(v.length * 4);
    buff.order(ByteOrder.nativeOrder());
    FloatBuffer buffer = buff.asFloatBuffer();
    buffer.put(v);//  w w  w .  ja  va  2s .c om
    buffer.position(0);
    return buffer;
}

From source file:Main.java

public static ShortBuffer shortToBuffer(short[] a) {
    ShortBuffer shortBuffer;/*  w  ww  . ja  va  2s .  com*/
    ByteBuffer ibb = ByteBuffer.allocateDirect(a.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    shortBuffer = ibb.asShortBuffer();
    shortBuffer.put(a);
    shortBuffer.position(0);
    return shortBuffer;
}

From source file:Main.java

public static FloatBuffer makeFloatBuffer(int i_len) {
    ByteBuffer bb = ByteBuffer.allocateDirect(i_len * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.position(0);//from w  ww  .j a  va2 s.  c  om
    return fb;
}

From source file:Main.java

public static byte[] shortToByteArrayLE(short value) {
    ByteBuffer buffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE);
    buffer.order(ByteOrder.LITTLE_ENDIAN);
    buffer.putShort(value);/*from  ww  w.j  a  v  a 2  s  .  c  o m*/
    return buffer.array();
}

From source file:Main.java

public static byte[] shortToByteArray(short value) {
    ByteBuffer buffer = ByteBuffer.allocate(Short.SIZE / Byte.SIZE);
    buffer.order(ByteOrder.BIG_ENDIAN);
    buffer.putShort(value);/*www.j  a  v a 2s. c o m*/
    return buffer.array();
}