Java Utililty Methods ByteBuffer Get

List of utility methods to do ByteBuffer Get

Description

The list of methods to do ByteBuffer Get are organized into topic(s).

Method

intcalculateNewLength(ByteBuffer source, ByteBuffer target)
calculate New Length
long newLength = source.capacity() + (target.limit() - source.remaining());
if (newLength <= threshold) {
    return (int) newLength;
return -1;
byte[]get(ByteBuffer buffer)
get
byte[] data = new byte[buffer.limit()];
buffer.get(data);
return data;
byte[]get(ByteBuffer source)
get
if (source.hasArray()) {
    return source.array();
} else {
    source.rewind();
    System.out.println(source.limit());
    byte[] array = new byte[source.limit()];
    for (int i = 0; i < array.length; i++) {
        array[i] = source.get();
...
intget3ByteInt(ByteBuffer buffer)
Read a 3 byte int from a buffer
return get3ByteInt(buffer, buffer.order());
byte[]getActiveArray(ByteBuffer buffer)
get Active Array
byte[] ret = new byte[buffer.remaining()];
if (buffer.hasArray()) {
    byte[] array = buffer.array();
    System.arraycopy(array, buffer.arrayOffset() + buffer.position(), ret, 0, ret.length);
} else {
    buffer.slice().get(ret);
return ret;
...
longgetAddress(ByteBuffer buf)
get Address
try {
    long address;
    address = ADDRESS_FIELD.getLong(buf);
    return address;
} catch (Exception x) {
    throw new RuntimeException(x);
longgetAddress(ByteBuffer buffer)
get Address
Preconditions.checkArgument(buffer.isDirect(), "ByteBuffer must be DirectBuffer");
return ((DirectBuffer) buffer).address();
longgetAddress(ByteBuffer buffer)
get Address
return ((sun.nio.ch.DirectBuffer) buffer).address();
longgetAddressFromDirectByteBuffer(ByteBuffer buffer)
Gets the address value for the memory that backs a direct byte buffer.
try {
    Field addressField = Buffer.class.getDeclaredField("address");
    addressField.setAccessible(true);
    return addressField.getLong(buffer);
} catch (Exception e) {
    throw new RuntimeException("Unable to address field from ByteBuffer", e);
byte[]getArray(ByteBuffer buffer)
You should almost never use this.
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);
byte[] bytes = new byte[length];
buffer.duplicate().get(bytes);
return bytes;