The mark() method of BufferedInputStream class in Java is used to mark the current position in the input stream. Reset() method of the same class BufferedInputStream is called after the mark() method. Reset() fixes the position at the last marked position so that same byte can be read again.
General Contract: The input stream somehow saves all the bytes that were read after the mark() method was called and returns same bytes again when reset() method is called. This operation is performed only if the markSupported() returns true. However, if the bytes that are read before the call of the reset() method, is more than readlimit bytes then the input stream does not require to save any data.
Syntax:
public void mark(int readlimit)
Overrides: It overrides the mark() method of FilterInputStream class.
Parameters: This method accepts one parameter readlimit of Integer type which represents the maximum limit of bytes that can be read before the mark position becomes invalid.
Return value: This method does not return any value.
Exception: This method does not throw any exception.
Below programs illustrates mark() method in BufferedInputStream class in IO package:
Program 1: Assume the existence of the file “c:/demo.txt”.
// Java program to illustrate // BufferedInputStream mark() method import 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()); // Reset() is invoked buffInputStr.reset(); // Read and print characters System.out.println( "Char : " + ( char )buffInputStr.read()); System.out.println( "Char : " + ( char )buffInputStr.read()); } } |
Char : G Char : E Char : E Char : K Char : K Char : S
Program 2: Assume the existence of file “c:/demo.txt”.
// Java program to illustrate // BufferedInputStream.mark() method import 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()); // 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()); } } |
Char : G Char : E Char : E Char : K Char : S Char : S Char : F Char : O Char : R
References:
https://docs.oracle.com/javase/10/docs/api/java/io/BufferedInputStream.html#mark(int)