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 ShortBuffer allocateShortBuffer(int capacity) {
    ByteBuffer vbb = ByteBuffer.allocateDirect(capacity);
    vbb.order(ByteOrder.nativeOrder());
    return vbb.asShortBuffer();
}

From source file:Main.java

public static ShortBuffer toShortBuffer(short[] values) {
    ByteBuffer vbb = ByteBuffer.allocateDirect(values.length * 2);
    vbb.order(ByteOrder.nativeOrder());
    ShortBuffer buffer = vbb.asShortBuffer();
    buffer.put(values);/*from  ww  w .  j a va 2  s. c  om*/
    buffer.position(0);
    return buffer;
}

From source file:Main.java

public static FloatBuffer toFloatBufferPositionZero(float[] values) {
    final ByteBuffer vbb = ByteBuffer.allocateDirect(values.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    final FloatBuffer buffer = vbb.asFloatBuffer();
    buffer.put(values);/*w  ww  . j  av  a2 s.  c o m*/
    buffer.position(0);
    return buffer;
}

From source file:Main.java

public static FloatBuffer toFloatBufferPositionZero(float[] values) {
    ByteBuffer vbb = ByteBuffer.allocateDirect(values.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    FloatBuffer buffer = vbb.asFloatBuffer();
    buffer.put(values);/*from   w  w w  . j a v  a2s. c o m*/
    buffer.position(0);
    return buffer;
}

From source file:Main.java

public static Buffer fillBuffer(double[] 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/*  w w w.  j a v  a 2s.c o m*/
    // takes 4
    // bytes
    bb.order(ByteOrder.LITTLE_ENDIAN);
    for (double d : array)
        bb.putFloat((float) d);
    bb.rewind();

    return bb;

}

From source file:Main.java

public static ShortBuffer transportArrayToNativeBuffer(short[] sArray) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(sArray.length * 2);
    byteBuffer.order(ByteOrder.nativeOrder());
    return (ShortBuffer) byteBuffer.asShortBuffer().put(sArray).position(0);
}

From source file:Main.java

public static ByteBuffer makeByteBuffer(byte[] array) {
    final int SIZE = Byte.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * SIZE);
    byteBuffer.order(ByteOrder.nativeOrder());
    byteBuffer.put(array);//w w w . j  a  v a2s  .co  m
    byteBuffer.position(0);
    return byteBuffer;
}

From source file:Main.java

public static ByteBuffer newByteBuffer(int numBytes) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(numBytes);
    buffer.order(ByteOrder.nativeOrder());
    return buffer;
}

From source file:Main.java

public static ShortBuffer createShortBuffer(final int size) {
    final ShortBuffer buf = ByteBuffer.allocateDirect(2 * size).order(ByteOrder.nativeOrder()).asShortBuffer();
    buf.clear();/*ww w  . ja  v  a 2 s .c  o m*/
    return buf;
}

From source file:Main.java

public static FloatBuffer makeFloatBuffer(int length) {
    final int floatSize = Float.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(length * floatSize);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.position(0);//from  w  w  w .j  av a2s .  c om
    return floatBuffer;
}