Example usage for java.nio ByteBuffer isDirect

List of usage examples for java.nio ByteBuffer isDirect

Introduction

In this page you can find the example usage for java.nio ByteBuffer isDirect.

Prototype

public abstract boolean isDirect();

Source Link

Document

Indicates whether this buffer is direct.

Usage

From source file:com.intel.chimera.cipher.Openssl.java

/**
 * Continues a multiple-part encryption or decryption operation. The data
 * is encrypted or decrypted, depending on how this cipher was initialized.
 * <p/>/*from w  w  w  .jav a 2  s. c om*/
 *
 * All <code>input.remaining()</code> bytes starting at
 * <code>input.position()</code> are processed. The result is stored in
 * the output buffer.
 * <p/>
 *
 * Upon return, the input buffer's position will be equal to its limit;
 * its limit will not have changed. The output buffer's position will have
 * advanced by n, when n is the value returned by this method; the output
 * buffer's limit will not have changed.
 * <p/>
 *
 * If <code>output.remaining()</code> bytes are insufficient to hold the
 * result, a <code>ShortBufferException</code> is thrown.
 *
 * @param input the input ByteBuffer
 * @param output the output ByteBuffer
 * @return int number of bytes stored in <code>output</code>
 * @throws ShortBufferException if there is insufficient space in the
 * output buffer
 */
public int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException {
    checkState();
    Utils.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required.");
    int len = OpensslNative.update(context, input, input.position(), input.remaining(), output,
            output.position(), output.remaining());
    input.position(input.limit());
    output.position(output.position() + len);
    return len;
}

From source file:com.intel.chimera.codec.OpensslCipher.java

/**
 * Finishes a multiple-part operation. The data is encrypted or decrypted,
 * depending on how this cipher was initialized.
 * <p/>//from w w w.  ja  v a  2s  .c  o  m
 * 
 * The result is stored in the output buffer. Upon return, the output buffer's
 * position will have advanced by n, where n is the value returned by this
 * method; the output buffer's limit will not have changed.
 * <p/>
 * 
 * If <code>output.remaining()</code> bytes are insufficient to hold the result,
 * a <code>ShortBufferException</code> is thrown.
 * <p/>
 * 
 * Upon finishing, this method resets this cipher object to the state it was
 * in when previously initialized. That is, the object is available to encrypt
 * or decrypt more data.
 * <p/>
 * 
 * If any exception is thrown, this cipher object need to be reset before it 
 * can be used again.
 * 
 * @param output the output ByteBuffer
 * @return int number of bytes stored in <code>output</code>
 * @throws ShortBufferException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 */
public int doFinal(ByteBuffer output)
        throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
    checkState();
    Preconditions.checkArgument(output.isDirect(), "Direct buffer is required.");
    int len = OpensslCipherNative.doFinal(context, output, output.position(), output.remaining());
    output.position(output.position() + len);
    return len;
}

From source file:com.intel.chimera.cipher.Openssl.java

/**
 * Finishes a multiple-part operation. The data is encrypted or decrypted,
 * depending on how this cipher was initialized.
 * <p/>//from www.  j ava 2  s  .c o  m
 *
 * The result is stored in the output buffer. Upon return, the output buffer's
 * position will have advanced by n, where n is the value returned by this
 * method; the output buffer's limit will not have changed.
 * <p/>
 *
 * If <code>output.remaining()</code> bytes are insufficient to hold the result,
 * a <code>ShortBufferException</code> is thrown.
 * <p/>
 *
 * Upon finishing, this method resets this cipher object to the state it was
 * in when previously initialized. That is, the object is available to encrypt
 * or decrypt more data.
 * <p/>
 *
 * If any exception is thrown, this cipher object need to be reset before it
 * can be used again.
 *
 * @param output the output ByteBuffer
 * @return int number of bytes stored in <code>output</code>
 * @throws ShortBufferException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 */
public int doFinal(ByteBuffer output)
        throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
    checkState();
    Utils.checkArgument(output.isDirect(), "Direct buffer is required.");
    int len = OpensslNative.doFinal(context, output, output.position(), output.remaining());
    output.position(output.position() + len);
    return len;
}

From source file:com.tinspx.util.io.ChannelSourceTest.java

