Buffer clear() methods in Java with Examples Last Updated : 18 Jul, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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); } } } Output: position before reset: 4 position after reset: 0 Examples 2: Java // 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); } } } Output: position before clear: 3 position after clear: 0 Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#clear-- Comment More infoAdvertise with us Next Article Buffer flip() methods in Java with Examples R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-Buffer Practice Tags : Java Similar Reads CharBuffer clear() methods in Java with Examples The clear() method of java.nio.CharBuffer 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: // Prepare buffer for 2 min read 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(); // Prepa 2 min read Buffer capacity() method in Java with Examples The capacity() method of java.nio.Buffer Class is used to return this buffer's capacity. Syntax: public final int capacity() Return Value: The capacity of this buffer Below are the examples to illustrate the capacity() method: Examples 1: Java // Java program to demonstrate // capacity() method impo 2 min read 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 3 min read Buffer duplicate() method in Java with Examples The duplicate() method of java.nio.Buffer class is used to create a new buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark 3 min read Buffer mark() methods in Java with Examples The mark() method of java.nio.Buffer Class is used to set this buffer's mark at its position.Syntax: public Buffer mark() Return Value: This method returns this buffer.Below are the examples to illustrate the mark() method:Examples 1: Java // Java program to demonstrate // mark() method import java. 2 min read Like