34

I have a ByteBuffer that can hold a maximum of (4 + size) bytes (that is, an integer followed by size characters). However, the number of characters written to the ByteBuffer , may be smaller than size.

So I was wondering, is there anyway to determine how many characters were written to the ByteBuffer and not just the total size of it? limit, position and such don't SEEM to be what I am after.

Thanks for your help!

2 Answers2

41

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.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Thanks Andy. Perhaps the issue is that I used `wrap` to copy a `byte[]` to the buffer? Is that messing with my use of the functions you mentioned? If so, is there a way I could read the ACTUAL number of bytes written to the `byte[]`? Thanks for your help. –  Jun 02 '13 at 02:19
  • @Girl You cannot know the actual number of bytes written, since `ByteBuffer` is essentially "seekable". What is your write pattern? Do you use only absolute read/write methods, relative read/write methods? – fge Jun 02 '13 at 03:02
  • @fge I am essentially reading from a `DatagrampPacket`. I know that the MAX size of the contents within the `DatagramPacket` will be `4 + size`. However, it is possible that the number of characters after the `int` could have been less than `size`. So I am just trying to determine how many characters were actually in the `DatagramPacket`. Thanks for your assistance. –  Jun 02 '13 at 03:19
  • @Giri `DatagramPacket` has `.getOffset()`, `.getLength()`, and the javadoc seems to indicate that the length of the actual received data is `.getLength() - .getOffset() + 1`; you can just wrap this using `.getData()` and this information, or is there something else? – fge Jun 02 '13 at 03:28
  • @fge I'm not sure if this is what you meant: `byte[] buffer = new byte[4 + size];` `DatagramPacket request = new DatagramPacket(buffer, 4 + size);` `receiver_Socket.receive(request);` `int length = request.getLength() - request.getOffset() + 1;` The result of this is `length` = 20 as `size` was 15. That is, `request.getOffset()` is equating to 0. In this packet I deliberately sent less than `size` characters. Sorry this is a bit messy... –  Jun 02 '13 at 04:12
  • An alternative would be to maybe just include the number of characters in the packet when I send it over by adding an extra `int`..? –  Jun 02 '13 at 04:22
  • @Giri - Sorry for the delay. As fge notes, ByteBuffer is seekable, with the position(int) method. However, you likely control whether and when this occurs, and can factor this into consideration. If the position doesn't start at 0, you could get the position before writing to the buffer, and get the position after writing, and subtract the former from the latter. – Andy Thomas Jun 02 '13 at 04:44
3
DatagramChannel channel = DatagramChannel.open();
ByteBuffer bb = ByteBuffer.allocate(5+size);
channel.receive(bb);
bb.flip();
// actual length of received packet
int len = bb.remaining();
Lord Moon
  • 31
  • 1