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

/**
 * Returns the byte at the given offset//w  w  w .j ava 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);
    }
}

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

public static Object fromByteBuffer(ByteBuffer byteBuffer, Class<?> clazz) {
    if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
        return null;
    }/*from w  w w . java 2 s  .com*/
    if (clazz == null) {
        clazz = Object.class;
    }

    Object obj = null;
    try {
        obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining(), clazz);
    } catch (Exception e) {
        LOG.error("Error parsing SMILE bytes", e);
    }
    return obj;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static int byteBufferToInt(ByteBuffer bytes) {
    if (bytes.remaining() < 4) {
        throw new IllegalArgumentException("An integer must be 4 bytes in size.");
    }//from w ww. ja v a  2  s.co  m
    int n = 0;
    for (int i = 0; i < 4; ++i) {
        n <<= 8;
        n |= bytes.array()[bytes.position() + bytes.arrayOffset() + i] & 0xFF;
    }
    return n;
}

From source file:com.icloud.framework.core.util.FBUtilities.java

public static void writeShortByteArray(ByteBuffer name, DataOutput out) {
    int length = name.remaining();
    assert 0 <= length && length <= MAX_UNSIGNED_SHORT;
    try {/*from   w w  w . j a v  a  2s . com*/
        out.writeByte((length >> 8) & 0xFF);
        out.writeByte(length & 0xFF);
        out.write(name.array(), name.position() + name.arrayOffset(), name.remaining());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.tajo.storage.orc.OrcScanner.java

/**
 * Ensure this is an ORC file to prevent users from trying to read text
 * files or RC files as ORC files.// w w w  .  ja  v  a  2 s  .  com
 * @param in the file being read
 * @param path the filename for error messages
 * @param psLen the postscript length
 * @param buffer the tail of the file
 * @throws IOException
 */
static void ensureOrcFooter(FSDataInputStream in, Path path, int psLen, ByteBuffer buffer) throws IOException {
    int len = OrcFile.MAGIC.length();
    if (psLen < len + 1) {
        throw new IOException("Malformed ORC file " + path + ". Invalid postscript length " + psLen);
    }
    int offset = buffer.arrayOffset() + buffer.position() + buffer.limit() - 1 - len;
    byte[] array = buffer.array();
    // now look for the magic string at the end of the postscript.
    if (!Text.decode(array, offset, len).equals(OrcFile.MAGIC)) {
        // If it isn't there, this may be the 0.11.0 version of ORC.
        // Read the first 3 bytes of the file to check for the header
        byte[] header = new byte[len];
        in.readFully(0, header, 0, len);
        // if it isn't there, this isn't an ORC file
        if (!Text.decode(header, 0, len).equals(OrcFile.MAGIC)) {
            throw new IOException("Malformed ORC file " + path + ". Invalid postscript.");
        }
    }
}

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

/**
 * You should almost never use this. Instead, use the write* methods to
 * avoid copies.// www  .  j  ava  2  s .  co m
 */
public static byte[] getArray(ByteBuffer buffer) {
    int length = buffer.remaining();

    if (buffer.hasArray()) {
        int boff = buffer.arrayOffset() + buffer.position();
        if (boff == 0 && length == buffer.array().length)
            return buffer.array();
        else
            return Arrays.copyOfRange(buffer.array(), boff, boff + length);
    }
    // else, DirectByteBuffer.get() is the fastest route
    byte[] bytes = new byte[length];
    buffer.duplicate().get(bytes);

    return bytes;
}

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

public static void write(ByteBuffer buffer, DataOutput out) throws IOException {
    if (buffer.hasArray()) {
        out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
    } else {/*from  w w w .j a  v  a  2s  .c o m*/
        for (int i = buffer.position(); i < buffer.limit(); i++) {
            out.writeByte(buffer.get(i));
        }
    }
}

From source file:org.usergrid.utils.JsonUtils.java

public static Object fromByteBuffer(ByteBuffer byteBuffer, Class<?> clazz) {
    if ((byteBuffer == null) || !byteBuffer.hasRemaining()) {
        return null;
    }/*from  w w  w .  ja  v a2  s  . com*/
    if (clazz == null) {
        clazz = Object.class;
    }

    Object obj = null;
    try {
        obj = smileMapper.readValue(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(),
                byteBuffer.remaining(), clazz);
    } catch (Exception e) {
        logger.error("Error parsing SMILE bytes", e);
    }
    return obj;
}

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/*from  w w  w.jav  a 2 s .  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:io.mycat.util.ByteBufferUtil.java

/**
 * @return a new copy of the data in @param buffer
 * USUALLY YOU SHOULD USE ByteBuffer.duplicate() INSTEAD, which creates a new Buffer
 * (so you can mutate its position without affecting the original) without copying the underlying array.
 *//*w ww . j  av  a2s  .c o  m*/
public static ByteBuffer clone(ByteBuffer buffer) {
    assert buffer != null;

    if (buffer.remaining() == 0) {
        return EMPTY_BYTE_BUFFER;
    }

    ByteBuffer clone = ByteBuffer.allocate(buffer.remaining());

    if (buffer.hasArray()) {
        System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), clone.array(), 0,
                buffer.remaining());
    } else {
        clone.put(buffer.duplicate());
        clone.flip();
    }

    return clone;
}