@Test
public void testMemoizeSource() throws IOException {
    ByteSource source = of(ByteBuffer.allocate(16));
    assertSame(source, ChannelSource.memoize(source));

    source = of(new byte[16]);
    assertSame(source, ChannelSource.memoize(source));

    source = memoize(ByteSource.empty());
    assertSame(source, source);/*from w  w  w .  j  a  v a 2 s . c  o  m*/

    source = memoize(ByteSourceTests.force(of(INPUT)));
    assertTrue(source instanceof ChannelSource.MemoizeChannelSource);
    ByteSourceTests.testByteSource(source, INPUT);

    source = memoize(ByteSourceTests.force(of(ByteBuffer.wrap(INPUT))));
    assertTrue(source instanceof ChannelSource.MemoizeChannelSource);
    ByteSourceTests.testByteSource(source, INPUT);

    source = memoize(ByteSourceTests.force(of(ByteBuffer.wrap(INPUT).asReadOnlyBuffer())));
    assertTrue(source instanceof ChannelSource.MemoizeChannelSource);
    ByteSourceTests.testByteSource(source, INPUT);

    ByteBuffer direct = ByteBuffer.allocateDirect(INPUT.length);
    assertTrue(direct.isDirect());
    direct.put(INPUT).flip();
    source = memoize(ByteSourceTests.force(of(direct)));
    assertTrue(source instanceof ChannelSource.MemoizeChannelSource);
    ByteSourceTests.testByteSource(source, INPUT);

    source = memoize(new BAOutputStream(INPUT, INPUT.length).asByteSource());
    assertTrue(source instanceof ChannelSource.MemoizeChannelSource);
    ByteSourceTests.testByteSource(source, INPUT);

    source = memoize(new ByteSourceTests.ForcedByteArray(INPUT));
    assertTrue(source instanceof ChannelSource.MemoizeChannelSource);
    ByteSourceTests.testByteSource(source, INPUT);

    source = memoize(ByteSource.wrap(INPUT));
    assertTrue(source instanceof ChannelSource.MemoizeChannelSource);
    ByteSourceTests.testByteSource(source, INPUT);

    ByteSourceTests.assertEmpty(memoize(ByteSource.empty()));
    ByteSourceTests.assertEmpty(memoize(ChannelSource.empty()));
}

From source file:org.apache.hadoop.crypto.OpensslCipher.java

/**
 * Continues a multiple-part encryption or decryption operation. The data
 * is encrypted or decrypted, depending on how this cipher was initialized.
 * <p/>// w  w w  .j av  a 2s .co m
 * 
 * All <code>input.remaining()</code> bytes starting at 
 * <code>input.position()</code> are processed. The result is stored in
 * the output buffer.
 * <p/>
 * 
 * Upon return, the input buffer's position will be equal to its limit;
 * its limit will not have changed. The output buffer's position will have
 * advanced by n, when n is the value returned by this method; the output
 * buffer's limit will not have changed.
 * <p/>
 * 
 * If <code>output.remaining()</code> bytes are insufficient to hold the
 * result, a <code>ShortBufferException</code> is thrown.
 * 
 * @param input the input ByteBuffer
 * @param output the output ByteBuffer
 * @return int number of bytes stored in <code>output</code>
 * @throws ShortBufferException if there is insufficient space in the
 * output buffer
 */
public int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException {
    checkState();
    Preconditions.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required.");
    int len = update(context, input, input.position(), input.remaining(), output, output.position(),
            output.remaining());
    input.position(input.limit());
    output.position(output.position() + len);
    return len;
}

From source file:org.apache.hadoop.crypto.OpensslCipher.java

/**
 * Finishes a multiple-part operation. The data is encrypted or decrypted,
 * depending on how this cipher was initialized.
 * <p/>//from ww w  .  java2  s.c o  m
 * 
 * The result is stored in the output buffer. Upon return, the output buffer's
 * position will have advanced by n, where n is the value returned by this
 * method; the output buffer's limit will not have changed.
 * <p/>
 * 
 * If <code>output.remaining()</code> bytes are insufficient to hold the result,
 * a <code>ShortBufferException</code> is thrown.
 * <p/>
 * 
 * Upon finishing, this method resets this cipher object to the state it was
 * in when previously initialized. That is, the object is available to encrypt
 * or decrypt more data.
 * <p/>
 * 
 * If any exception is thrown, this cipher object need to be reset before it 
 * can be used again.
 * 
 * @param output the output ByteBuffer
 * @return int number of bytes stored in <code>output</code>
 * @throws ShortBufferException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 */
