Java - ByteArrayInputStream mark() method



Description

The Java ByteArrayInputStream mark() method sets the current mark position in the stream. By default the mark ByteArrayInputStream are marked at postion 0.

mark method is used to mark the current position in the stream so that you can return to this position later using the reset() method. The readAheadLimit parameter is ignored in ByteArrayInputStream, as it does not impose any limits on how far you can reset.

Declaration

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

public void mark(int readAheadLimit)

Parameters

readLimit− This integer value for this class has no meaning.

Return Value

This method doesn't return any value.

Exception

NA

Example - Using ByteArrayInputStream mark() method

The following example shows the usage of Java ByteArrayInputStream mark() method. We've created a variable buf as byte[] and initialized with few bytes. We've created a ByteArrayInputStream reference and then initialized it with buf variable. We're reading first three bytes using read() method and then using mark() method, we've reset the mark current position and then bytes are read again. Then we've called the reset() method to reset the head to previously marked position and bytes are read again.

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

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

public class ByteArrayInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;      
      try {
         
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         // print bytes
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());         
         System.out.println("Mark() invocation");

         // mark() invocation;
         bais.mark(0);
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());         
         System.out.println("Reset() invocation");
         
         // reset() invocation
         bais.reset();
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());         
      } catch(Exception e) {
         
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }   
   }
}

Output

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

Byte read 65
Byte read 66
Byte read 67
Mark() invocation
Byte read 68
Byte read 69
Reset() invocation
Byte read 68
Byte read 69

Example - Using ByteArrayInputStream mark() method

The following example shows the usage of Java ByteArrayInputStream mark() method. We've created a variable buf as byte[] and initialized with few bytes. We've created a ByteArrayInputStream reference and then initialized it with buf variable. We're reading first two bytes using read() method and then using mark() method, we've reset the mark to current position and then bytes are read again. Then we've called the reset() method to reset the head to previously marked position and bytes are read again.

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

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

public class ByteArrayInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;      
      try {
         
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         // print bytes
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());         
         System.out.println("Mark() invocation");

         // mark() invocation;
         bais.mark(0);
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());         
         System.out.println("Reset() invocation");
         
         // reset() invocation
         bais.reset();
         System.out.println("Byte read "+ bais.read());
         System.out.println("Byte read "+ bais.read());         
      } catch(Exception e) {
         
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }   
   }
}

Output

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

Byte read 65
Byte read 66
Mark() invocation
Byte read 67
Byte read 68
Byte read 69
Reset() invocation
Byte read 67
Byte read 68

Example - Using ByteArrayInputStream mark() method

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

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;

public class ByteArrayInputStreamMarkExample {
   public static void main(String[] args) {
      // Create a byte array
      byte[] data = {65, 66, 67, 68, 69}; // Corresponds to 'A', 'B', 'C', 'D', 'E'

      // Create a ByteArrayInputStream
      ByteArrayInputStream inputStream = new ByteArrayInputStream(data);

      // Read and print the first two bytes
      System.out.println("Reading first two bytes:");
      System.out.println((char) inputStream.read()); // Read 'A'
      System.out.println((char) inputStream.read()); // Read 'B'

      // Mark the current position in the stream
      inputStream.mark(0); // Mark at position 2 (after 'B')
      System.out.println("Marked the current position.");

      // Read the next two bytes
      System.out.println("Reading next two bytes:");
      System.out.println((char) inputStream.read()); // Read 'C'
      System.out.println((char) inputStream.read()); // Read 'D'

      // Reset the stream to the marked position
      inputStream.reset();
      System.out.println("Stream reset to the marked position.");

      // Read again from the marked position
      System.out.println("Reading bytes again from the marked position:");
      System.out.println((char) inputStream.read()); // Read 'C' again
      System.out.println((char) inputStream.read()); // Read 'D' again

      // Read the last byte
      System.out.println((char) inputStream.read()); // Read 'E'
   }
}

Output

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

Reading first two bytes:
A
B
Marked the current position.
Reading next two bytes:
C
D
Stream reset to the marked position.
Reading bytes again from the marked position:
C
D
E

Explanation

  • Initialization− A ByteArrayInputStream is created using a byte array containing the ASCII values for 'A', 'B', 'C', 'D', and 'E'.

  • Marking the Stream− After reading the first two bytes ('A' and 'B'), the mark(0) method is called to mark the current position in the stream. Here, readAheadLimit is ignored for ByteArrayInputStream.

  • Resetting the Stream− After reading the next two bytes ('C' and 'D'), the reset() method is called. This resets the stream back to the position marked by mark() (position 2, after 'B').

  • Reading Again− After resetting, the stream reads 'C' and 'D' again, demonstrating that the stream has returned to the marked position.

  • Final Byte− The program continues to read the last byte ('E') to complete the reading process.

Key Points

  • The mark() method allows you to bookmark a position in the stream.

  • The reset() method repositions the stream to the last marked position.

  • The readAheadLimit parameter is ignored for ByteArrayInputStream.

  • If mark() is not called before reset(), a IOException will be thrown.

java_bytearrayinputstream.htm
Advertisements