Buffer flip() methods in Java with Examples

The flip() method of java.nio.ByteBuffer Class is used to flip this buffer. The limit is set to the current position and then the position is set to zero. If the mark is defined then it is discarded. After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations.

For example:

buf.put(magic);    // Prepend header
in.read(buf);      // Read data into rest of buffer
buf.flip();        // Flip buffer
out.write(buf);    // Write header + data to channel

This method is often used in conjunction with the compact() method when transferring data from one place to another.



Syntax:

public Buffer flip()

Return Value: This method returns this buffer.

Below are the examples to illustrate the flip() method:

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// flip() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // Declare and initialize the byte array
        byte[] bb = { 10, 20, 30 };
  
        // wrap the byte array into ByteBuffer
        // using wrap() method
        ByteBuffer byteBuffer = ByteBuffer.wrap(bb);
  
        // Typecast ByteBuffer to Buffer
        Buffer buffer = (Buffer)byteBuffer;
  
        // set position at index 1
        buffer.position(1);
  
        // print the buffer
        System.out.println("Buffer before flip: "
                           + Arrays.toString((byte[])buffer.array())
                           + "\nPosition: " + buffer.position()
                           + "\nLimit: " + buffer.limit());
  
        // Flip the Buffer
        // using flip() method
        buffer.flip();
  
        // print the buffer
        System.out.println("\nBuffer after flip: "
                           + Arrays.toString((byte[])buffer.array())
                           + "\nPosition: " + buffer.position()
                           + "\nLimit: " + buffer.limit());
    }
}

chevron_right


Output:

Buffer before flip: [10, 20, 30]
Position: 1
Limit: 3

Buffer after flip: [10, 20, 30]
Position: 0
Limit: 1

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// flip() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // defining and allocating ByteBuffer
        // using allocate() method
        ByteBuffer byteBuffer = ByteBuffer.allocate(4);
  
        // put byte value in byteBuffer
        // using put() method
        byteBuffer.put((byte)20);
        byteBuffer.put((byte)30);
  
        // Typecast ByteBuffer to Buffer
        Buffer buffer = (Buffer)byteBuffer;
  
        // set position at index 1
        buffer.position(1);
  
        // print the buffer
        System.out.println("Buffer before flip: "
                           + Arrays.toString((byte[])buffer.array())
                           + "\nPosition: " + buffer.position()
                           + "\nLimit: " + buffer.limit());
  
        // Flip the Buffer
        // using flip() method
        buffer.flip();
  
        // print the buffer
        System.out.println("\nBuffer after flip: "
                           + Arrays.toString((byte[])buffer.array())
                           + "\nPosition: " + buffer.position()
                           + "\nLimit: " + buffer.limit());
    }
}

chevron_right


Output:

Buffer before flip: [20, 30, 0, 0]
Position: 1
Limit: 4

Buffer after flip: [20, 30, 0, 0]
Position: 0
Limit: 1

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#flip–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.