简体   繁体   English

类ByteBuffer的rewind()和clear()有什么区别?

[英]What is the difference between rewind() and clear() of class ByteBuffer?

Note that in JDK document's words rewind() and clear() looks alike for "cleaning" the ByteBuffer . 请注意,在JDK文档中, rewind()clear()看起来类似于“清理” ByteBuffer Actually the old data exists, furthermore rewind() should be used before channel-write or get operation and clear() corresponds to channel-read or put operation. 实际上存在旧数据,而且在频道写入或获取操作之前应该使用rewind()clear()对应于频道读取或放置操作。

I feel confused about the given description. 我对给定的描述感到困惑。 What is the difference between these counterparts on earth? 地球上这些对应物有什么区别?

区别在于clear重置容量限制,因此准备缓冲区以便写入(并由您读取)其他数据,而rewind不会对限制执行任何操作,因此可能会再次读取数据。

1) Here is clear() method: 1)这是clear()方法:

public final Buffer clear() {
    position = 0;
    limit = capacity;
    mark = -1;
    return this;
}

The clear( ) method resets a buffer to an empty state. clear()方法将缓冲区重置为空状态。 It doesn't change any of the data elements of the buffer but simply sets the limit to the capacity and the position back to 0 它不会更改缓冲区的任何数据元素,只是将容量限制和位置设置为0

2) Here is rewind() method: 2)这是rewind()方法:

 public final Buffer rewind() {
    position = 0;
    mark = -1;
    return this;
}

You can use rewind( ) to go back and reread the data in a buffer that has already been flipped. 您可以使用rewind()返回并重新读取已翻转的缓冲区中的数据。 As you can see: it doesn't change the limit. 如您所见:它不会改变限制。 Sometimes, after read data from a buffer, you may want to read it again, rewind() is the right choice for you. 有时,在从缓冲区读取数据后,您可能希望再次读取它,rewind()是您的正确选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM