Java - FilterInputStream mark(int readLimit) method



Description

The Java FilterInputStream mark(int readLimit) method marks a position in the input stream, allowing the stream to return to that position later using reset(). The method mark(int readlimit) marks the current position in the stream.

Declaration

Following is the declaration for java.io.FilterInputStream.mark(int readLimit) method −

public void mark(int readLimit)

Parameters

readlimit − The maximum limit of bytes that can be read before the mark position becomes invalid.

Return Value

The method does not return any value.

Exception

NA

Example - Usage of FilterInputStream mark(int readLimit) method

The following example shows the usage of Java FilterInputStream mark(int readLimit) method.

FilterInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FilterInputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null; 
      FilterInputStream fis = null; 
      
      try {
         // create input streams
         is = new FileInputStream("test.txt");
         fis = new BufferedInputStream(is);
         
         // reads and prints BufferedReader
         System.out.println((char)fis.read());
         System.out.println((char)fis.read());
         
         // mark invoked at this position
         fis.mark(0);
         System.out.println("mark() invoked");
         System.out.println((char)fis.read());
         System.out.println((char)fis.read());
         
         // reset() repositioned the stream to the mark
         fis.reset();
         System.out.println("reset() invoked");
         System.out.println((char)fis.read());
         System.out.println((char)fis.read());

      } catch(IOException e) {
         
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(is!=null)
            is.close();
         if(fis!=null)
            fis.close();
      }
   }
}

Output(assuming test.txt contains ABCDEF)

Let us compile and run the above program, this will produce the following result−

A
B
mark() invoked
C
D
reset() invoked
C
D

Example - Using mark(int readlimit) with BufferedInputStream

The following example shows the usage of Java FilterInputStream mark(int readLimit) method.

FilterInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class FilterInputStreamDemo {
   public static void main(String[] args) {
      try (FilterInputStream fis = new BufferedInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Mark supported? " + fis.markSupported());

         // Read and print first character
         System.out.print((char) fis.read());

         // Mark the current position
         fis.mark(5); // Can read up to 5 bytes before the mark is invalid

         // Read next two characters
         System.out.print((char) fis.read());
         System.out.print((char) fis.read());

         // Reset back to marked position
         fis.reset();

         // Read again from the marked position
         System.out.print((char) fis.read());
         System.out.print((char) fis.read());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(assuming example.txt contains Hello)

Let us compile and run the above program, this will produce the following result−

Mark supported? true
Helo
elo

Explanation

  • Checks if marking is supported (markSupported()).

  • Marks the position after reading one character (mark(5)) with a read limit of 5.

  • Reads two more characters.

  • Calls reset(), which moves the stream back to the marked position.

  • Reads the same characters again.

Example - Checking if Marking Works on a DataInputStream

The following example shows the usage of Java FilterInputStream mark(int readLimit) method.

FilterInputStreamDemo.java

package com.tutorialspoint;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class FilterInputStreamDemo {
   public static void main(String[] args) {
      try (FilterInputStream fis = new DataInputStream(new FileInputStream("example.txt"))) {
         System.out.println("Mark supported? " + fis.markSupported());

         // Attempt to mark and reset
         fis.mark(10);
         System.out.println((char) fis.read());
         fis.reset(); // This will throw an IOException because DataInputStream does NOT support mark/reset

      } catch (IOException e) {
         System.out.println("Reset not supported: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Mark supported? False
J
Reset not supported: mark/reset not supported

Explanation

  • Uses DataInputStream, which does not support mark().

  • Checks markSupported(), which returns false.

  • Tries to mark and reset, but reset() fails with an IOException.

java_io_filterinputstream.htm
Advertisements