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[]toByteArray(ByteBuffer buffer)
to Byte Array
byte[] ret = new byte[buffer.remaining()];
buffer.get(ret, 0, ret.length);
return ret;
byte[]toByteArray(ByteBuffer buffer)
to Byte Array
return toByteArray(buffer, 0, buffer.capacity());
byte[]toByteArray(ByteBuffer buffer)
to Byte Array
if (buffer.hasArray())
    return buffer.array();
byte[] result = new byte[buffer.capacity()];
buffer.rewind();
buffer.get(result);
return result;
byte[]toByteArray(ByteBuffer buffer)
to Byte Array
return buffer != null && buffer.hasArray() ? buffer.array() : null;
byte[]toByteArray(ByteBuffer buffer, int length)
to Byte Array
byte[] bytes = new byte[length];
buffer.mark();
try {
    buffer.position(0);
    buffer.get(bytes, 0, bytes.length);
} finally {
    buffer.reset();
return bytes;
byte[]toByteArray(ByteBuffer buffer, int length)
Returns an array (that shouldn't be written to!) of the ByteBuffer.
if (buffer.hasArray() && buffer.arrayOffset() == 0) {
    return buffer.array();
byte[] data = new byte[length];
buffer.get(data);
return data;
byte[]toByteArray(ByteBuffer bytes)
Copies the byte buffer's content to a newly allocated byte array.
byte[] array = new byte[bytes.capacity()];
bytes.get(array);
return array;
byte[]toByteArrayByByteBuffer(int value)
to Byte Array By Byte Buffer
return ByteBuffer.allocate(4).putInt(value).array();
byte[]toBytes(ByteBuffer buff)
to Bytes
int limit = buff.limit();
byte[] arr = new byte[limit];
System.arraycopy(buff.array(), buff.arrayOffset() + buff.position(), arr, 0, limit);
return arr;
byte[]toBytes(ByteBuffer buffer)
to Bytes
if (buffer == null)
    return null;
return Arrays.copyOfRange(buffer.array(), buffer.position(), buffer.limit());