Buffer reset() methods in Java with Examples

The reset() method of java.nio.Buffer 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 Buffer reset()
Return Value: This method returns this buffer.
Below are the examples to illustrate the reset() method:
Examples 1:
Â
Java
// Java program to demonstrate// reset() 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);Â
            // Typecast ByteBuffer to buffer            Buffer buffer = (Buffer)bb;Â
            // try to set the position at index 2            buffer.position(2);Â
            // Set this buffer mark position            // using mark() method            buffer.mark();Â
            // try to set the position at index 4            buffer.position(4);Â
            // display position            System.out.println("position before reset: "                               + buffer.position());Â
            // try to call reset() to restore            // to the position we marked            buffer.reset();Â
            // display position            System.out.println("position after reset: "                               + buffer.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: 2
Â
Examples 2:
Â
Java
// Java program to demonstrate// reset() 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);Â
            // Typecast ByteBuffer to buffer            Buffer buffer = (Buffer)bb;Â
            // try to set the position at index 2            buffer.position(2);Â
            // Set this buffer mark position            // using mark() method            buffer.mark();Â
            // try to set the position at index 4            buffer.position(1);Â
            // display position            System.out.println("position before reset: "                               + buffer.position());Â
            // try to call reset() to restore            // to the position we marked            buffer.reset();Â
            // display position            System.out.println("position after reset: "                               + buffer.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: 1 new position is less than the position we marked before Exception throws: java.nio.InvalidMarkException
Â
Reference: https://docs.oracle.com/javase/9/docs/api/java/nio/Buffer.html#reset–
Â



