The class java.nio.ByteBufferhas a method ByteBuffer.array(), however, it returns an array whose size is equal to the size of the buffer, and not the capacity used. Because of this, I had a lot of problems.
I noticed that using ByteBuffer.remaining()gives me the number of bytes that are currently being used by the buffer, so basically what I'm looking for is a way to get byte[]ONLY used bytes, (i.e. bytes displayed in ByteBuffer.remaining().
I tried several different things, but I seem to fail, the only workaround I can come up with is to create another one ByteBufferwith the remaining buffer size allocated, and then write (x) bytes,
source
share