Java - ByteArrayInputStream close() method



Description

The Java ByteArrayInputStream close() method is used to release any resources associated with the stream. However, since ByteArrayInputStream operates on an in-memory byte array, calling close() method does not have any actual effect. It is effectively a no-op. It is still good practice to call close() method on streams for consistency, especially when working with streams that require resource cleanup.

Declaration

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

public void close()

Parameters

NA

Return Value

This method doesn't return any value.

Exception

IOException− If an I/O error occurs

Example - Using ByteArrayInputStream close() method on a byte[]

The following example shows the usage of Java ByteArrayInputStream close() 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've used the close() method to close the stream before invoking read() and available() method. Then in while loop, we're checking if stream contains any byte using available() method and then its bytes are read using read() method.

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);
         
         // close invoked before read() and available() invocations
         bais.close();
         
         int count = 0;
         
         // read till the end of the stream
         while((count = bais.available())>0) {
            
            // convert byte to character
            char c = (char)bais.read();
            
            // print number of bytes available
            System.out.print("available byte(s) : "+ count);
            
            // print characters read form the byte array
            System.out.println(" & byte read : "+c);
         }
         
      } 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 −

available byte(s) : 5 & byte read : A
available byte(s) : 4 & byte read : B
available byte(s) : 3 & byte read : C
available byte(s) : 2 & byte read : D
available byte(s) : 1 & byte read : E

Example - Using ByteArrayInputStream close() method with empty byte[]

The following example shows the usage of Java ByteArrayInputStream close() 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. In while loop, we're checking if stream contains any byte using available() method and then its bytes are read using read() method. In finally block, we've called the close() method to free up the resources.

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);
         
         int count = 0;
         
         // read till the end of the stream
         while((count = bais.available())>0) {
            
            // convert byte to character
            char c = (char)bais.read();
            
            // print number of bytes available
            System.out.print("available byte(s) : "+ count);
            
            // print characters read form the byte array
            System.out.println(" & byte read : "+c);
         }
         
      } 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 −

available byte(s) : 5 & byte read : A
available byte(s) : 4 & byte read : B
available byte(s) : 3 & byte read : C
available byte(s) : 2 & byte read : D
available byte(s) : 1 & byte read : E

Example - Using ByteArrayInputStream close() method

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

ByteArrayInputStreamDemo.java

package com.tutorialspoint;

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

public class ByteArrayInputStreamDemo {
   public static void main(String[] args) {
      // Create a byte array
      byte[] data = {72, 101, 108, 108, 111}; // Corresponds to 'H', 'e', 'l', 'l', 'o'

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

      // Read and print data from the stream
      try {
         int byteData;
         System.out.println("Reading bytes from ByteArrayInputStream:");
         while ((byteData = inputStream.read()) != -1) {
            System.out.print((char) byteData);
         }
         System.out.println();

         // Close the ByteArrayInputStream
         inputStream.close();
         System.out.println("Stream closed successfully.");
      } catch (IOException e) {
         System.err.println("An I/O error occurred: " + e.getMessage());
      }
   }
}

Output

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

Reading bytes from ByteArrayInputStream:
Hello
Stream closed successfully.

Explanation

  • Initialization− A ByteArrayInputStream is created using a byte array that represents the string "Hello".

  • Reading Data− The read() method is used to read bytes from the stream one at a time until the end of the stream is reached.

  • Closing the Stream− The close() method is called to close the stream. For ByteArrayInputStream, this is not strictly necessary because no resources are allocated outside of the memory. However, calling close() is consistent with standard Java I/O practices.

Key Points

  • No Effect for ByteArrayInputStream() The close() method has no effect in ByteArrayInputStream, but calling it is a good habit to adopt for stream management consistency.

java_bytearrayinputstream.htm
Advertisements