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 void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (src.read(buffer) != -1) {
        // prepare the buffer to be drained
        buffer.flip();//from www.j  a  v a  2 s. c o m
        // write to the channel, may block
        dest.write(buffer);
        // If partial transfer, shift remainder down
        // If buffer is empty, same as doing recycle()
        buffer.compact();
    }
    // EOF will leave buffer in fill state
    buffer.flip();
    // make sure the buffer is fully drained.
    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}

From source file:Main.java

/**
 * Creates a {@link FloatBuffer} based on the given data.
 *
 * @param data the data for the buffer//ww  w .ja v  a2 s.c  o m
 * @return the float buffer
 */
public static FloatBuffer createFloatBuffer(final float[] data) {
    ByteBuffer bbVertices = ByteBuffer.allocateDirect(data.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    final FloatBuffer fBuffer = bbVertices.asFloatBuffer();
    fBuffer.put(data);
    fBuffer.position(0);
    return fBuffer;
}

From source file:Main.java

/**
 * Allocates a FloatBuffer with the given length.
 * //from   w w w.  java  2  s  . c  o  m
 * @param length The length of the buffer to allocate (in elements, not bytes!).
 * @return The newly allocated buffer.
 */
public static FloatBuffer allocateFloatBuffer(int length) {
    return ByteBuffer.allocateDirect(length * 4) // float == 4 bytes
            .order(ByteOrder.nativeOrder()).asFloatBuffer();
}

From source file:Main.java

/**
 * Allocates a ShortBuffer with the given length.
 *
 * @param length The length of the buffer to allocate (in elements, not bytes!).
 * @return The newly allocated buffer.//from www  . j  a va 2  s . c  o  m
 */
public static ShortBuffer allocateShortBuffer(int length) {
    return ByteBuffer.allocateDirect(length * 2) // short == 2 bytes
            .order(ByteOrder.nativeOrder()).asShortBuffer();
}

From source file:Main.java

/**
 * Creates a {@link ShortBuffer} based on the given data.
 *
 * @param data the data for the buffer/*w w w  .  j av  a2s . com*/
 * @return the short buffer
 */
public static ShortBuffer createShortBuffer(final short[] data) {
    if (data == null) {
        return null;
    }
    ByteBuffer bbVertices = ByteBuffer.allocateDirect(data.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    final ShortBuffer fBuffer = bbVertices.asShortBuffer();
    fBuffer.put(data);
    fBuffer.position(0);
    return fBuffer;
}

From source file:Main.java

public static FloatBuffer makeFloatBuffer(float[] i_arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(i_arr.length * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(i_arr);//w w w  . j  av a  2 s  . c  o  m
    fb.position(0);
    return fb;
}

From source file:Main.java

public static FloatBuffer makeBuffer(float[] data) {
    ByteBuffer b = ByteBuffer.allocateDirect(data.length * 4);
    b.order(ByteOrder.nativeOrder());
    FloatBuffer buffer = b.asFloatBuffer();
    buffer.put(data);//ww w . j  a va2 s  .  c  o m
    buffer.position(0);
    return buffer;
}

From source file:Main.java

private static final FloatBuffer createDebugColors(int drawMode, int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(size * 4 * 4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer colors = bb.asFloatBuffer();
    if (drawMode == GL10.GL_TRIANGLES) {
        for (int i = 0; i < size * 4; i++) {
            colors.put(triangleColors[i % triangleColors.length]);
        }/*from   ww  w.  ja  v  a  2 s.  c  om*/
    } else {
        for (int i = 0; i < size; i++) {
            colors.put(1f);
            colors.put(0f);
            colors.put(1f);
            colors.put(1f);
        }
    }
    colors.rewind();
    return colors;
}

From source file:Main.java

public static ByteBuffer makeByteBuffer(int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(size);
    bb.position(0);
    return bb;
}

From source file:Main.java

/**
 * Creates a {@link IntBuffer} based on the given data.
 *
 * @param data the data for the buffer//  w  ww  . ja  v  a2 s .  c o m
 * @return the int buffer
 */
public static IntBuffer createIntBuffer(final float[] data) {
    final int[] tmpData = new int[data.length];
    for (int i = 0; i < tmpData.length; i++) {
        tmpData[i] = Float.floatToRawIntBits(data[i]);
    }
    final ByteBuffer bbVertices = ByteBuffer.allocateDirect(tmpData.length * 4);
    bbVertices.order(ByteOrder.nativeOrder());
    final IntBuffer intBuffer = bbVertices.asIntBuffer();
    intBuffer.put(tmpData);
    intBuffer.flip();
    return intBuffer;
}