确定ByteBuffer中的字节数 [英] Determine number of Bytes in ByteBuffer
问题描述
我有一个 ByteBuffer
,最多可以容纳(4 + size
)个字节
(即,后跟 size
个字符的整数)。但是,写入 ByteBuffer
的字符数可能小于 size
。
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
.
所以我想知道,是否有确定向 ByteBuffer
写入多少字符的信息,而不只是确定它的总大小? 限制
,位置
,这些都不是我追求的目标。
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.
谢谢您的帮助!
推荐答案
写入ByteBuffer后,字节数您可以使用 position()
方法找到您写的内容。
After you've written to the ByteBuffer, the number of bytes you've written can be found with the position()
method.
如果您选择了 flip()
缓冲区,缓冲区中的字节数可以通过 limit()
或保留()
方法。
If you then flip()
the buffer, the number of bytes in the buffer can be found with the limit()
or remaining()
methods.
如果您随后读取一些缓冲区,则可以通过 remaining()
方法。
If you then read some of the buffer, the number of bytes remaining can be found with the remaining()
method.
这篇关于确定ByteBuffer中的字节数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!