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[]convertVarIntByteBufferToByteArray(ByteBuffer byteBuffer)
Converts a variable length integer (https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer) from a ByteBuffer to byte array
byte originalVarIntSize = byteBuffer.get();
byte varIntSize = getVarIntSize(originalVarIntSize);
byte[] varInt = new byte[varIntSize];
varInt[0] = originalVarIntSize;
byteBuffer.get(varInt, 1, varIntSize - 1);
return varInt;
byte[]deserializeBytes(ByteBuffer buf)
deserialize Bytes
int size = buf.getInt();
if (size == 0) {
    return null;
byte[] b = new byte[size];
buf.get(b);
return b;
byte[]getByteArray(ByteBuffer buff)
Create a new byte[] array and populate it with the given ByteBuffer's contents.
if (buff == null)
    return null;
buff.clear();
byte[] inds = new byte[buff.limit()];
for (int x = 0; x < inds.length; x++) {
    inds[x] = buff.get();
return inds;
...
byte[]getByteArray(ByteBuffer byteBuffer)
get Byte Array
if (byteBuffer.hasArray()) {
    return copyOfRange(byteBuffer.array(), byteBuffer.position() + byteBuffer.arrayOffset(),
            byteBuffer.limit() + byteBuffer.arrayOffset());
byte[] result = new byte[byteBuffer.limit() - byteBuffer.position()];
byteBuffer.get(result);
return result;
byte[]getByteArray(ByteBuffer byteBuffer)
get Byte Array
short length = byteBuffer.getShort();
byte[] array = new byte[length];
if (length != 0) {
    byteBuffer.get(array);
return array;
byte[]getByteArray(final ByteBuffer buff)
get Byte Array
if (buff == null) {
    return null;
buff.clear();
final byte[] inds = new byte[buff.limit()];
for (int x = 0; x < inds.length; x++) {
    inds[x] = buff.get();
return inds;
byte[]getByteArrayFromBuffer(ByteBuffer byteBuf)
get Byte Array From Buffer
byteBuf.flip();
byte[] row = new byte[byteBuf.limit()];
byteBuf.get(row);
byteBuf.clear();
return row;
byte[]getByteArrayFromByteBuffer(ByteBuffer content)
This will return a byte array that only contains the data from ByteBuffer.
int contentLength = content.position();
byte[] bytesArray = new byte[contentLength];
content.flip();
content.get(bytesArray, 0, contentLength);
return bytesArray;
byte[]getBytes(ByteBuffer bb)
get Bytes
if (bb.hasArray() && bb.arrayOffset() == 0 && bb.position() == 0) {
    byte[] b = bb.array();
    if (b.length == bb.limit())
        return b;
byte[] b = new byte[bb.limit() - bb.position()];
bb.duplicate().get(b);
return b;
...
byte[]getBytes(ByteBuffer buf)
Return the data in the buffer.
if (buf.hasArray() && buf.arrayOffset() == 0 && buf.position() == 0 && buf.limit() == buf.capacity()) {
    return buf.array();
final byte[] a;
    buf = buf.asReadOnlyBuffer();
    final int len = buf.remaining();
    a = new byte[len];
...