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();
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