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[]asByteArray(ByteBuffer buf)
Gets the content of the ByteBuffer as a byte[] without mutating the buffer (so thread safe) and minimizing GC (i.e.
byte[] result;
if (buf.hasArray() && buf.arrayOffset() == 0 && buf.capacity() == buf.remaining()) {
    result = buf.array();
} else {
    result = new byte[buf.remaining()];
    if (buf.hasArray()) {
        System.arraycopy(buf.array(), buf.arrayOffset() + buf.position(), result, 0, result.length);
    } else {
...
byte[]asByteArray(ByteBuffer buf)
as Byte Array
byte[] bytes = new byte[buf.remaining()];
buf.get(bytes);
return bytes;
byte[]byteBufferToByteArray(ByteBuffer buf)
byte Buffer To Byte Array
ByteBuffer str = buf.duplicate();
str.rewind();
byte[] dst = new byte[str.limit()];
str.get(dst);
return dst;
byte[]ByteBufferToByteArray(ByteBuffer byteBuffer)
ByteBuffer to byte[]
byteBuffer.rewind();
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
byteBuffer.rewind();
return bytes;
byte[]byteBufferToByteArray(ByteBuffer byteBuffer)
byte Buffer To Byte Array
if (wrapsFullArray(byteBuffer)) {
    return byteBuffer.array();
byte[] target = new byte[byteBuffer.remaining()];
byteBufferToByteArray(byteBuffer, target, 0);
return target;
ArrayListbyteBufferToByteArray(List byteBufferList)
byte Buffer To Byte Array
List<byte[]> result = new ArrayList<byte[]>();
for (ByteBuffer buf : byteBufferList) {
    result.add(buf.array());
return (ArrayList<byte[]>) result;
byte[]byteBufferToBytes(ByteBuffer buffer)
byte Buffer To Bytes
final byte[] array = buffer.array();
final int arrayOffset = buffer.arrayOffset();
return Arrays.copyOfRange(array, arrayOffset + buffer.position(), arrayOffset + buffer.limit());
byte[]bytes(ByteBuffer bb)
bytes
byte[] b = new byte[bb.remaining()];
bb.duplicate().get(b);
return b;
byte[]bytes(ByteBuffer buf)
bytes
byte[] d = new byte[buf.limit()];
buf.get(d);
return d;
byte[]convertToBytes(@Nonnull ByteBuffer buffer)
convert To Bytes
final byte[] array = buffer.array();
final int arrayOffset = buffer.arrayOffset();
return Arrays.copyOfRange(array, arrayOffset, arrayOffset + buffer.position());