Java Utililty Methods ByteBuffer to Byte Array

List of utility methods to do ByteBuffer to Byte Array

Description

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

Method

byte[]toBytes(ByteBuffer buffer)
to Bytes
if (buffer == null)
    return null;
if (buffer.hasArray()) {
    return Arrays.copyOfRange(buffer.array(), buffer.position() + buffer.arrayOffset(),
            buffer.limit() + buffer.arrayOffset());
} else {
    byte[] data = new byte[buffer.remaining()];
    buffer.duplicate().get(data);
...
byte[]toBytes(ByteBuffer buffer, int offset, int length)
Copy the given number of bytes from specified offset into a new byte[]
byte[] output = new byte[length];
for (int i = 0; i < length; i++) {
    output[i] = buffer.get(offset + i);
return output;
byte[]toBytes(ByteBuffer buffer, int startPosition)
Copy the bytes from position to limit into a new byte[] of the exact length and sets the position and limit back to their original values (though not thread safe).
int originalPosition = buffer.position();
byte[] output = new byte[buffer.limit() - startPosition];
buffer.position(startPosition);
buffer.get(output);
buffer.position(originalPosition);
return output;
byte[]toBytes(ByteBuffer value)
Convert a ByteBuffer to a byte[] ByteBuffer is reset to its original state.
byte[] result = null;
if (value != null && value.remaining() > 0) {
    result = new byte[value.remaining()];
    value.mark();
    value.get(result);
    value.reset();
return result;
...
byte[]toBytes(final ByteBuffer bb)
to Bytes
return toBytes(bb, bb.limit());