ByteBuffer clear() methods in Java with Examples Last Updated : 27 Jun, 2019 Comments Improve Suggest changes 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 clear() methods in Java with Examples 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 ByteBuffer duplicate() method in Java with Examples The duplicate() method of java.nio.ByteBuffer class is used to create a new byte 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, 3 min read ByteBuffer 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 ByteBuffer wrap() methods in Java with Examples wrap(byte[] array) The wrap() method of java.nio.ByteBuffer Class is used to wraps a byte array into a buffer. The new buffer will be backed by the given byte array, i.e., modifications to the buffer will cause the array to be modified and vice versa. The new buffer's capacity and limit will be arra 4 min read ByteBuffer reset() methods in Java with Examples The reset() method of java.nio.ByteBuffer Class is used to reset this buffer's position to the previously-marked position. Invoking this method neither changes nor discards the mark's value. Syntax: public ByteBuffer reset() Return Value: This method returns this buffer. Below are the examples to il 2 min read Like