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

/**
 * @param length// w w w . j a  v  a2s.  com
 * @return A direct, native-order buffer
 */
public static ByteBuffer createByteBuffer(int length) {
    return ByteBuffer.allocateDirect(length).order(ByteOrder.nativeOrder());
}

From source file:Main.java

public static IntBuffer createIndexBuffer(int[] indices) {
    IntBuffer indexBuffer;/* w ww. j  a va 2  s.co m*/

    ByteBuffer bb = ByteBuffer.allocateDirect(indices.length * 4);
    bb.order(ByteOrder.nativeOrder());
    indexBuffer = bb.asIntBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);

    return indexBuffer;
}

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);//from w  w  w . j av  a  2 s.  c o  m
    buffer.position(0);
    return buffer;
}

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

public static IntBuffer arrayToIntBuffer(int[] inputArray) {
    IntBuffer iBuf = ByteBuffer.allocateDirect(inputArray.length * BYTES_PER_INT).order(ByteOrder.nativeOrder())
            .asIntBuffer();/*from ww  w  .j a v a2s  . c om*/
    iBuf.put(inputArray).position(0);
    return iBuf;
}

From source file:Main.java

public static ShortBuffer createShortBuffer(short[] shortData) {
    ShortBuffer drawListBuffer;//ww w.j a v  a2 s .  c  om
    ByteBuffer dlb = ByteBuffer.allocateDirect(
            // (# of coordinate values * 2 bytes per short)
            shortData.length * 2);
    dlb.order(ByteOrder.nativeOrder());
    drawListBuffer = dlb.asShortBuffer();
    drawListBuffer.put(shortData);
    drawListBuffer.position(0);
    return drawListBuffer;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(int size) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(size * BYTES_PER_FLOAT);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asFloatBuffer();
}

From source file:Main.java

public static ShortBuffer createShortBuffer(short[] data) {
    ShortBuffer sb = ByteBuffer.allocateDirect(data.length * BYTES_PER_SHORT).order(ByteOrder.nativeOrder())
            .asShortBuffer();//from   w  w  w .  ja  v  a2s  .c o m
    sb.put(data).position(0);
    return sb;
}

From source file:Main.java

public static ByteBuffer create(int size) {
    return ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder());
}