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 short buffer with native byte order.
 *
 * @param size/*from   ww w.j  av a  2 s.  c o m*/
 *            The data
 * @return The created direct short buffer
 */

public static ShortBuffer createDirectShortBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * SHORT_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asShortBuffer();
}

From source file:org.quickserver.util.pool.ByteBufferObjectFactory.java

public Object makeObject() {
    if (useDirectByteBuffer)
        return ByteBuffer.allocateDirect(bufferSize);
    else/*w  ww  .  j  a va 2  s  .c om*/
        return ByteBuffer.allocate(bufferSize);
}

From source file:org.italiangrid.storm.webdav.fs.attrs.DefaultExtendedFileAttributesHelper.java

protected String getAttributeValue(UserDefinedFileAttributeView view, String attributeName) throws IOException {

    if (view.list().contains(attributeName)) {
        ByteBuffer buffer = ByteBuffer.allocateDirect(view.size(attributeName));
        view.read(attributeName, buffer);
        buffer.flip();// ww  w. ja  v  a  2  s . com
        return StandardCharsets.UTF_8.decode(buffer).toString();
    } else {
        return "";
    }
}

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

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

public static IntBuffer createDirectIntegerBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * INTEGER_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asIntBuffer();
}

From source file:io.uploader.drive.util.FileUtils.java

public static String readAllAndgetMD5(InputStream in) throws IOException {
    com.google.common.hash.HashingInputStream his = null;
    try {//  w ww  . j  a  v  a  2  s . co m
        his = new com.google.common.hash.HashingInputStream(Hashing.md5(), in);

        final int bufferSize = 2097152;
        final ReadableByteChannel inputChannel = Channels.newChannel(his);
        final ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize);
        while (inputChannel.read(buffer) != -1) {
            buffer.clear();
        }
        /*
        byte[] bytesBuffer = new byte[bufferSize] ;
        int r = his.read(bytesBuffer, 0, bufferSize) ;
        while (r != -1)
           r = his.read(bytesBuffer) ;
        */
        HashCode hc = his.hash();
        return (hc != null) ? (hc.toString()) : (null);
    } finally {
        if (his != null)
            his.close();
    }
}

From source file:Triangle.java

public Triangle() {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
    mProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgram, vertexShader);
    GLES20.glAttachShader(mProgram, fragmentShader);
    GLES20.glLinkProgram(mProgram);//from   w  w w .  j ava  2s  . c o m

    ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());

    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(triangleCoords);
    vertexBuffer.position(0);
}

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

public static void decodeBulk(byte[][] readBufs, byte[][] writeBufs, int[] erasedLocation, int dataStart,
        int dataLen, int stripeSize, int paritySize) {
    ByteBuffer[] inputBuffers = new ByteBuffer[readBufs.length];
    ByteBuffer[] outputBuffers = new ByteBuffer[writeBufs.length];
    for (int i = 0; i < writeBufs.length; i++) {
        outputBuffers[i] = ByteBuffer.allocateDirect(dataLen);
    }/*from w  ww  .  j  a  v  a2  s.com*/
    for (int i = 0; i < readBufs.length; i++) {
        inputBuffers[i] = directify(readBufs[i], dataStart, dataLen);
    }
    nativeDecodeBulk(inputBuffers, outputBuffers, erasedLocation, dataLen, stripeSize, paritySize,
            erasedLocation.length);
    for (int i = 0; i < writeBufs.length; i++) {
        outputBuffers[i].get(writeBufs[i], dataStart, dataLen);
    }
}

From source file:me.schiz.jmeter.ring.udp.EventLoopRunnable.java

public EventLoopRunnable(Ring ring) {
    this.ring = ring;
    byteBuffer = ByteBuffer.allocateDirect(ring.getBufferSize());
    registerQueue = new ArrayBlockingQueue<KeyValue>(REGS_PER_ITERATION * 4);

    try {/*from  ww w.  j a  v a  2 s  .co  m*/
        this.selector = Selector.open();
    } catch (IOException e) {
        log.error("Can't open selector", e);
    }
}

From source file:net.kungfoo.grizzly.proxy.impl.ProxyProcessingInfo.java

public ProxyProcessingInfo() {
    super();//ww  w  .j ava2s  . c  o  m
    this.originState = IDLE;
    this.clientState = IDLE;
    this.inBuffer = ByteBuffer.allocateDirect(1024 * 8);
    this.outBuffer = ByteBuffer.allocateDirect(1024 * 8);
}

From source file:com.mgmtp.perfload.perfalyzer.util.IoUtilities.java

/**
 * Copies the content from one channel to another.
 *
 * @param srcChannel//ww  w .ja v a  2s  . c  o  m
 *       the source channel to copy from
 * @param destChannel
 *       the destination channel to copy to
 */
public static void copy(final ReadableByteChannel srcChannel, final WritableByteChannel destChannel)
        throws IOException {
    final ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
    while (srcChannel.read(buffer) != -1) {
        // flip the buffer so it can be written to the destination channel
        buffer.flip();

        // write to the destination channel
        destChannel.write(buffer);

        // If partial transfer, shift remainder down so it does not get lost
        // If buffer is empty, this is the same as calling clear()
        buffer.compact();
    }

    // EOF will leave buffer in fill state
    buffer.flip();

    // make sure the buffer is fully drained
    while (buffer.hasRemaining()) {
        destChannel.write(buffer);
    }
}