Java - FilterInputStream markSupported() method



Description

The Java FilterInputStream markSupported() method checks whether the input stream supports marking and resetting using mark(int readlimit) and reset(). Returns true if the stream supports mark(int) and reset(). Returns false if marking is not supported. Commonly supported by: BufferedInputStream, PushbackInputStream. Not supported by: DataInputStream, FileInputStream.

Declaration

Following is the declaration for java.io.FilterInputStream.markSupported() method −

public boolean markSupported()

Parameters

NA

Return Value

The method returns true if the stream type supports mark and reset methods.

Exception

NA

Example - Usage of FilterInputStream markSupported() method

The following example shows the usage of Java FilterInputStream markSupported() 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; 
      boolean bool = false;
      
      try {
         // create input streams
         is = new FileInputStream("test.txt");
         fis = new BufferedInputStream(is);
         
         // tests if the input stream supports mark() and reset()
         bool = fis.markSupported();
         
         // prints
         System.out.print("Supports mark and reset methods: "+bool);
         
      } catch(IOException e) {
         // if any I/O error occurs
         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−

Supports mark and reset methods: true

Example - Checking markSupported() for BufferedInputStream

The following example shows the usage of Java FilterInputStream markSupported() 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());

         fis.mark(5); // Mark the current position

         System.out.print((char) fis.read()); // Read a character
         System.out.print((char) fis.read()); // Read another character

         fis.reset(); // Reset back to the mark
         System.out.print((char) fis.read()); // Read again

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

Output(assuming example.txt contains JavaProgramming)

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

Mark supported? true
JaJ

Explanation

  • Uses BufferedInputStream, which supports marking.

  • Calls markSupported(), which returns true.

  • Marks a position, reads characters, and resets to the mark.

Example - Checking markSupported() for DataInputStream

The following example shows the usage of Java FilterInputStream markSupported() 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());

         fis.mark(5); // Attempting to mark
         System.out.print((char) fis.read()); // Read a character

         fis.reset(); // Will throw an IOException since mark is not supported
      } 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
Reset not supported: mark/reset not supported

Explanation

  • Uses DataInputStream, which does not support marking.

  • Calls markSupported(), which returns false.

  • Attempting reset() throws an IOException.

java_io_filterinputstream.htm
Advertisements