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 IntBuffer newIntBuffer(int numInts) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(numInts * 4);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asIntBuffer();
}

From source file:Main.java

/**buffer methods*/

public static IntBuffer makeIntBuffer(int[] array) {
    final int integerSize = Integer.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * integerSize);
    byteBuffer.order(ByteOrder.nativeOrder());
    IntBuffer intBuffer = byteBuffer.asIntBuffer();
    intBuffer.put(array);/*from w  w  w.  ja  v  a2 s . c om*/
    intBuffer.position(0);
    return intBuffer;
}

From source file:Main.java

public static CharBuffer newCharBuffer(int numChars) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(numChars * 2);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asCharBuffer();
}

From source file:Main.java

public static LongBuffer newLongBuffer(int numLongs) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(numLongs * 8);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asLongBuffer();
}

From source file:Main.java

public static ShortBuffer newShortBuffer(int numShorts) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(numShorts * 2);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asShortBuffer();
}

From source file:Main.java

public static FloatBuffer newFloatBuffer(int numFloats) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(numFloats * 4);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asFloatBuffer();
}

From source file:Main.java

public static DoubleBuffer newDoubleBuffer(int numDoubles) {
    ByteBuffer buffer = ByteBuffer.allocateDirect(numDoubles * 8);
    buffer.order(ByteOrder.nativeOrder());
    return buffer.asDoubleBuffer();
}

From source file:Main.java

public static DoubleBuffer createDoubleBuffer(final int size) {
    final DoubleBuffer buf = ByteBuffer.allocateDirect(SIZEOF_DOUBLE * size).order(ByteOrder.nativeOrder())
            .asDoubleBuffer();// w  w  w .  j  av  a  2 s . c  o m
    buf.clear();
    return buf;
}

From source file:Main.java

public static ShortBuffer createShortBuffer(final int size) {
    final ShortBuffer buf = ByteBuffer.allocateDirect(SIZEOF_SHORT * size).order(ByteOrder.nativeOrder())
            .asShortBuffer();/*  w  w w. j  a  v  a 2s  .  com*/
    buf.clear();
    return buf;
}

From source file:Main.java

public static void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
    ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY);
    while (src.read(buffer) != -1) {
        buffer.flip();/* w ww .  j a  v  a2s  .c om*/
        dest.write(buffer);
        buffer.compact();
    }

    buffer.flip();

    while (buffer.hasRemaining()) {
        dest.write(buffer);
    }
}