ByteBuffer clear() methods in Java with Examples Last Updated : 27 Jun, 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 ByteBuffer 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); // 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); } } } 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) { 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()); } } Output: position before clear: 3 position after clear: 0 Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html#clear-- Comment More infoAdvertise with us Next Article ByteBuffer get() method in Java with Examples R rohitprasad3 Follow Improve Article Tags : Java Java-Functions Java-NIO package Java-ByteBuffer Practice Tags : Java Similar Reads Buffer 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 3 min read 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 equals() method in Java with Examples The equals() method of java.nio.ByteBuffer class is used to check whether or not the given buffer is equal to another object. Two byte buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, con 3 min read ByteBuffer compact() method in Java with Examples The compact() method of java.nio.ByteBuffer class is used to compact the given buffer.The bytes between the buffer's current position and its limit, if any, are copied to the beginning of the buffer. That is, the byte at index p = position() is copied to index zero, the byte at index p + 1 is copied 3 min read ByteBuffer get() method in Java with Examples get() The get() method of java.nio.ByteBuffer class is used to read the byte at the buffer's current position, and then increments the position. Syntax : public abstract byte get() Return Value: This method returns the byte at the buffer's current position. Throws: This method throws BufferUnderflow 6 min read ByteBuffer mark() methods in Java with Examples The mark() method of java.nio.ByteBuffer Class is used to set this buffer's mark at its position. Syntax: public ByteBuffer 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 i 2 min read Like