ByteBuffer byte count

What’s the actual size of the data that resides inside of a bytebuffer?

When I create one with a UnsafeBufferPointer with a previously known size, the bytebuffer has the write index matching the size but since it can be moved I don’t know if I should trust it as its storage size.

There are other properties like capacity and stored capacity but they don’t seem to be what I’m looking for, unless I’m not understanding the nature of them.

Thanks in advance

ByteBuffer can be used to write bytes to and read bytes from.

writerIndex contains the number of bytes ever written to the ByteBuffer and increase when you use any of the methods prefixed with write. It's initialised to how many bytes you pass to one of the ByteBuffer initialisers, in your case the size of your UnsafeBufferPointer.

readerIndex contains the number of bytes ever read and increases when you use any of the methods prefixed with read. It's initialised to zero by default.

readableBytes is probably what your are looking for and defined as writerIndex - readerIndex or in other words as the number of readable bytes you can consume with the read* methods.

Does this answer your question?

2 Likes

It does, I was unsure to trust the indices as the count of written data inside of it but if that's how it's meant, then it's perfect. Thank you :slight_smile:

1 Like