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);
There are several differences between a byte array and ByteBuffer class in Java, but the most important of them is that bytes from byte array always reside in Java heap space, but bytes in a ByteBuffer may potentially reside outside of the Java heap in case of direct byte buffer and memory mapped files.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With