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 makeShortBuffer(short[] array) {
    if (array == null)
        throw new IllegalArgumentException();

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(2 * array.length);
    byteBuffer.order(ByteOrder.nativeOrder());
    ShortBuffer shortBuffer = byteBuffer.asShortBuffer();
    shortBuffer.put(array);/*from  w  w w  . j  ava  2 s.  c o  m*/
    shortBuffer.position(0);
    return shortBuffer;
}

From source file:Main.java

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

From source file:Main.java

public static ByteBuffer cloneByteBuffer(final ByteBuffer original) {
    // Create clone with same capacity as original.
    final ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity())
            : ByteBuffer.allocate(original.capacity());

    // Create a read-only copy of the original.
    // This allows reading from the original without modifying it.
    final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer();

    // Flip and read from the original.
    readOnlyCopy.flip();//ww w .  ja va  2s  . c o  m
    clone.put(readOnlyCopy);
    clone.position(original.position());
    clone.limit(original.limit());
    clone.order(original.order());
    return clone;
}

From source file:Main.java

/**
 * @param size number of chars the buffer should hold
 * @return the newly allocated char buffer
 *//*from   w  w w.j  a  v  a 2 s  .  co m*/
public static CharBuffer createCharBuffer(int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(2 * size);
    bb.order(ByteOrder.nativeOrder());
    return bb.asCharBuffer();
}

From source file:Main.java

/**
 * @param pCapacity the capacity of the returned {@link FloatBuffer} in floats.
 * @return/*from  ww w. j av a  2s.  co m*/
 */
public static FloatBuffer allocateDirectFloatBuffer(final int pCapacity) {
    return ByteBuffer.allocateDirect(pCapacity * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();
}

From source file:Main.java

/**
 * @param size number of floats the buffer should hold
 * @return the newly allocated float buffer
 *///from   w w w .j  a va  2s.  co m
public static FloatBuffer createFloatBuffer(int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(4 * size);
    bb.order(ByteOrder.nativeOrder());
    return bb.asFloatBuffer();
}

From source file:Main.java

/**
 * Make a direct NIO ByteBuffer from an array of floats
 * @param arr The array//ww  w. j a  v a2s  .c  o m
 * @return The newly created FloatBuffer
 */
public static ByteBuffer makeByteBuffer(byte[] arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length);
    bb.order(ByteOrder.nativeOrder());
    bb.put(arr);
    bb.position(0);
    return bb;
}

From source file:Main.java

/**
 * Make a direct NIO FloatBuffer from an array of floats
 * @param arr The array// w ww . j ava2  s.  c  o  m
 * @return The newly created FloatBuffer
 */
public static FloatBuffer makeFloatBuffer(float[] arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(arr);
    fb.position(0);
    return fb;
}

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//from w w w . j  a  v  a  2 s  .co  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

/**
 * create a Floatbuffer for a given Array
 * @param array/*from  w  w w.  j  a  v  a 2s .  c o m*/
 * @return
 */
public static FloatBuffer createFloatBuffer(float[] array) {
    final int floatSize = Float.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * floatSize);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.put(array);
    floatBuffer.position(0);
    return floatBuffer;
}