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:org.apache.hadoop.hbase.util.UnsafeAccess.java

/**
 * Reads bytes at the given offset as an int value.
 * @param buf//from www  .j  a  v  a  2  s . co m
 * @param offset
 * @return int value at offset
 */
static int getAsInt(ByteBuffer buf, int offset) {
    if (buf.isDirect()) {
        return theUnsafe.getInt(((DirectBuffer) buf).address() + offset);
    }
    return theUnsafe.getInt(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset);
}

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

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

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

/**
 * Put an int value out to the specified ByteBuffer offset in big-endian format.
 * @param buf the ByteBuffer to write to
 * @param offset offset in the ByteBuffer
 * @param val int to write out/*  ww  w. j av a  2s. c om*/
 * @return incremented offset
 */
public static int putInt(ByteBuffer buf, int offset, int val) {
    if (littleEndian) {
        val = Integer.reverseBytes(val);
    }
    if (buf.isDirect()) {
        theUnsafe.putInt(((DirectBuffer) buf).address() + offset, val);
    } else {
        theUnsafe.putInt(buf.array(), offset + buf.arrayOffset() + BYTE_ARRAY_BASE_OFFSET, val);
    }
    return offset + Bytes.SIZEOF_INT;
}

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

/**
 * Copies the bytes from given array's offset to length part into the given buffer.
 * @param src/*from ww  w.j  a  va 2s.c  o  m*/
 * @param srcOffset
 * @param dest
 * @param destOffset
 * @param length
 */
public static void copy(byte[] src, int srcOffset, ByteBuffer dest, int destOffset, int length) {
    long destAddress = destOffset;
    Object destBase = null;
    if (dest.isDirect()) {
        destAddress = destAddress + ((DirectBuffer) dest).address();
    } else {
        destAddress = destAddress + BYTE_ARRAY_BASE_OFFSET + dest.arrayOffset();
        destBase = dest.array();
    }
    long srcAddress = srcOffset + BYTE_ARRAY_BASE_OFFSET;
    theUnsafe.copyMemory(src, srcAddress, destBase, destAddress, length);
}

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

/**
 * Copies specified number of bytes from given offset of {@code src} ByteBuffer to the
 * {@code dest} array./*from  w w  w  . j  a  v a  2 s.  c om*/
 *
 * @param src
 * @param srcOffset
 * @param dest
 * @param destOffset
 * @param length
 */
public static void copy(ByteBuffer src, int srcOffset, byte[] dest, int destOffset, int length) {
    long srcAddress = srcOffset;
    Object srcBase = null;
    if (src.isDirect()) {
        srcAddress = srcAddress + ((DirectBuffer) src).address();
    } else {
        srcAddress = srcAddress + BYTE_ARRAY_BASE_OFFSET + src.arrayOffset();
        srcBase = src.array();
    }
    long destAddress = destOffset + BYTE_ARRAY_BASE_OFFSET;
    theUnsafe.copyMemory(srcBase, srcAddress, dest, destAddress, length);
}

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

/**
 * Copies specified number of bytes from given offset of {@code src} buffer into the {@code dest}
 * buffer.//  ww w  .ja  v a  2s.  c o  m
 *
 * @param src
 * @param srcOffset
 * @param dest
 * @param destOffset
 * @param length
 */
public static void copy(ByteBuffer src, int srcOffset, ByteBuffer dest, int destOffset, int length) {
    long srcAddress, destAddress;
    Object srcBase = null, destBase = null;
    if (src.isDirect()) {
        srcAddress = srcOffset + ((DirectBuffer) src).address();
    } else {
        srcAddress = srcOffset + src.arrayOffset() + BYTE_ARRAY_BASE_OFFSET;
        srcBase = src.array();
    }
    if (dest.isDirect()) {
        destAddress = destOffset + ((DirectBuffer) dest).address();
    } else {
        destAddress = destOffset + BYTE_ARRAY_BASE_OFFSET + dest.arrayOffset();
        destBase = dest.array();
    }
    theUnsafe.copyMemory(srcBase, srcAddress, destBase, destAddress, length);
}

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

/**
 * Put a short value out to the specified BB position in big-endian format.
 * @param buf the byte buffer//from  www.j  a  v a  2 s .  c o m
 * @param offset position in the buffer
 * @param val short to write out
 * @return incremented offset
 */
public static int putShort(ByteBuffer buf, int offset, short val) {
    if (littleEndian) {
        val = Short.reverseBytes(val);
    }
    if (buf.isDirect()) {
        theUnsafe.putShort(((DirectBuffer) buf).address() + offset, val);
    } else {
        theUnsafe.putShort(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, val);
    }
    return offset + Bytes.SIZEOF_SHORT;
}

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

/**
 * Put a long value out to the specified BB position in big-endian format.
 * @param buf the byte buffer//from www .  j av  a2s.co m
 * @param offset position in the buffer
 * @param val long to write out
 * @return incremented offset
 */
public static int putLong(ByteBuffer buf, int offset, long val) {
    if (littleEndian) {
        val = Long.reverseBytes(val);
    }
    if (buf.isDirect()) {
        theUnsafe.putLong(((DirectBuffer) buf).address() + offset, val);
    } else {
        theUnsafe.putLong(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, val);
    }
    return offset + Bytes.SIZEOF_LONG;
}

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

/**
 * Put a byte value out to the specified BB position in big-endian format.
 * @param buf the byte buffer/*  w  ww.j  a v a  2s.co  m*/
 * @param offset position in the buffer
 * @param b byte to write out
 * @return incremented offset
 */
public static int putByte(ByteBuffer buf, int offset, byte b) {
    if (buf.isDirect()) {
        theUnsafe.putByte(((DirectBuffer) buf).address() + offset, b);
    } else {
        theUnsafe.putByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset, b);
    }
    return offset + 1;
}

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

/**
 * Returns the byte at the given offset/*from  ww w  . j  a v a  2  s .c o m*/
 * @param buf the buffer to read
 * @param offset the offset at which the byte has to be read
 * @return the byte at the given offset
 */
public static byte toByte(ByteBuffer buf, int offset) {
    if (buf.isDirect()) {
        return theUnsafe.getByte(((DirectBuffer) buf).address() + offset);
    } else {
        return theUnsafe.getByte(buf.array(), BYTE_ARRAY_BASE_OFFSET + buf.arrayOffset() + offset);
    }
}