Java - InputStream markSupported() method



Description

The Java InputStream markSupported() method checks whether the input stream supports marking and resetting using mark(int readLimit) and reset(). Returns true if the stream supports mark() and reset(), false if marking is not supported.

Declaration

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

public boolean markSupported()

Parameters

NA

Return Value

The method returns true if this stream supports instance the mark and reset method.

Exception

NA

Example - Usage of InputStream markSupported() method

The following example shows the usage of Java InputStream markSupported() method.

InputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream is = null;
      boolean bool = false;      
      
      try {
         // new input stream created
         is = new FileInputStream("test.txt");
         
         // returns true if the mark()/reset() supported.
         bool = is.markSupported();
         
         // prints
         System.out.print("Is mark()/reset() supported? ");
         System.out.print(bool);
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(is!=null)
            is.close();
      }
   }
}

Output(Assuming test.txt contains "ABCDEF")

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

Is mark()/reset() supported? false

Example - Checking markSupported() for BufferedInputStream

The following example shows the usage of Java InputStream markSupported() method.

InputStreamDemo.java

package com.tutorialspoint;

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

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

         inputStream.mark(5); // Mark the position

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

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

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

Output(if example.txt contains "Java")

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

Mark supported? true
Jav

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 FileInputStream (Mark Not Supported)

The following example shows the usage of Java InputStream markSupported() method.

InputStreamDemo.java

package com.tutorialspoint;

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

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

         inputStream.mark(10); // Attempting to mark (Has no effect)
         System.out.print((char) inputStream.read()); // Read a character

         inputStream.reset(); // This will throw an IOException since FileInputStream does NOT support mark/reset

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

Output

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

Mark supported? False
HReset failed: mark/reset not supported

Explanation

  • Uses FileInputStream, which does not support marking.

  • Calls markSupported(), which returns false.

  • Attempting reset() throws an IOException.

java_io_inputstream.htm
Advertisements