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[]toArray(ByteBuffer buffer)
to Array
byte[] array = new byte[buffer.limit() - buffer.position()];
if (buffer.hasArray()) {
    int offset = buffer.arrayOffset();
    byte[] bufferArray = buffer.array();
    System.arraycopy(bufferArray, offset, array, 0, array.length);
    return array;
} else {
    buffer.get(array);
...
byte[]toArray(ByteBuffer buffer)
Get the byte array out of a ByteBuffer.
if (buffer.hasArray()) {
    byte[] array = buffer.array();
    int from = buffer.arrayOffset() + buffer.position();
    return Arrays.copyOfRange(array, from, from + buffer.remaining());
} else {
    byte[] to = new byte[buffer.remaining()];
    buffer.slice().get(to);
    return to;
...
byte[]toArray(ByteBuffer buffer)
to Array
byte[] result = new byte[buffer.remaining()];
buffer.duplicate().get(result);
return result;
byte[]toArray(ByteBuffer buffer)
to Array
byte[] arr = new byte[buffer.limit()];
buffer.get(arr);
return arr;
byte[]toArray(ByteBuffer buffer)
Returns the content of buffer as byte array.
byte[] retVal = null;
try {
    retVal = buffer.array();
} catch (Throwable t) {
if (retVal == null || retVal.length != buffer.limit()) {
    retVal = new byte[buffer.limit()];
    int pos = buffer.position();
...
byte[]toArray(ByteBuffer bytebuffer)
to Array
if (false == bytebuffer.hasArray()) {
    int oldPosition = bytebuffer.position();
    bytebuffer.position(0);
    int size = bytebuffer.limit();
    byte[] buffers = new byte[size];
    bytebuffer.get(buffers);
    bytebuffer.position(oldPosition);
    return buffers;
...
byte[]toArray(final ByteBuffer b)
Return a byte[] having the data in the ByteBuffer from the ByteBuffer#position() to the ByteBuffer#limit() .
return toArray(b, false, null);
byte[]toArray(final ByteBuffer buffer)
Converts a byte buffer into a byte array.
if (buffer.limit() == buffer.capacity()) {
    return buffer.array();
final byte[] array = new byte[buffer.limit()];
buffer.position(0);
buffer.get(array);
return array;
byte[]toByteArray(ByteBuffer bb)
to Byte Array
if (bb.hasArray() && bb.arrayOffset() == 0 && bb.position() == 0) {
    return bb.array();
} else {
    byte[] arr = new byte[bb.remaining()];
    int p = bb.position();
    bb.get(arr);
    bb.position(p);
    return arr;
...
byte[]toByteArray(ByteBuffer buf)
to Byte Array
buf.rewind();
byte[] bytes = new byte[buf.limit()];
buf.get(bytes);
return bytes;