Java - BufferedInputStream mark() method



Description

The Java BufferedInputStream mark() method sets the limit of bytes by int value to be read before the mark postion becomes invalid.

The mark() method allows the input stream to remember its current position in the stream so that the reset() method can later reposition the stream back to this point.

Behavior

  • The mark remains valid as long as fewer than readLimit bytes are read after the call to mark().

  • If more bytes are read than the readLimit, the mark is invalidated, and a subsequent call to reset() will throw an IOException.

Declaration

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

public void mark(int readlimit)

Parameters

readLimit − specifies the maximum number of bytes that can be read while still preserving the mark.

Return Value

This method does not return any value.

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 mark() method

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

BufferedInputStreamDemo.java

package com.tutorialspoint;

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

public class BufferedInputStreamDemo {
   public static void main(String[] args) throws Exception {
      InputStream iStream = null;
      BufferedInputStream bis = null;
      
      try {
         // read from file example.txt to input stream        
         iStream = new FileInputStream("example.txt");
         
         // input stream converted to buffered input stream
         bis = new BufferedInputStream(iStream);
         
         // read and print characters one by one
         System.out.println("Char : "+(char)bis.read());
         System.out.println("Char : "+(char)bis.read());
         System.out.println("Char : "+(char)bis.read());
         
         // mark is set on the input stream
         bis.mark(0);
         System.out.println("Char : "+(char)bis.read());
         System.out.println("reset() invoked");
         
         // reset is called
         bis.reset();
         
         // read and print characters
         System.out.println("char : "+(char)bis.read());
         System.out.println("char : "+(char)bis.read());

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

Output

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

Char : A
Char : B
Char : C
Char : D
reset() invoked
char : D
char : E

Example - Basic Usage of mark() and reset() methods

This example demonstrate proper usage of mark(int readLimit) method in a BufferedInputStream object.

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 = "Hello, BufferedInputStream!".getBytes();
      try (BufferedInputStream bis = new BufferedInputStream(
         new ByteArrayInputStream(data))) {
         System.out.println("Marking position at the start of the stream.");
         bis.mark(10); // Mark position with a readLimit of 10 bytes

         // Read first 5 bytes
         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 mark
         // Re-read the first 5 bytes
         for (int i = 0; i < 5; i++) {
            System.out.print((char) bis.read());
         }
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Marking position at the start of the stream.
Hello
Resetting to the marked position.
Hello

Explanation

  • mark(10) sets a mark at the beginning of the stream with a read limit of 10 bytes.

  • The first 5 bytes are read, moving the stream position forward.

  • reset() returns the stream position to the marked point, allowing the first 5 bytes to be read again.

Example - Invalidating the Mark

This example demonstrate proper usage of mark(int readLimit) method in a BufferedInputStream object.

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 = "Hello, BufferedInputStream!".getBytes();
      try (BufferedInputStream bis = new BufferedInputStream(
         new ByteArrayInputStream(data), 8)) {
         System.out.println("Marking position at the start of the stream.");
         bis.mark(10); // Mark position with a readLimit of 10 bytes

         // Read 12 bytes (exceeding the readLimit)
         for (int i = 0; i < 12; i++) {
            System.out.print((char) bis.read());
         }
         System.out.println("\nAttempting to reset.");

         // Try resetting to the mark
         bis.reset();
      } catch (IOException e) {
         System.out.println("IOException occurred: " + e.getMessage());
      }
   }
}

Output

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

Marking position at the start of the stream.
Hello, Buffe
Attempting to reset.
IOException occurred: Resetting to invalid mark

Explanation

  • mark(10) sets a mark with a read limit of 10 bytes.

  • 12 bytes are read, exceeding the specified read limit.

  • reset() attempts to return to the mark but fails because the read limit was exceeded. An IOException is thrown, and the error message is displayed

java_io_bufferedinputstream.htm
Advertisements