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[]getBytes(ByteBuffer buf)
This method will get a sequence of bytes from pos -> limit, but will restore pos after.
int savedPos = buf.position();
byte[] newBytes = new byte[buf.remaining()];
buf.get(newBytes);
buf.position(savedPos);
return newBytes;
booleangetBytes(ByteBuffer buf, byte[] arr)
Retrieves arr.length bytes from ByteBuffer
if (buf.remaining() < arr.length)
    return false;
buf.get(arr);
return true;
byte[]getBytes(ByteBuffer buf, int length)
This is a relative bulk get method for ByteBuffer that returns the array of bytes gotten.
final byte[] dst = new byte[length];
buf.get(dst, 0, length);
return dst;
byte[]getBytes(ByteBuffer buffer, int len)
Reads an array of bytes from the given buffer
byte[] bytes = new byte[len];
buffer.get(bytes);
return bytes;
byte[]getBytes(ByteBuffer byteBuffer, int length)
get Bytes
byte[] bytes = new byte[length];
byteBuffer.get(bytes);
return bytes;
byte[]getBytes(ByteBuffer bytes)
Extract the bytes in the given ByteBuffer and return it as a byte[].
byte[] result = new byte[bytes.remaining()]; 
bytes.get(result);
return result;
byte[]getBytes(ByteBuffer floatCalculator, float number)
Calculates the 4 sequential bytes given the corresponding float value
byte[] bytes = new byte[4];
floatCalculator.clear();
floatCalculator.putFloat(number);
floatCalculator.position(0);
floatCalculator.get(bytes);
return bytes;
byte[]getBytes(ByteBuffer in)
get Bytes
if (in.hasArray() && in.position() == 0 && in.arrayOffset() == 0 && in.array().length == in.limit()) {
    return in.array();
} else {
    byte[] buf = new byte[in.remaining()];
    in.get(buf);
    return buf;
voidgetBytes(MappedByteBuffer buffer, int pos, byte[] target)
get Bytes
for (int i = 0; i < target.length; ++i) {
    target[i] = buffer.get(pos + i);
byte[]getBytesAtOffset(ByteBuffer buffer, int offset, int length)
Given a java.nio.ByteBuffer get bytes in an absolute offset without altering its current position.
byte[] data = new byte[length];
synchronized (buffer) {
    int position = buffer.position();
    buffer.position(offset);
    buffer.get(data);
    buffer.position(position);
return data;
...