Example usage for java.nio ByteBuffer arrayOffset

List of usage examples for java.nio ByteBuffer arrayOffset

Introduction

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

Prototype

public final int arrayOffset() 

Source Link

Document

Returns the offset of the byte array which this buffer is based on, if there is one.

Usage

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 w w  w  .  ja v a2 s  .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

/**
 * Put a long value out to the specified BB position in big-endian format.
 * @param buf the byte buffer//w  w w .j a va  2  s.  c  o  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:com.glaf.core.util.ByteBufferUtils.java

public static void arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset, int length) {
    if (buffer.hasArray())
        System.arraycopy(buffer.array(), buffer.arrayOffset() + position, bytes, offset, length);
    else// w ww.j  a  va  2 s. c  o m
        ((ByteBuffer) buffer.duplicate().position(position)).get(bytes, offset, 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  2s  .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.tajo.storage.orc.OrcScanner.java

private static OrcProto.PostScript extractPostScript(ByteBuffer bb, Path path, int psLen, int psAbsOffset)
        throws IOException {
    // TODO: when PB is upgraded to 2.6, newInstance(ByteBuffer) method should be used here.
    assert bb.hasArray();
    CodedInputStream in = CodedInputStream.newInstance(bb.array(), bb.arrayOffset() + psAbsOffset, psLen);
    OrcProto.PostScript ps = OrcProto.PostScript.parseFrom(in);
    checkOrcVersion(LOG, path, ps.getVersionList());

    // Check compression codec.
    switch (ps.getCompression()) {
    case NONE://from   w  w  w. j  a  v  a  2s.  c  o  m
        break;
    case ZLIB:
        break;
    case SNAPPY:
        break;
    case LZO:
        break;
    default:
        throw new IllegalArgumentException("Unknown compression");
    }
    return ps;
}

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.// w  w  w . j av  a 2s .com
 *
 * @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./*from   w  ww.j  a  va  2 s  .co  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:com.glaf.core.util.ByteBufferUtils.java

/**
 * buffer?/*from w  ww .  j a  v a 2s .c o m*/
 * 
 * @param buffer
 * @return
 */
public static byte[] toArray(ByteBuffer buffer) {
    // ?heap buffer
    if (buffer.hasArray()) {
        byte[] array = buffer.array();
        int from = buffer.arrayOffset() + buffer.position();
        return Arrays.copyOfRange(array, from, from + buffer.remaining());
    }
    //  direct buffer
    else {
        byte[] to = new byte[buffer.remaining()];
        buffer.slice().get(to);
        return to;
    }
}

From source file:com.glaf.core.util.ByteBufferUtils.java

/**
 * Transfer bytes from one ByteBuffer to another. This function acts as
 * System.arrayCopy() but for ByteBuffers.
 * //from   ww w .  j  a  v a 2  s .  c  o m
 * @param src
 *            the source ByteBuffer
 * @param srcPos
 *            starting position in the source ByteBuffer
 * @param dst
 *            the destination ByteBuffer
 * @param dstPos
 *            starting position in the destination ByteBuffer
 * @param length
 *            the number of bytes to copy
 */
public static void arrayCopy(ByteBuffer src, int srcPos, ByteBuffer dst, int dstPos, int length) {
    if (src.hasArray() && dst.hasArray()) {
        System.arraycopy(src.array(), src.arrayOffset() + srcPos, dst.array(), dst.arrayOffset() + dstPos,
                length);
    } else {
        if (src.limit() - srcPos < length || dst.limit() - dstPos < length)
            throw new IndexOutOfBoundsException();

        for (int i = 0; i < length; i++)

            dst.put(dstPos++, src.get(srcPos++));
    }
}

From source file:com.easemob.dataexport.utils.ConversionUtils.java

public static String string(ByteBuffer bytes) {
    if (bytes == null) {
        return null;
    }//from   ww w.  j  av  a 2 s  . com
    return string(bytes.array(), bytes.arrayOffset() + bytes.position(), bytes.remaining(), UTF8_ENCODING);
}