difference between bytebuffer.flip() and bytebuffer.rewind()

18,902

Solution 1

From the source code, they are very similar. You can see the follow:

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

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

So the difference is the flip set the limit to the position, while rewind not. Consider you have allocated a buffer with 8 bytes, you have filled the buffer with 4 bytes, then the position is set to 3, just show as follow:

    [ 1  1  1  1  0  0  0  0]
               |           |
flip           limit       |
rewind                     limit

So rewind just used limit has set appropriately.

Solution 2

They're not equivalent at all.

A ByteBuffer is normally ready for read() (or for put()).

flip() makes it ready for write() (or for get()).

rewind() and compact() and clear() make it ready for read()/put() again after write() (or get()).

Solution 3

The rewind( ) method is similar to flip( ) but does not affect the limit. It only sets the position back to 0. You can use rewind( ) to go back and reread the data in a buffer that has already been flipped. A common situation would be : after you use flip(), you read the data from the buffer, the you want to reread the data, this method would work.

Solution 4

Here is an example where the two will produce different results. As you said, both set position to 0, the difference between the two is that flip sets the limit to the previous position.

So with flip, if you were writing ( put ) , the limit for reading ( get ) will become the position of the last element you wrote. If you try to read any more than that, it will throw an exception.

Rewind leaves the limit unchanged. Assuming it was at capacity ( buffer size ) , it will let you keep on reading beyond the data that you actually wrote, in this case reading the initial zeros that the buffer was initialized with.

ByteBuffer bb = ByteBuffer.allocateDirect(2);
byte b = 127;
bb.put(b);
bb.rewind();//or flip();

System.out.println(bb.get());  // will print 127 in either case
System.out.println(bb.get());  // will print 0 for rewind, BufferUnderflow exception for flip

Solution 5

Buffer has position, limit and capacity properties. You can allocate space for n number of elements while creating a buffer. Here n is the capacity. After buffer is allocated, position is set to 0 and limit is set to capacity.

If you filled the buffer with n-x number of elements, position will be set to n-x. Buffer will have empty elements after n-x.

If you want to drain the buffer at this point and that too want only non empty values, you need set limit to current position and position to zero. Using hasRemaining(), you can get elements till n-x. Flip sets the limit and position properties like described above.

Difference between flip and rewind is that flip sets the position to 0 and sets limit to active content. Method rewind just sets the position to 0.

For more information http://www.zoftino.com/java-nio-tutorial

Share:
18,902
Rollerball
Author by

Rollerball

Updated on June 18, 2022

Comments

  • Rollerball
    Rollerball almost 2 years

    I am aware that flip() set the current buffer position to 0 and set the limit to the previous buffer position whereas rewind() just set the current buffer position to 0.

    In the following code, either I use rewind() or flip() i get the same result.

    byte b = 127;
    bb.put(b);
    bb.rewind();//or flip();
    
    System.out.println(bb.get());
    bb.rewind();// or flip();
    System.out.println(bb.get());
    

    Could you provide me with a real example where the difference of these 2 methods really matters? Thanks in advance.