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

JavaFlipBytebuffer

Java Problem Overview


Why does ByteBuffer's flip() method called "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.

Can I "unflip" somehow to reuse bytes went out of a limit?

Can I concatenate tail to be flipped with some other data?

Java Solutions


Solution 1 - Java

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. flip is used to flip the ByteBuffer from "reading from I/O" (putting) to "writing to I/O" (getting): after a sequence of puts is used to fill the ByteBuffer, flip will set the limit of the buffer to the current position and reset the position to zero. 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.

After finishing the put, you might want to reuse the ByteBuffer to construct another data structure. To "unflip" it, call clear. This resets the limit to the capacity (making all of the buffer usable), and the position to 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);
}

Solution 2 - Java

Flip assigns the current position value to the limit property and sets the position property to 0. Flip is useful to drain only active elements from a buffer.

For example, below program prints "hello" instead of empty elements of the buffer. Method calls limit and position can be replaced with flip.

CharBuffer cbuff = CharBuffer.allocate(40);
cbuff.put("hello");	
// These two lines below are 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.

Solution 3 - Java

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.

Buffer keeps track of the data written into it. Post writing, flip() method is called to switch from writing to reading mode.

Solution 4 - Java

A buffer has a fixed capacity; it maintains 2 pointers: start and end. get() returns the byte at the start position and increments start. put() puts the byte at the end position and increments end. No flip()!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSuzan CiocView Question on Stackoverflow
Solution 1 - JavanneonneoView Answer on Stackoverflow
Solution 2 - JavaArnav RaoView Answer on Stackoverflow
Solution 3 - JavavkohliView Answer on Stackoverflow
Solution 4 - JavairreputableView Answer on Stackoverflow