Java - InputStream mark(int readLimit) method



Description

The Java InputStream mark(int readLimit) method marks a position in the input stream, allowing it to be reset later using reset(). Marks the current position in the stream. readLimit specifies how many bytes can be read before the mark becomes invalid. The stream must support marking (markSupported() should return true).

Declaration

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

public void mark(int readLimit)

Parameters

readlimit − The maximum number 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 InputStream mark(int readLimit) method

The following example shows the usage of Java InputStream mark(int readLimit) 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;
            
      try {
         // new input stream created
         is = new FileInputStream("test.txt");
         
         // read and print characters one by one
         System.out.println("Char : "+(char)is.read());
         System.out.println("Char : "+(char)is.read());
         System.out.println("Char : "+(char)is.read());
                     
         // mark is set on the input stream
         is.mark(0);
         
         System.out.println("Char : "+(char)is.read());
         System.out.println("Char : "+(char)is.read());
         
         if(is.markSupported()) {
         
            // reset invoked if mark() is supported
            is.reset();
            System.out.println("Char : "+(char)is.read());
            System.out.println("Char : "+(char)is.read());
         }
         
      } 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−

Char : A
Char : B
Char : C
Char : D
Char : E

Example - Using mark(int readLimit) with BufferedInputStream

The following example shows the usage of Java InputStream mark(int readLimit) 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());

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

         // Mark the current position
         inputStream.mark(5); // Can read up to 5 bytes before mark expires

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

         // Reset back to the marked position
         inputStream.reset();

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

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

Output(if example.txt contains "Hello")

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

Mark supported? true
Helo
elo

Explanation

  • Uses BufferedInputStream, which supports marking.

  • Marks the position after reading one character.

  • Reads two more characters.

  • Resets to the marked position.

  • Reads again from the marked position.

Example - Checking mark(int readLimit) with ByteArrayInputStream

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

InputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] data = "Microservices".getBytes();
      InputStream inputStream = new ByteArrayInputStream(data);

      System.out.println("Mark supported? " + inputStream.markSupported());

      inputStream.mark(6); // Mark after reading no bytes
      System.out.print((char) inputStream.read()); // Read 'M'
      System.out.print((char) inputStream.read()); // Read 'i'

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

      try {
         inputStream.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Mark supported? true
MiM

Explanation

  • Uses ByteArrayInputStream, which supports marking.

  • Marks at the beginning of the input stream.

  • Reads two characters (M and i).

  • Resets to the marked position and reads M again.

java_io_inputstream.htm
Advertisements