[英]What is the difference between limit and capacity in ByteBuffer?
Its best illustrated HERE in this article : They are mainly different depending on the mode, 在本文中对其进行了最佳说明:它们主要因模式而异,
ByteBuffer
does not have a length()
method. ByteBuffer
没有length()
方法。 Instead it has a several length-like concepts:相反,它有几个类似长度的概念:
mark <= position <= limit <= capacity
capacity
= Inside the ByteBuffer
, there is a backing byte[]
or something that behaves much like one. capacity
= 在ByteBuffer
内部,有一个支持byte[]
或行为类似于一个的东西。 The capacity is its size.容量就是它的大小。 The capacity indexes the first slot past the end of the buffer.
容量索引超出缓冲区末尾的第一个槽。
limit
= When filling the buffer, the limit is the same as the capacity. limit
= 填充缓冲区时,限制与容量相同。 When emptying the buffer, it is one past the last filled byte in the buffer.清空缓冲区时,它是缓冲区中最后一个填充字节的后一个。
position
= When filling the buffer, the position points just past the last byte filled in the buffer. position
= 填充缓冲区时,位置指向缓冲区中填充的最后一个字节。 When emptying the buffer, the position points just past the last byte written from the buffer.清空缓冲区时,位置刚好超过从缓冲区写入的最后一个字节。
mark
The mark is an optional bookmark to let you record an interesting spot in the ByteBuffer
that you want to return to later. mark
标记是一个可选的书签,让您可以在ByteBuffer
中记录一个您想稍后返回的有趣位置。 When you take a mark()
it records current position, and when you call reset()
it restores that position.当您使用
mark()
它会记录当前位置,当您调用reset()
它会恢复该位置。
I hope this helps.我希望这有帮助。 Also an example can be seen over here: http://mindprod.com/jgloss/bytebuffer.html
也可以在这里看到一个例子: http : //mindprod.com/jgloss/bytebuffer.html
Source: Oracle Java Buffer reference - See 'Invariants' section.来源: Oracle Java Buffer 参考- 请参阅“不变量”部分。
capacity is buffer's max size which is determined on buffer creation and never changes, limit is the actual size which can be changed.容量是缓冲区的最大大小,它在缓冲区创建时确定并且永远不会改变,限制是可以更改的实际大小。 You cannot read or write beyond limit.
您不能读取或写入超出限制。
ByteBuffer b= ByteBuffer.allocate(10); // capacity = 10, limit = 10
b.limit(1); //set limit to 1
b.put((byte)1);
b.put((byte)1); //causes java.nio.BufferOverflowException
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.