简体   繁体   English

ByteBuffer的flip方法的目的是什么? (为什么它被称为“翻转”?)

[英]What is the purpose of ByteBuffer's flip method? (And why is it called "flip"?)

Why does ByteBuffer's flip() method called "flip"?为什么 ByteBuffer 的 flip() 方法称为“flip”? What is "flipped" here?这里的“翻转”是什么? According to apidoc, two successive flips won't restore original state, and multiple flips will probably tend limit() to become zero.根据 apidoc,连续两次翻转不会恢复原始状态,多次翻转可能会使limit()变为零。

Can I "unflip" somehow to reuse bytes went out of a limit?我可以以某种方式“取消翻转”以重用超出限制的字节吗?

Can I concatenate tail to be flipped with some other data?我可以连接尾部以与其他一些数据翻转吗?

One fairly common use case for the ByteBuffer is to construct some data structure piece-by-piece and then write that whole structure to disk. ByteBuffer一个相当常见的用例是逐个构建一些数据结构,然后将整个结构写入磁盘。 flip is used to flip the ByteBuffer from "reading from I/O" ( put ting) to "writing to I/O" ( get ting): after a sequence of put s is used to fill the ByteBuffer , flip will set the limit of the buffer to the current position and reset the position to zero. flip用于将ByteBuffer从“读取 I/O”( put ting)翻转到“写入 I/O”( get ting):使用put序列填充ByteBufferflip将设置限制的缓冲区到当前位置并将位置重置为零。 This has the effect of making a future get or write from the buffer write all of what was put into the buffer and no more.这具有使未来从缓冲区getwrite写入所有put缓冲区的内容的效果,而不是更多。

After finishing the put , you might want to reuse the ByteBuffer to construct another data structure.完成put ,您可能希望重用ByteBuffer来构造另一个数据结构。 To "unflip" it, call clear .要“解开”它,请调用clear This resets the limit to the capacity (making all of the buffer usable), and the position to 0.这会将限制重置为容量(使所有缓冲区可用),并将位置重置为 0。

So, a typical usage scenario:所以,一个典型的使用场景:

ByteBuffer b = new ByteBuffer(1024);
for(int i=0; i<N; i++) {
    b.clear();
    b.put(header[i]);
    b.put(data[i]);
    b.flip();
    out.write(b);
}

Flip assigns current position value to the limit property and sets the position property to 0. Flip is useful to only drain active elements from a buffer. Flip 将当前位置值分配给 limit 属性并将 position 属性设置为 0。Flip 可用于仅从缓冲区中排出活动元素。

For example, below program prints "hello" not empty elements of buffer.例如,下面的程序打印“hello”而不是缓冲区的空元素。 Method calls limit and position can be replaced with flip.方法调用 limit 和 position 可以替换为 flip 。

CharBuffer cbuff = CharBuffer.allocate(40);
cbuff.put("hello"); 
// what below two line of code is what flip does
cbuff.limit(cbuff.position());
cbuff.position(0);      
while(cbuff.hasRemaining()) {
    System.out.println(cbuff.get());
}

See http://www.zoftino.com/java-nio-tutorial for more information on buffers and channels.有关缓冲区和通道的更多信息,请参见http://www.zoftino.com/java-nio-tutorial

ByteBuffer is ill designed. ByteBuffer 设计不当。 There are lots of complaints from decent programmers.有很多来自体面的程序员的抱怨。

So don't try to reason about it, just study and use the API carefully.所以不要试图去推理它,只要仔细研究和使用 API。

Now I cannot badmouth it without presenting an alternative, so here it is:现在我不能不提出替代方案就说坏话,所以这里是:

A buffer has a fixed capacity ;缓冲区具有固定capacity it maintains 2 pointers: start and end .它维护 2 个指针: startend get() returns the byte at the start position and increments start . get()返回start位置的字节并递增start put() puts the byte at the end position and increments end . put()将字节放在end位置并递增end No flip() !没有flip()

flip() method makes a buffer ready for a new sequence of channel-write or relative get operations: It sets the limit to the current position and then sets the position to zero. flip()方法使缓冲区为新的通道写入或相对获取操作序列做好准备:它将限制设置为当前位置,然后将位置设置为零。

Buffer keeps track of the data written into it.缓冲区跟踪写入其中的数据。 Post writing, flip() method is called to switch from writing to reading mode.写入后,调用flip()方法从写入模式切换到读取模式。

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

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