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:de.ailis.threedee.utils.BufferUtils.java

/**
 * Creates a direct byte buffer with native byte order.
 *
 * @param size/*from  w  w  w .j  av  a 2  s  .  com*/
 *            The data
 * @return The created direct byte buffer
 */

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

From source file:BufferTest.java

private void run() {
    final int size = 100000;

    bb = ByteBuffer.allocateDirect(4 * size).order(ByteOrder.nativeOrder());
    floats = bb.asFloatBuffer();//from ww w.j av a2  s .  c o m

    data = data(size);

    System.out.println("# size\tsingle\tindexed\tbatch");

    int base = 0;

    int e = size;
    while (base + e > 100) {
        test(base + e);
        e /= 2;
    }
}

From source file:Main.java

public static void copy(ReadableByteChannel in, WritableByteChannel out) throws IOException {
    // First, we need a buffer to hold blocks of copied bytes.
    ByteBuffer buffer = ByteBuffer.allocateDirect(32 * 1024);

    // Now loop until no more bytes to read and the buffer is empty
    while (in.read(buffer) != -1 || buffer.position() > 0) {
        // The read() call leaves the buffer in "fill mode". To prepare
        // to write bytes from the bufferwe have to put it in "drain mode"
        // by flipping it: setting limit to position and position to zero
        buffer.flip();//  w w w.  jav a2  s.co m

        // Now write some or all of the bytes out to the output channel
        out.write(buffer);

        // Compact the buffer by discarding bytes that were written,
        // and shifting any remaining bytes. This method also
        // prepares the buffer for the next call to read() by setting the
        // position to the limit and the limit to the buffer capacity.
        buffer.compact();
    }
}

From source file:ChannelToWriter.java

/**
 * Read bytes from the specified channel, decode them using the specified
 * Charset, and write the resulting characters to the specified writer
 *//*from   www.  ja  v a  2 s .c o m*/
public static void copy(ReadableByteChannel channel, Writer writer, Charset charset) throws IOException {
    // Get and configure the CharsetDecoder we'll use
    CharsetDecoder decoder = charset.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.IGNORE);
    decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);

    // Get the buffers we'll use, and the backing array for the CharBuffer.
    ByteBuffer bytes = ByteBuffer.allocateDirect(2 * 1024);
    CharBuffer chars = CharBuffer.allocate(2 * 1024);
    char[] array = chars.array();

    while (channel.read(bytes) != -1) { // Read from channel until EOF
        bytes.flip(); // Switch to drain mode for decoding
        // Decode the byte buffer into the char buffer.
        // Pass false to indicate that we're not done.
        decoder.decode(bytes, chars, false);

        // Put the char buffer into drain mode, and write its contents
        // to the Writer, reading them from the backing array.
        chars.flip();
        writer.write(array, chars.position(), chars.remaining());

        // Discard all bytes we decoded, and put the byte buffer back into
        // fill mode. Since all characters were output, clear that buffer.
        bytes.compact(); // Discard decoded bytes
        chars.clear(); // Clear the character buffer
    }

    // At this point there may still be some bytes in the buffer to decode
    // So put the buffer into drain mode call decode() a final time, and
    // finish with a flush().
    bytes.flip();
    decoder.decode(bytes, chars, true); // True means final call
    decoder.flush(chars); // Flush any buffered chars
    // Write these final chars (if any) to the writer.
    chars.flip();
    writer.write(array, chars.position(), chars.remaining());
    writer.flush();
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Creates a direct float buffer with native byte order.
 *
 * @param size/*from  ww  w  .  jav  a 2  s  . c o m*/
 *            The data
 * @return The created direct float buffer
 */

public static FloatBuffer createDirectFloatBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * FLOAT_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asFloatBuffer();
}

From source file:org.apache.hadoop.util.NativeJerasure.java

public static void encodeBulk(byte[][] inputs, byte[][] outputs, int stripeSize, int paritySize) {
    ByteBuffer[] inputBuffers = new ByteBuffer[inputs.length];
    ByteBuffer[] outputBuffers = new ByteBuffer[outputs.length];
    int bufferLen = inputs[0].length;
    for (int i = 0; i < outputs.length; i++) {
        outputBuffers[i] = ByteBuffer.allocateDirect(bufferLen);
    }/*from ww  w .  ja va  2 s  .  com*/
    for (int i = 0; i < inputs.length; i++) {
        inputBuffers[i] = directify(inputs[i], 0, bufferLen);
    }
    nativeEncodeBulk(inputBuffers, outputBuffers, stripeSize, paritySize, bufferLen);
    for (int i = 0; i < outputs.length; i++) {
        outputBuffers[i].get(outputs[i]);
    }
}

From source file:Main.java

private static ByteBuffer convertImageData(BufferedImage bufferedImage) {
    ByteBuffer imageBuffer;//from  w  ww  .  j av  a2 s.  c  o m
    WritableRaster raster;
    BufferedImage texImage;

    // for a texture
    if (bufferedImage.getColorModel().hasAlpha()) {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 4, null);
        texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>());
    } else {
        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(),
                bufferedImage.getHeight(), 3, null);
        texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>());
    }

    // copy the source image into the produced image
    Graphics g = texImage.getGraphics();
    g.setColor(new Color(0f, 0f, 0f, 0f));
    g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight());
    g.drawImage(bufferedImage, 0, 0, null);

    // build a byte buffer from the temporary image
    // that be used by OpenGL to produce a texture.
    byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();

    imageBuffer = ByteBuffer.allocateDirect(data.length);
    imageBuffer.order(ByteOrder.nativeOrder());
    imageBuffer.put(data, 0, data.length);
    imageBuffer.flip();

    return imageBuffer;
}

From source file:com.thinkberg.webdav.Util.java

public static long copyStream(final InputStream is, final OutputStream os) throws IOException {
    ReadableByteChannel rbc = Channels.newChannel(is);
    WritableByteChannel wbc = Channels.newChannel(os);

    int bytesWritten = 0;
    final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
    while (rbc.read(buffer) != -1) {
        buffer.flip();/*ww w . ja v a 2 s .c o  m*/
        bytesWritten += wbc.write(buffer);
        buffer.compact();
    }
    buffer.flip();
    while (buffer.hasRemaining()) {
        bytesWritten += wbc.write(buffer);
    }

    rbc.close();
    wbc.close();

    return bytesWritten;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(final int size) {
    final FloatBuffer buf = ByteBuffer.allocateDirect(4 * size).order(ByteOrder.nativeOrder()).asFloatBuffer();
    buf.clear();/*from   w w w.ja  v a2  s.  co  m*/
    return buf;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(final int size) {
    final FloatBuffer buf = ByteBuffer.allocateDirect(SIZEOF_FLOAT * size).order(ByteOrder.nativeOrder())
            .asFloatBuffer();/*ww w  . j  ava  2  s  .c  o m*/
    buf.clear();
    return buf;
}