public int doFinal(ByteBuffer output)
        throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
    checkState();
    Preconditions.checkArgument(output.isDirect(), "Direct buffer is required.");
    int len = doFinal(context, output, output.position(), output.remaining());
    output.position(output.position() + len);
    return len;
}

From source file:org.apache.hadoop.hbase.io.ByteBufferPool.java

/**
 * Return back a ByteBuffer after its use. Do not try to return put back a ByteBuffer, not
 * obtained from this pool.//from  w w  w .  jav a  2  s  .  co m
 * @param buf ByteBuffer to return.
 */
public void putbackBuffer(ByteBuffer buf) {
    if (buf.capacity() != this.bufferSize || (this.directByteBuffer ^ buf.isDirect())) {
        LOG.warn("Trying to put a buffer, not created by this pool! Will be just ignored");
        return;
    }
    buffers.offer(buf);
}

From source file:org.apache.hadoop.hbase.KeyValueUtil.java

/**
 * Creates a new KeyValue object positioned in the supplied ByteBuffer and sets the ByteBuffer's
 * position to the start of the next KeyValue. Does not allocate a new array or copy data.
 * @param bb//from  w ww.  j  a  v  a 2s  .c o m
 * @param includesMvccVersion
 * @param includesTags
 */
public static KeyValue nextShallowCopy(final ByteBuffer bb, final boolean includesMvccVersion,
        boolean includesTags) {
    if (bb.isDirect()) {
        throw new IllegalArgumentException("only supports heap buffers");
    }
    if (bb.remaining() < 1) {
        return null;
    }
    KeyValue keyValue = null;
    int underlyingArrayOffset = bb.arrayOffset() + bb.position();
    int keyLength = bb.getInt();
    int valueLength = bb.getInt();
    ByteBufferUtils.skip(bb, keyLength + valueLength);
    int tagsLength = 0;
    if (includesTags) {
        // Read short as unsigned, high byte first
        tagsLength = ((bb.get() & 0xff) << 8) ^ (bb.get() & 0xff);
        ByteBufferUtils.skip(bb, tagsLength);
    }
    int kvLength = (int) KeyValue.getKeyValueDataStructureSize(keyLength, valueLength, tagsLength);
    keyValue = new KeyValue(bb.array(), underlyingArrayOffset, kvLength);
    if (includesMvccVersion) {
        long mvccVersion = ByteBufferUtils.readVLong(bb);
        keyValue.setSequenceId(mvccVersion);
    }
    return keyValue;
}

From source file:org.apache.hadoop.hbase.util.DirectMemoryUtils.java

/**
 * DirectByteBuffers are garbage collected by using a phantom reference and a
 * reference queue. Every once a while, the JVM checks the reference queue and
 * cleans the DirectByteBuffers. However, as this doesn't happen
 * immediately after discarding all references to a DirectByteBuffer, it's
 * easy to OutOfMemoryError yourself using DirectByteBuffers. This function
 * explicitly calls the Cleaner method of a DirectByteBuffer.
 * /* w  w w  .jav  a  2  s  .co m*/
 * @param toBeDestroyed
 *          The DirectByteBuffer that will be "cleaned". Utilizes reflection.
 *          
 */
public static void destroyDirectByteBuffer(ByteBuffer toBeDestroyed) throws IllegalArgumentException,
        IllegalAccessException, InvocationTargetException, SecurityException, NoSuchMethodException {

    Preconditions.checkArgument(toBeDestroyed.isDirect(), "toBeDestroyed isn't direct!");

    Method cleanerMethod = toBeDestroyed.getClass().getMethod("cleaner");
    cleanerMethod.setAccessible(true);
    Object cleaner = cleanerMethod.invoke(toBeDestroyed);
    Method cleanMethod = cleaner.getClass().getMethod("clean");
    cleanMethod.setAccessible(true);
    cleanMethod.invoke(cleaner);
}

From source file:org.apache.hadoop.hbase.util.UnsafeAccess.java

/**
 * Reads bytes at the given offset as a short value.
 * @param buf//from   w ww . j  a v  a 2s.c om
 * @param offset
 * @return short value at offset
 */
static short getAsShort(ByteBuffer buf, int offset) {
    if (buf.isDirect()) {
        return theUnsafe.getShort(((DirectBuffer) buf).address() + offset);
    }
    return theUnsafe.getShort(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset);
}