Java - ByteArrayOutputStream reset() method



Description

The Java ByteArrayOutputStream reset() method is used to discard the currently stored data in the buffer and reset the stream to an empty state. This is useful when you want to reuse the same ByteArrayOutputStream instance for writing new data without creating a new object.

Declaration

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

public void reset()

Parameters

NA

Return Value

This method doesn't return any value.

Exception

NA

Example - Using ByteArrayOutputStream reset() method

The following example shows the usage of Java ByteArrayOutputStream reset() method. We've created a ByteArrayOutputStream reference and then initialized it with ByteArrayOutputStream object. Now we write a value to output stream and print the stream using toString() method. As a next step, we've reset the output stream to be empty and write a new value and printed it again.

ByteArrayOutputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      ByteArrayOutputStream baos = null;      
      try {
         String str = "";
         
         // create new ByteArrayOutputStream
         baos = new ByteArrayOutputStream();
         
         // writing byte to output stream
         baos.write(75);
         
         // output stream to string
         str = baos.toString();
         System.out.println("Before Resetting : "+str);
         
         // reset() method invocation 
         baos.reset();
         
         // writing byte to output stream
         baos.write(65);
         
         // output stream to string()
         str = baos.toString();
         System.out.println("After Resetting : "+ str);
      
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(baos!=null)
            baos.close();
      }   
   }
}

Output

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

Before Resetting : K
After Resetting : A

Example - Using ByteArrayOutputStream reset() method

The following example shows the usage of Java ByteArrayOutputStream reset() method. We've created a ByteArrayOutputStream reference and then initialized it with ByteArrayOutputStream object. Now we're writing multiple value to output stream and print the stream using toString() method. As a next step, we've reset the output stream to be empty and printed it again.

ByteArrayOutputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamDemo {
   public static void main(String[] args) throws IOException {
      ByteArrayOutputStream baos = null;
      
      try {
         String str = "";
         
         // create new ByteArrayOutputStream
         baos = new ByteArrayOutputStream();
         
         // writing bytes to output stream
         baos.write(75);
         baos.write(65);
         
         // output stream to string
         str = baos.toString();
         System.out.println("Before Resetting : "+str);
         
         // reset() method invocation 
         baos.reset();
         
         // output stream to string()
         str = baos.toString();
         System.out.println("After Resetting : "+ str);
      
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(baos!=null)
            baos.close();
      }   
   }
}

Output

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

Before Resetting : KA
After Resetting : 

Example - Using reset() to Reuse the Stream

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

ByteArrayOutputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamDemo {
   public static void main(String[] args) {
      // Create a ByteArrayOutputStream
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

      try {
         // Write some data to the stream
         outputStream.write("Hello, World!".getBytes());
         System.out.println("First output: " + outputStream.toString());

         // Reset the stream to clear the buffer
         outputStream.reset();
         System.out.println("Stream reset.");

         // Write new data to the same stream
         outputStream.write("Java Programming".getBytes());
         System.out.println("Second output: " + outputStream.toString());

         // Close the stream (optional for ByteArrayOutputStream)
         outputStream.close();
      } catch (IOException e) {
         System.err.println("IOException occurred: " + e.getMessage());
      }
   }
}

Output

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

First output: Hello, World!
Stream reset.
Second output: Java Programming

Explanation

  • A ByteArrayOutputStream is created.

  • The first string, "Hello, World!", is written to the stream and printed.

  • The reset() method is called, which clears the buffer and makes the stream empty again.

  • The second string, "Java Programming", is written to the same stream after resetting it.

  • The new content of the stream is printed, showing that the previous data has been discarded.

Key Points

  • Purpose

    • The reset() method clears the buffer, allowing the ByteArrayOutputStream to be reused.

  • Efficiency

    • Reusing the same instance avoids creating multiple objects, which can improve performance in scenarios where the stream is used repeatedly.

  • Use Case

    • Useful in applications where data is written to the stream in multiple phases or iterations, and previous data is no longer needed.

  • Behavior

    • After calling reset(), the size of the buffer is set to 0, but the internal buffer itself is retained for reuse, reducing memory overhead.

java_bytearrayoutputstream.htm
Advertisements