Is this the recommended way to get the bytes from the ByteBuffer
ByteBuffer bb =..byte[] b =newbyte[bb.remaining()]
bb.get(b,0, b.length);
Is this the recommended way to get the bytes from the ByteBuffer
| |||
add comment |
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:
which is equivalent as per the ByteBuffer javadocs. | |||
add comment |
bb.array() will return the byte array that backs the buffer. | |||||||||||||
|
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. | |||||
|
Gets byte array from a ByteBuffer in java,布布扣,bubuko.com
Gets byte array from a ByteBuffer in java
原文:http://www.cnblogs.com/beautiful-scenery/p/3590021.html
bb.capacity()
might equalbb.remaining()
even when the backing array is longer, so you must not use their equality as a test of whenbb.array()
is correct. SeeByteBuffer.slice()
. – cdunn2001 Sep 26 ‘12 at 0:01