120

Is this the recommended way to get the bytes from the ByteBuffer

ByteBuffer bb =..

byte[] b = new byte[bb.remaining()]
bb.get(b, 0, b.length);

6 Answers 6

126

Depends what you want to do.

If what you want is to retrieve the bytes that are remaining (between position and limit), then what you have will work. You could also just do:

ByteBuffer bb =..

byte[] b = new byte[bb.remaining()];
bb.get(b);

which is equivalent as per the ByteBuffer javadocs.

Sign up to request clarification or add additional context in comments.

5 Comments

Correct. And note that bb.capacity() might equal bb.remaining() even when the backing array is longer, so you must not use their equality as a test of when bb.array() is correct. See ByteBuffer.slice().
Note that, to avoid changing the position of the buffer, I used bb.slice().remaining(). That way it looks like a clean dump without touching the original buffer.
this method gives me signed bytes however i want unsigned...any idea?
Java doesn't have the notion of unsigned integers, only signed ones. If you want "unsigned bytes", you need to cast as int and use a bitmask: int unsigned_byte = b[k] & 0xff; for some value of k.
If you want to get the entire buffer into a byte array, would you call ByteBuffer#clear first?
24

Note that the bb.array() doesn't honor the byte-buffers position, and might be even worse if the bytebuffer you are working on is a slice of some other buffer.

I.e.

byte[] test = "Hello World".getBytes("Latin1");
ByteBuffer b1 = ByteBuffer.wrap(test);
byte[] hello = new byte[6];
b1.get(hello); // "Hello "
ByteBuffer b2 = b1.slice(); // position = 0, string = "World"
byte[] tooLong = b2.array(); // Will NOT be "World", but will be "Hello World".
byte[] world = new byte[5];
b2.get(world); // world = "World"

Which might not be what you intend to do.

If you really do not want to copy the byte-array, a work-around could be to use the byte-buffer's arrayOffset() + remaining(), but this only works if the application supports index+length of the byte-buffers it needs.

1 Comment

"bb.array() doesn't honor the byte-buffers position", can you provide us more details about this part. I understood the slice example but need more details about why bb.array() mess up
9

As simple as that

  private static byte[] getByteArrayFromByteBuffer(ByteBuffer byteBuffer) {
    byte[] bytesArray = new byte[byteBuffer.remaining()];
    byteBuffer.get(bytesArray, 0, bytesArray.length);
    return bytesArray;
}

1 Comment

This is an excellent answer. I confirm the same logic is used here: java.nio.channels.Channels.WritableByteChannelImpl.write(ByteBuffer). Does anyone know if this answer has any special edge cases that will not work?
5
final ByteBuffer buffer;
if (buffer.hasArray()) {
    final byte[] array = buffer.array();
    final int arrayOffset = buffer.arrayOffset();
    return Arrays.copyOfRange(array, arrayOffset + buffer.position(),
                              arrayOffset + buffer.limit());
}
// do something else

Comments

4

If one does not know anything about the internal state of the given (Direct)ByteBuffer and wants to retrieve the whole content of the buffer, this can be used:

ByteBuffer byteBuffer = ...;
byte[] data = new byte[byteBuffer.capacity()];
((ByteBuffer) byteBuffer.duplicate().clear()).get(data);

5 Comments

ByteBuffer.get(byte[]) returns a ByteBuffer
And...? Not sure what you mean, sorry.
The question is how to go from a ByteBuffer to a byte[].
Once called, they are in the data variable. The getter returns this, see its Javadoc.
Thanks, I didn't get that. Since the get method is returning a value, I wasn't expecting it to have a side effect too.
1

This is a simple way to get a byte[], but part of the point of using a ByteBuffer is avoiding having to create a byte[]. Perhaps you can get whatever you wanted to get from the byte[] directly from the ByteBuffer.

1 Comment

But often you'll need to call something (not in your own code) that takes a byte[], so converting isn't optional.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.