简体   繁体   English

ByteBuffer 中的限制和容量有什么区别?

[英]What is the difference between limit and capacity in ByteBuffer?

Java 的java.nio.ByteBuffer 中的限制和容量有什么区别?

Its best illustrated HERE in this article : They are mainly different depending on the mode, 在本文中对其进行了最佳说明:它们主要因模式而异,

  • In Write Mode , Capacity and Limit are Same.写入模式下,容量和限制相同。
  • But in Read mode Limit means the limit of how much data you can read from the data但是在读取模式下限制意味着您可以从数据中读取多少数据

在此处输入图片说明

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.

相关问题 Java NIO中ByteBuffer和CharBuffer有什么区别? - What is the difference between ByteBuffer and CharBuffer in Java NIO? 类ByteBuffer的rewind()和clear()有什么区别? - What is the difference between rewind() and clear() of class ByteBuffer? bytebuffer.flip() 和 bytebuffer.rewind() 的区别 - difference between bytebuffer.flip() and bytebuffer.rewind() ByteBuffer.allocateDirect()和glGenBuffers()之间的区别 - Difference between ByteBuffer.allocateDirect() and glGenBuffers() ArrayDeque &lt;&gt;(int容量)-Scanner.nextInt()== 4和(int)4有什么区别? - ArrayDeque<>(int capacity) - what's the difference between Scanner.nextInt() == 4 and (int) 4? StringBuilder 中 length() 和 capacity() 方法的区别 - Difference between length() and capacity() methods in StringBuilder ArrayList和HashMap的容量增长之间的差异 - Difference between capacity growth of ArrayList and HashMap 如果我们超过了在Java中的ByteBuffer.allocate(48)NIO包类中分配缓冲区的容量怎么办? - what if we exceed the capacity of allocating buffer in ByteBuffer.allocate(48) NIO package class in java ByteBuffer - 编码字符串和 put 与 CharSet 编码之间的区别? - ByteBuffer - difference between encoding string and put vs CharSet encode? FileChannel和FileInputStream中read(ByteBuffer)和read(byte [])之间的区别 - difference between read(ByteBuffer) and read(byte[]) in FileChannel and FileInputStream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM