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:
Output:
position before reset: 4
position after reset: 0
Examples 2: