English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java flow control

Java array

Java object-oriented (I)

Java object-oriented (II)

Java object-oriented (III)

Java Exception Handling

Java List

Java Queue (queue)

Java Map collection

Java Set collection

Java input output (I/O)

Java Reader/Writer

Java other topics

Java ByteArrayInputStream class

In this tutorial, we will learn about Java ByteArrayInputStream and its methods with the help of examples.

The ByteArrayInputStream class in the java.io package can be used to read input data arrays (in bytes).

It inherits the InputStream abstract class.

Note: In ByteArrayInputStream, an input stream is created using a byte array. It includes an internal array used to store the data of the specific byte array.

Create a ByteArrayInputStream

To create a byte array input stream, we must first import the java.io.ByteArrayInputStream package. After importing the package, we can create the input stream.

//Create a ByteArrayInputStream to read the entire array
ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr);

Here, we have created an input stream that reads the entire data from the arr array. However, we can also create an input stream that reads only some data from the array.

//Create a ByteArrayInputStream to read a part of the array
ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr, int start, int length);

Here, the input stream starts reading a number of bytes equal to the array length from the start position of the array.

Methods of ByteArrayInputStream

The ByteArrayInputStream class provides implementations for different methods provided in the InputStream class.

read() method

  • read()  - Reads a single byte from the existing array in the input stream.

  • read(byte[] array)  - Reads bytes from the input stream and stores them in the specified array.

  • read(byte[] array, int start, int length) - Reads a byte array of length and stores it starting from position start in the specified array from the stream.

Example: ByteArrayInputStream reads data

import java.io.ByteArrayInputStream;
public class Main {
  public static void main(String[] args) {
    //Create a byte array
    byte[] array = {1, 2, 3, 4};
    try {
      ByteArrayInputStream input = new ByteArrayInputStream(array);
      System.out.print("Bytes read from the input stream: ");
      for(int i = 0; i < array.length;++) {
        //Read bytes
        int data = input.read();
        System.out.print(data + "");
      }
      input.close();
    }
    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

Bytes read from the input stream: 1, 2, 3, 4,

In the above example, we created a byte array input stream named input.

ByteArrayInputStream input = new ByteArrayInputStream(array);

Here, the input stream includes all data from the specified array. To read data from the input stream, we use the read() method.

available() method

To get the number of available bytes in the input stream, we can use the available() method. For example,

import java.io.ByteArrayInputStream;
public class Main {
  public static void main(String args[]) {
    //Create a byte array
    byte[] array = { 1, 2, 3, 4 };
    try {
      ByteArrayInputStream input = new ByteArrayInputStream(array);
      //Returns the number of available bytes
      System.out.println("Available byte count at the start: "); + input.available());
      //Read two bytes from the input stream
      input.read();
      input.read();
      //Returns the number of available bytes
      System.out.println("Available byte count at the end: "); + input.available());
      input.close();
    }
    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

Available byte count at the start: 4
Available byte count at the end: 2

In the above example,

  1. We have used the available() method to check the number of available bytes in the input stream.

  2. Then, we use the read() method2time from the input stream2bytes.

  3. Now, after reading2bytes after, we checked the available bytes. This time, the available bytes decreased2.

skip() method

To discard and skip specified bytes, you can use the skip() method. For example

import java.io.ByteArrayInputStream;
public class Main {
  public static void main(String args[]) {
    //Create a byte array
    byte[] array = { 1, 2, 3, 4 };
    try {
      ByteArrayInputStream input = new ByteArrayInputStream(array);
      //Use the skip() method
      input.skip(2);
      System.out.print("Skip ");2bytes after the input stream: ");
      int data = input.read();
      while (data != -1) {
        System.out.print(data + "");
        data = input.read();
      }
      // close() Method
      input.close();
    }
    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

Skip2bytes after the input stream: 3, 4,

In the above example, we use the skip() method to skip2byte data. Therefore, it will not read from the input stream1And2.

close() Method

To close the input stream, you can use the close() method.

However, the close() method does not take effect in the ByteArrayInputStream class. Even after the close() method is called, we can still use the methods of this class.

Other Methods of ByteArrayInputStream

MethodContent Description
finalize()Ensure that the close() method is called
mark()Mark the position of the data that has been read in the input stream
reset()Return the control to the point in the input stream where the mark was set
markSupported()Check if the input stream supports mark() and reset()