Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine number of Bytes in ByteBuffer

After you've written to the ByteBuffer, the number of bytes you've written can be found with the position() method.

If you then flip() the buffer, the number of bytes in the buffer can be found with the limit() or remaining() methods.

If you then read some of the buffer, the number of bytes remaining can be found with the remaining() method.


DatagramChannel channel = DatagramChannel.open();
ByteBuffer bb = ByteBuffer.allocate(5+size);
channel.receive(bb);
bb.flip();
// actual length of received packet
int len = bb.remaining();