Java - ByteArrayInputStream reset() method



Description

The Java ByteArrayInputStream reset() method resets the buffered to the last marked position. It takes mark position is 0, unless a mark is explicitly specified.

reset() method is used to reposition the stream back to the most recent position marked by the mark(int readlimit) method. If mark() has not been called or the mark has been invalidated, calling reset() will throw an IOException.

Declaration

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

public void reset()

Parameters

NA

Return Value

This method doesn't return any value.

Exception

NA

Example - Using ByteArrayInputStream reset() method

The following example shows the usage of Java ByteArrayInputStream reset() 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 reset() method

The following example shows the usage of Java ByteArrayInputStream reset() 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 reset() method

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

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

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

public class ByteArrayInputStreamDemo {
   public static void main(String[] args) {
      // Create a byte array as the data source
      byte[] data = "Hello, World!".getBytes();

      // Create a BufferedInputStream using a ByteArrayInputStream
      try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new ByteArrayInputStream(data))) {

         // Check if mark/reset is supported
         if (bufferedInputStream.markSupported()) {
            System.out.println("mark() and reset() are supported.");

            // Read and print the first 5 bytes
            System.out.print("Initial bytes read: ");
            for (int i = 0; i < 5; i++) {
               System.out.print((char) bufferedInputStream.read());
            }
            System.out.println();

            // Mark the current position
            bufferedInputStream.mark(10); // Allow reading up to 10 bytes before the mark is invalidated
            System.out.println("Stream marked at position.");

            // Read the next 5 bytes
            System.out.print("Next bytes read: ");
            for (int i = 0; i < 5; i++) {
               System.out.print((char) bufferedInputStream.read());
            }
            System.out.println();

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

            // Read the same 5 bytes again after reset
            System.out.print("Bytes read after reset: ");
            for (int i = 0; i < 5; i++) {
               System.out.print((char) bufferedInputStream.read());
            }
            System.out.println();
         } else {
            System.out.println("mark() and reset() are not supported by this stream.");
         }
      } catch (IOException e) {
         System.err.println("An IOException occurred: " + e.getMessage());
      }
   }
}

Output

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

mark() and reset() are supported.
Initial bytes read: Hello
Stream marked at position.
Next bytes read: , Wor
Stream reset to the marked position.
Bytes read after reset: , Wor

Explanation

  • Initialization

    • A BufferedInputStream is created using a ByteArrayInputStream that holds the string "Hello, World!"

    • The markSupported() method is called to check if mark() and reset() are supported. For BufferedInputStream, this always returns true.

  • Marking the Stream

    • The mark(int readlimit) method is called to mark the current position in the stream. The readlimit parameter specifies the maximum number of bytes that can be read before the mark becomes invalid. In this case, it's set to 10.

  • Reading Data

    • The first 5 bytes ("Hello") are read and printed.

    • After marking, the next 5 bytes (", Wor") are read.

  • Resetting the Stream

    • The reset() method is called to reposition the stream back to the marked position.

    • The same 5 bytes (", Wor") are read again, demonstrating that the stream has returned to the marked position.

  • Closing the Stream

    • The try-with-resources statement ensures that the BufferedInputStream is closed automatically.

Key Points

  • Mark and Reset

    • The reset() method repositions the stream to the most recent mark().

    • If mark() is not called before reset(), or if the mark has been invalidated, an IOException will be thrown.

  • Read Limit

    • The readlimit parameter in mark(int readlimit) specifies the maximum number of bytes that can be read while still allowing reset() to work.

  • Practical Use

    • mark() and reset() are useful when you need to temporarily read ahead in a stream and then go back to an earlier position.

java_bytearrayinputstream.htm
Advertisements