Java - BufferedInputStream markSupported() method



Description

The Java BufferedInputStream markSupported() method tests if the input stream type supports mark() and reset() methods. markSupported() method returns true for BufferedInputStream.

The markSupported() method of the BufferedInputStream class checks whether the input stream supports the mark() and reset() methods. It returns true for BufferedInputStream because it inherently supports these methods. This method is typically used to confirm if the mark() and reset() operations are available before using them.

Declaration

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

public boolean markSupported()

Parameters

NA

Return Value

This method returns true if the stream type supports mark() and read() methods else the method returns false.

Exception

NA

Assumption

Assuming we have a text file example.txt, which has the following content. This file will be used as an input for our example programs −

ABCDE

Example - Using markSupported() method

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

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedInputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream inStream = null;
      BufferedInputStream bis = null;
      boolean bool = false;
      
      try {
         // open input stream test.txt for reading purpose.
         inStream = new FileInputStream("example.txt");

         // input stream is converted to buffered input stream
         bis = new BufferedInputStream(inStream);
         
         // returns true if mark() and read() supports
         bool = bis.markSupported();
         System.out.println("Support for mark() and reset() : "+bool);  
      
      } catch(Exception e) {
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(bis!=null)
            bis.close();
         if(inStream!=null)
            inStream.close();
      }
   }
}

Output

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

Support for mark() and reset() : true

Example - Checking markSupport with BufferedInputStream

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

BufferedInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;

public class BufferedInputStreamDemo {
   public static void main(String[] args) {
      byte[] data = "Hello, BufferedInputStream!".getBytes();

      try (BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(data))) {
         // Check if the stream supports mark/reset
         if (bis.markSupported()) {
            System.out.println("mark/reset is supported by this stream.");

            // Demonstrate mark/reset
            bis.mark(10); // Mark the current position with a read limit of 10 bytes
            System.out.println("Mark set. Reading first 5 characters:");

            for (int i = 0; i < 5; i++) {
               System.out.print((char) bis.read());
            }

            System.out.println("\nResetting to the marked position.");
            bis.reset(); // Reset to the marked position

            // Read the same characters again
            for (int i = 0; i < 5; i++) {
               System.out.print((char) bis.read());
            }
         } else {
            System.out.println("mark/reset is NOT supported by this stream.");
         }
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

Output

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

mark/reset is supported by this stream.
Mark set. Reading first 5 characters:
Hello
Resetting to the marked position.
Hello

Explanation

  • The program checks if markSupported() returns true for the BufferedInputStream (it does).

  • Demonstrates marking and resetting functionality.

Example - Checking markSupport with ByteArrayStream

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

BufferedInputStreamDemo.java

package com.tutorialspoint;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;

public class BufferedInputStreamDemo {
   public static void main(String[] args) {
      byte[] data = "This is a test for markSupported!".getBytes();
      try (BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(data))) {
         // Check if mark/reset is supported
         if (bis.markSupported()) {
            System.out.println("mark/reset is supported by BufferedInputStream.");
            // Mark the stream
            bis.mark(20); // Mark position with a read limit of 20 bytes
            System.out.println("Mark set. Reading first 10 characters:");
            for (int i = 0; i < 10; i++) {
               System.out.print((char) bis.read());
            }
            System.out.println("\nResetting to the marked position.");                                  
            bis.reset(); // Reset to the marked position

            System.out.println("Re-reading the first 10 characters:");
            for (int i = 0; i < 10; i++) {
               System.out.print((char) bis.read());
            }
         } else {
            System.out.println("mark/reset is NOT supported by this stream.");
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

mark/reset is supported by BufferedInputStream.
Mark set. Reading first 10 characters:
This is a 
Resetting to the marked position.
Re-reading the first 10 characters:
This is a

Explanation

  • The program checks if the BufferedInputStream wrapped around a ByteArrayInputStream supports mark/reset (it does).

  • Demonstrates marking, reading some bytes, and resetting the stream to reread those bytes.

Notes

  • In examples, markSupported() is used to ensure that the stream supports marking and resetting before attempting to use those features.

  • Using mark() and reset() without checking markSupported() could result in exceptions for streams that do not support marking.

java_io_bufferedinputstream.htm
Advertisements