Example usage for java.nio ByteBuffer allocateDirect

List of usage examples for java.nio ByteBuffer allocateDirect

Introduction

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

Prototype

public static ByteBuffer allocateDirect(int capacity) 

Source Link

Document

Creates a direct byte buffer based on a newly allocated memory block.

Usage

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   w  w w  .  j  a  v a  2s  .  c  om
    fb.position(0);
    return fb;
}

From source file:Main.java

public static IntBuffer intArrayToBuffer(int[] intArray) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(intArray.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    intBuffer.put(intArray);/*from w  w  w .  ja  v a 2  s . c o m*/
    intBuffer.position(0);
    return intBuffer;
}

From source file:Main.java

public static ShortBuffer buildShortBuffer(short[] array) {
    ByteBuffer byteBuf = ByteBuffer.allocateDirect(array.length * 2);
    byteBuf.order(ByteOrder.nativeOrder());
    ShortBuffer buffer = byteBuf.asShortBuffer();
    buffer.put(array);//from w  w  w . j  a  v  a 2 s .c  o  m
    buffer.position(0);
    return buffer;
}

From source file:Main.java

public static FloatBuffer getBufferFromFloatArray(float[] array) {
    FloatBuffer result = ByteBuffer.allocateDirect(array.length * 4).order(ByteOrder.nativeOrder())
            .asFloatBuffer();/*from   w w  w.  java2s .c  om*/
    result.put(array).position(0);
    return result;
}

From source file:Main.java

public static FloatBuffer floatArrayToBuffer(float[] floatArray) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(floatArray.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.put(floatArray);//w ww. j a va  2s.c  o  m
    floatBuffer.position(0);
    return floatBuffer;
}

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 IntBuffer makeBuffer(int[] data) {
    ByteBuffer b = ByteBuffer.allocateDirect(data.length * 4);
    b.order(ByteOrder.nativeOrder());
    IntBuffer buffer = b.asIntBuffer();
    buffer.put(data);/*from ww  w .j a va2  s .  c  o  m*/
    buffer.position(0);
    return buffer;
}

From source file:Main.java

public static FloatBuffer float2Buffer(float[] a) {
    FloatBuffer floatBuffer;/*from ww w.j  a  v  a 2 s .  c  o 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

public static ShortBuffer shortToBuffer(short[] a) {
    ShortBuffer shortBuffer;//from  w w  w.j  ava2  s. c o  m
    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 ByteBuffer makeByteBuffer(byte[] array)
/*     */ {/*from   ww w .  j  ava  2s. c o  m*/
    /*  63 */int SIZE = 1;
    /*  64 */ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * 1);
    /*  65 */byteBuffer.order(ByteOrder.nativeOrder());
    /*  66 */byteBuffer.put(array);
    /*  67 */byteBuffer.position(0);
    /*  68 */return byteBuffer;
    /*     */}