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

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length);
    byteBuffer.order(ByteOrder.nativeOrder());
    byteBuffer.put(array);//from w w  w  .j  a va2s. c om
    byteBuffer.position(0);
    return byteBuffer;
}

From source file:Main.java

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

From source file:Main.java

public static FloatBuffer toFloatBuffer(final float[] data) {
    final FloatBuffer buffer = ByteBuffer.allocateDirect(data.length * FLOAT_SIZE_BYTES)
            .order(ByteOrder.nativeOrder()).asFloatBuffer();
    buffer.put(data).position(0);//www  .j a va 2  s.  c  o m
    return buffer;
}

From source file:Main.java

public static IntBuffer makeIntBuffer(int[] array)
/*     */ {//from www . ja  va2 s  .  co  m
    /*  52 */int integerSize = 4;
    /*  53 */ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * 4);
    /*  54 */byteBuffer.order(ByteOrder.nativeOrder());
    /*  55 */IntBuffer intBuffer = byteBuffer.asIntBuffer();
    /*  56 */intBuffer.put(array);
    /*  57 */intBuffer.position(0);
    /*  58 */return intBuffer;
    /*     */}

From source file:Main.java

public static FloatBuffer makeFloatBuffer3(float $a, float $b, float $c) {
    ByteBuffer b = ByteBuffer.allocateDirect(3 * BYTES_PER_FLOAT);
    b.order(ByteOrder.nativeOrder());
    FloatBuffer buffer = b.asFloatBuffer();
    buffer.put($a);/* w w  w . j  a  v a2 s .  c  om*/
    buffer.put($b);
    buffer.put($c);
    buffer.position(0);
    return buffer;
}

From source file:Main.java

/**
 * // w w w .jav a2  s . co m
 * @param arr
 * @return
 */
public static final ByteBuffer createByteBuffer(byte[] arr) {
    int len = sizeofB(arr.length);
    ByteBuffer bb = ByteBuffer.allocateDirect(len);
    bb.order(ByteOrder.nativeOrder());
    ByteBuffer fb = bb;
    fb.put(arr);
    fb.position(0);
    return fb;
}

From source file:Main.java

public static FloatBuffer makeFloatBuffer4(float $a, float $b, float $c, float $d) {
    ByteBuffer b = ByteBuffer.allocateDirect(4 * BYTES_PER_FLOAT);
    b.order(ByteOrder.nativeOrder());
    FloatBuffer buffer = b.asFloatBuffer();
    buffer.put($a);//  w  w w  . ja v a 2s.  co m
    buffer.put($b);
    buffer.put($c);
    buffer.put($d);
    buffer.position(0);
    return buffer;
}

From source file:Main.java

public static FloatBuffer transportArrayToNativeBuffer(float[] fArray) {
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(fArray.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());
    return (FloatBuffer) byteBuffer.asFloatBuffer().put(fArray).position(0);
}

From source file:Main.java

/**
 * /*from  w  w w  .j av  a 2 s  . c  o  m*/
 * @since 1.0.0
 * @param v
 * @return the FloatBuffer
 */
public static FloatBuffer toFloatBuffer(float[] v) {
    ByteBuffer buf = ByteBuffer.allocateDirect(v.length * 4);
    buf.order(ByteOrder.nativeOrder());
    FloatBuffer buffer = buf.asFloatBuffer();
    buffer.put(v);
    buffer.position(0);
    return buffer;
}

From source file:Main.java

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