ByteBuffer clear() methods in Java with Examples

The clear() method of java.nio.ByteBuffer Class is used to clear this buffer. The position is set to zero, the limit is set to the capacity, and the mark is discarded. Invoke this method before using a sequence of channel-read or put operations to fill this buffer.

For example:

 
buf.clear();     // Prepare buffer for reading
in.read(buf);    // Read data

This method does not actually erase the data in the buffer, but it is named as if it did because it will most often be used in situations in which that might as well be the case.



Syntax:

public ByteBuffer clear()

Return Value: This method returns this buffer.

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

Examples 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// clear() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        try {
  
            byte[] barr = { 10, 20, 30, 40 };
  
            // creating object of ByteBuffer
            // and allocating size capacity
            ByteBuffer bb = ByteBuffer.wrap(barr);
  
            // try to set the position at index 2
            bb.position(2);
  
            // Set this buffer mark position
            // using mark() method
            bb.mark();
  
            // try to set the position at index 4
            bb.position(4);
  
            // display position
            System.out.println("position before reset: "
                               + bb.position());
  
            // try to call clear() to restore
            // to the position at index 0
            // by discarding the mark
            bb.clear();
  
            // display position
            System.out.println("position after reset: "
                               + bb.position());
        }
  
        catch (InvalidMarkException e) {
            System.out.println("new position is less than "
                               + "the position we marked before ");
            System.out.println("Exception throws: " + e);
        }
    }
}

chevron_right


Output:

position before reset: 4
position after reset: 0

Examples 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// clear() method
  
import java.nio.*;
import java.util.*;
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        byte[] barr = { 10, 20, 30, 40 };
  
        // creating object of ByteBuffer
        // and allocating size capacity
        ByteBuffer bb = ByteBuffer.wrap(barr);
  
        // try to set the position at index 2
        bb.position(3);
  
        // display position
        System.out.println("position before clear: "
                           + bb.position());
  
        // try to call clear() to restore
        // to the position at index 0
        // by discarding the mark
        bb.clear();
  
        // display position
        System.out.println("position after clear: "
                           + bb.position());
    }
}

chevron_right


Output:

position before clear: 3
position after clear: 0

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



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.