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[]getBytesFromBuffer(ByteBuffer buffer, int len)
Get bytes out of a buffer, moves the buffer position to where the bytes finished
final int start = buffer.position();
final byte[] bytes = new byte[len];
System.arraycopy(buffer.array(), start, bytes, 0, len);
buffer.position(start + len);
return bytes;
byte[]getBytesFromByteBuffer(ByteBuffer byteBuffer)
get Bytes From Byte Buffer
byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes, 0, byteBuffer.remaining());
return bytes;
byte[]getBytesFromByteBuffer(Object obj)
get Bytes From Byte Buffer
ByteBuffer buffer = (ByteBuffer) obj;
byte[] bytes = new byte[buffer.remaining()];
buffer.duplicate().get(bytes);
return bytes;
voidreadBytes(byte[] dest, int offset, int length, ByteBuffer buffer)
read Bytes
buffer.get(dest, offset, length);
voidreadBytes(SocketChannel socketChannel, ByteBuffer byteBuffer, int length)
read Bytes
byteBuffer.clear();
byteBuffer.limit(length);
while (byteBuffer.remaining() > 0 && socketChannel.read(byteBuffer) > 0)
    ;
if (byteBuffer.remaining() > 0) {
    throw new EOFException();
byteBuffer.flip();
...
byte[]readBytesFromByteBuffer(ByteBuffer byteBuffer)
Reads the bytes in a ByteBuffer into a byte[]
byte[] buffer = new byte[byteBuffer.remaining()];
byteBuffer.get(buffer);
return buffer;
byte[]readBytesNoLength(ByteBuffer logBuf, int size)
Read a byte array from the log.
if (DEBUG) {
    System.out.println("pos = " + logBuf.position() + " byteArray is " + size + " on read");
if (size == 0) {
    return ZERO_LENGTH_BYTE_ARRAY;
byte[] b = new byte[size];
logBuf.get(b); 
...
ByteBufferreadBytesWithShortLength(ByteBuffer bb)
read Bytes With Short Length
int length = readShortLength(bb);
return readBytes(bb, length);
byte[]toArray(ByteBuffer buffer)
to Array
byte[] dst = new byte[buffer.remaining()];
buffer.mark();
buffer.get(dst);
buffer.reset();
return dst;
byte[]toArray(ByteBuffer buffer)
Read the given byte buffer into a byte array
return toArray(buffer, 0, buffer.limit());