BufferedInputStream reset() method in Java with Examples - GeeksforGeeks

05 Jun, 2020
The reset() method of BufferedInputStream class in Java is used to reset the position of the stream to the position at the time the mark method was last called. It is used with the combination of mark() method of the same class.
General Contract:There are two cases:
If mark() and reset() are supported i.e., if markSupported returns true,An IOException might be thrown if the mark() is not called since the creation of the input stream or the bytes that are read from the input stream since the last call of mark() method is greater than the argument to mark method at the last call.In case an IOException is not thrown, then the input stream is reset to a state such that all the bytes that are read after the most recent call of mark() is supplied again to subsequent callers of the read(). This is followed by the bytes that would have at reset() call.If mark() and reset() are not supported i.e., if markSupported returns false,Calling reset() method might throw an IOException.In case an IOException is not thrown, then input stream is reset to a fixed state that depends on the particular type of the input stream. The supplied bytes to subsequent callers of the read() depend on the particular type of the input stream.
If mark() and reset() are supported i.e., if markSupported returns true,An IOException might be thrown if the mark() is not called since the creation of the input stream or the bytes that are read from the input stream since the last call of mark() method is greater than the argument to mark method at the last call.In case an IOException is not thrown, then the input stream is reset to a state such that all the bytes that are read after the most recent call of mark() is supplied again to subsequent callers of the read(). This is followed by the bytes that would have at reset() call.
An IOException might be thrown if the mark() is not called since the creation of the input stream or the bytes that are read from the input stream since the last call of mark() method is greater than the argument to mark method at the last call.
In case an IOException is not thrown, then the input stream is reset to a state such that all the bytes that are read after the most recent call of mark() is supplied again to subsequent callers of the read(). This is followed by the bytes that would have at reset() call.
If mark() and reset() are not supported i.e., if markSupported returns false,Calling reset() method might throw an IOException.In case an IOException is not thrown, then input stream is reset to a fixed state that depends on the particular type of the input stream. The supplied bytes to subsequent callers of the read() depend on the particular type of the input stream.
Calling reset() method might throw an IOException.
In case an IOException is not thrown, then input stream is reset to a fixed state that depends on the particular type of the input stream. The supplied bytes to subsequent callers of the read() depend on the particular type of the input stream.
Syntax:
public void reset()
Overrides: The method overrides the reset class in FilterInputStream.
Parameters: This method does not accept any parameter.
Return value: This method does not return any value.
Exception: This method throws IOException if this stream has not been marked or mark has been invalidated or input stream has been closed by invoking the close() method or an I/O error occurs.
Below programs illustrates reset() method in BufferedInputStream class in IO package:
Program 1: Assume the existence of file “c:/demo.txt”.
// Java program to illustrate// BufferedInputStream reset() methodimport java.io.*;public class GFG {    public static void main(String[] args)    {          // Create input stream 'demo.txt'        // for reading containing text "GEEKS"        FileInputStream inputStream            = new FileInputStream(                "c:/demo.txt");          // Convert inputStream to        // bufferedInputStream        BufferedInputStream buffInputStr            = new BufferedInputStream(                inputStream);          // Read and print characters one by one        System.out.println(            "Char : "            + (char)buffInputStr.read());        System.out.println(            "Char : "            + (char)buffInputStr.read());        System.out.println(            "Char : "            + (char)buffInputStr.read());          // Mark is set on the input stream        buffInputStr.mark(0);          System.out.println(            "Char : "            + (char)buffInputStr.read());          System.out.println(            "reset() called");          // Reset() is invoked        buffInputStr.reset();          // Read and print characters        System.out.println(            "Char : "            + (char)buffInputStr.read());        System.out.println(            "Char : "            + (char)buffInputStr.read());    }}
Program 2: Assume the existence of file “c:/demo.txt”.
// Java program to illustrate// BufferedInputStream reset() methodimport java.io.*;public class GFG {    public static void main(String[] args)    {          // Create input stream 'demo.txt'        // for reading containing text        // "GEEKSFORGEEKS"        FileInputStream inputStream            = new FileInputStream(                "c:/demo.txt");          // Convert inputStream to        // bufferedInputStream        BufferedInputStream buffInputStr            = new BufferedInputStream(                inputStream);          // Read and print characters one by one        System.out.println(            "Char : "            + (char)buffInputStr.read());        System.out.println(            "Char : "            + (char)buffInputStr.read());        System.out.println(            "Char : "            + (char)buffInputStr.read());        System.out.println(            "Char : "            + (char)buffInputStr.read());          // Mark is set on the input stream        buffInputStr.mark(0);          System.out.println(            "Char : "            + (char)buffInputStr.read());          System.out.println(            "reset() called");          // Reset() is invoked        buffInputStr.reset();          // Read and print characters        System.out.println(            "Char : "            + (char)buffInputStr.read());        System.out.println(            "Char : "            + (char)buffInputStr.read());        System.out.println(            "Char : "            + (char)buffInputStr.read());        System.out.println(            "Char : "            + (char)buffInputStr.read());    }}
References:https://docs.oracle.com/javase/10/docs/api/java/io/BufferedInputStream.html#reset()
Java-Functions
Java-IO package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Generics in Java
Comparator Interface in Java with Examples
Strings in Java
How to remove an element from ArrayList in Java?
Difference between Abstract Class and Interface in Java