Buffer clear() methods in Java with Examples

18 Jul, 2019
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 Buffer clear()
Return Value: This method returns this buffer.
Below are the examples to illustrate the clear() method:
Examples 1:
// 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);              // Typecasting ByteBuffer into Buffer            Buffer bb1 = (Buffer)bb;              // try to set the position at index 2            bb1.position(2);              // Set this buffer mark position            // using mark() method            bb1.mark();              // try to set the position at index 4            bb1.position(4);              // display position            System.out.println("position before reset: "                               + bb1.position());              // try to call clear() to restore            // to the position at index 0            // by discarding the mark            bb1.clear();              // display position            System.out.println("position after reset: "                               + bb1.position());        }          catch (InvalidMarkException e) {            System.out.println("new position is less than "                               + "the position we marked before ");            System.out.println("Exception throws: " + e);        }    }}
position before reset: 4
position after reset: 0

Examples 2:
// 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);              // Typecasting ByteBuffer into Buffer            Buffer bb1 = (Buffer)bb;              // try to set the position at index 2            bb1.position(3);              // display position            System.out.println("position before clear: "                               + bb1.position());              // try to call clear() to restore            // to the position at index 0            // by discarding the mark            bb1.clear();              // display position            System.out.println("position after clear: "                               + bb1.position());        }          catch (InvalidMarkException e) {            System.out.println("new position is less than "                               + "the position we marked before ");            System.out.println("Exception throws: " + e);        }    }}
position before clear: 3
position after clear: 0

Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#clear–
Java-Buffer
Java-Functions
Java-NIO package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Collections in Java
Multidimensional Arrays in Java
Stream In Java
Set in Java
Singleton Class in Java