Saturday, October 11, 2025
HomeLanguagesJavaByteArrayInputStream close() method in Java with Examples

ByteArrayInputStream close() method in Java with Examples

The close() method is a built-in method of the Java.io.ByteArrayInputStream closes the input stream and releases system resources associated with this stream to Garbage Collector.

Syntax:

public void close()

Parameters: The function does not accepts any parameter.

Return Value: The function returns nothing.

Below is the implementation of the above function:

Program 1:




// Java program to implement
// the above function
import java.io.*;
  
public class Main {
    public static void main(String[] args) throws Exception
    {
  
        // Array
        byte[] buffer = { 1, 2, 3, 4 };
  
        // Create InputStream
        ByteArrayInputStream geek
            = new ByteArrayInputStream(buffer);
  
        // Use the function to get the number
        // of available
        int number = geek.available();
  
        // Print
        System.out.println("Use of available() method : "
                           + number);
  
        // Closes the InputStream
        geek.close();
    }
}


Output:

Use of available() method : 4

Program 2:




// Java program to implement
// the above function
import java.io.*;
  
public class Main {
    public static void main(String[] args) throws Exception
    {
  
        // Array
        byte[] buffer = { 2, 3, 4, 8, 9 };
  
        // Create InputStream
        ByteArrayInputStream geek
            = new ByteArrayInputStream(buffer);
  
        // Use the function to get the number
        // of available
        int number = geek.available();
  
        // Print
        System.out.println("Use of available() method : "
                           + number);
  
        // Closes the InputStream
        geek.close();
    }
}


Output:

Use of available() method : 5

Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ByteArrayInputStream.html#close()

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6719 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS