Java Utililty Methods ByteBuffer from Byte Array

List of utility methods to do ByteBuffer from Byte Array

Description

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

Method

ByteBufferreadBytes(ByteBuffer bb, int length)
read Bytes
ByteBuffer copy = bb.duplicate();
copy.limit(copy.position() + length);
bb.position(bb.position() + length);
return copy;
byte[]readBytes(ByteBuffer buf, int length)
read Bytes
if ((buf.position() + length) <= buf.limit()) {
    byte[] r = new byte[length];
    for (int i = 0; i < length; i++) {
        r[i] = buf.get();
    return r;
return null;
...
byte[]readBytes(ByteBuffer buffer)
Read the given byte buffer into a byte array
return readBytes(buffer, 0, buffer.limit());
byte[]readBytes(ByteBuffer buffer, int length)
read Bytes
byte[] ab = new byte[length];
buffer.get(ab);
return ab;
byte[]readBytes(ByteBuffer buffer, int position, int length)
read Bytes
assert buffer != null;
prepare(buffer, position, length);
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
return bytes;
byte[]readBytes(final ByteBuffer bb, final int length)
read Bytes
final byte[] result = new byte[length];
for (int index = 0; index < length; index++) {
    result[index] = bb.get();
return result;
ByteBuffertoByteBuffer(byte[] array)
to Byte Buffer
ByteBuffer buffer = ByteBuffer.allocateDirect(array.length).order(ByteOrder.nativeOrder());
buffer.put(array).flip();
return buffer;
ByteBuffertoByteBuffer(byte[] bytes)
to Byte Buffer
return ByteBuffer.wrap(bytes).order(STORED_BYTE_ORDER);
ByteBuffertoByteBuffer(byte[] data)
Wraps data into a ByteBuffer prefixed by its length.
ByteBuffer bb = ByteBuffer.allocate(4 + data.length);
bb.putInt(data.length);
bb.put(data);
bb.rewind();
return bb;