ByteArrayInputStream in Java



ByteArrayInputStream Java

ByteArrayInputStream class in Java

ByteArrayInputStream class in Java reads an array of bytes as a stream. It contains both ByteArray and InputStream. The ByteArrayInputStream class is a subclass of InputStream class. Internally, it has a buffer, that reads an array of bytes as a stream. It is part of the java.io package and its main use is to convert ByteArray into InputStream. The size of the ByteArrayInputStream is dynamic and grows based on the data storage.

ByteArrayInputStream class in Java

Constructors of ByteArrayInputStream in Java

Below are the constructors of the ByteArrayInputStream class in Java.

ConstructorDescription
ByteArrayInputStream(byte[] buf)Creates a ByteArrayInputStream with buf as its byte array
ByteArrayInputStream(byte[] buf, int offset, int length)Creates a ByteArrayInputStream with buf as its byte array and offset as starting position to read data and len as the length of bytes to read

Methods of ByteArrayInputStream in Java

Below are the methods of the ByteArrayInputStream class in Java

MethodDescription
int available()Returns the number of remaining bytes to read
void close()Closes the ByteArrayInputStream object
void mark(int readLimit)Sets the current reading position
boolean markSupported()Checks if the ByteArrayInputStream supports mark or reset methods
int read()Reads the next byte of data from the input stream
int read(byte[] b)Reads some number of bytes and stores it in the byte array 'b'.
int read(byte[] b, int off, int len)Reads the number of bytes starting from the specified offset position until the specified length and stores it in the byte array 'b'
byte[] readAllBytes()Reads all the remaining bytes of data from the input stream
byte[] readNBytes(int len)Reads the specified length of bytes from the input stream
void reset()Resets the buffer to the marked position
long skip(long n)Discards and skips n number of bytes while reading data form the input stream
long transferTo(OutputStream out)Reads all data from the input stream and writes it to the specified output stream.
InputStream nullInputStream()Returns a new InputStream that reads no bytes of data

Java ByteArrayInputStream Examples

Next, we will see various examples of the methods of the ByteArrayInputStream class.

Example: Read a single byte of data

This example shows how to use the read() method that reads a single byte of data at a time. Since we have used the read() method twice, it prints the first 2 data which 20 and 30 respectively.

import java.io.ByteArrayInputStream;
public class ReadDataByteArray {

  public static void main(String[] args) {
    byte[] b = {20,30,40,50,60};
    ByteArrayInputStream by = new ByteArrayInputStream(b);
    System.out.println(by.read());
    System.out.println(by.read());
  
  }

}
20
30

Example: Read an array of bytes

The below example shows how to read an array of bytes using the read() method. Here, we create a buffer with capacity 3, and hence using for loop we can print the array of bytes printing only 3 values.

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ReadDataByteArray {

  public static void main(String[] args) throws IOException {
    byte[] b = {20,30,40,50,60};
    ByteArrayInputStream by = new ByteArrayInputStream(b);

    byte[] buf = new byte[3];
    int n = by.read(buf);
    
    System.out.println("Number of bytes read: " + n);
    
    for(byte c : buf) {
      System.out.println(c);
    }
  }

}
Number of bytes read: 3
20
30
40

Example: Read a specific number of bytes

This example demonstrates how to read a specific number of bytes to the destination array using the read() method of the ByteArrayInoutStream class in Java. The offset position denotes the position in the destination array to start filling the data and the length denotes the number of bytes to read. Here 8 is the capacity of the destination buffer where the starting position is 3 and 4 is the number of bytes to read. We fill the empty positions with “*”.

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ReadDataByteArray {

  public static void main(String[] args) throws IOException {
    byte[] b = {20,30,40,50,60,70,80,90,100};
    ByteArrayInputStream by = new ByteArrayInputStream(b);
  
    byte[] buf = new byte[8];
    int n = by.read(buf,3,4);
    
    System.out.println("Number of bytes read: " + n);
    
    for(byte c : buf) {
      if(c==0)
        System.out.print("*, ");
      else
        System.out.print(c + ",");
    }
  }

}
Number of bytes read: 4
*, *, *, 20,30,40,50,*,

Example: Skip and read bytes using the ByteArrayInputStream

Using the skip() method of the ByteArrayInputStream class in Java, we can also skip or discard a certain number of bytes while reading. In this example, we skip the first 2 bytes and then read the remaining bytes.

import java.io.ByteArrayInputStream;
import java.io.IOException;
public class SkipAndRead {

  public static void main(String[] args) throws IOException {
    byte[] b = {10,20,30,40,50,60};
    ByteArrayInputStream by = new ByteArrayInputStream(b);
    
    by.skip(2);
    
    int a = 0;
    while((a=by.read()) != -1)
      System.out.println((int)a);
    by.close();

  }

}
30
40
50
60

Example: available() method

This example shows to retrieve the remaining number of bytes to read from the ByteArrayInputStream in Java using the available() method. Initially, there are 9 bytes to read. After reading 3 bytes, the remaining number of bytes to read reduces to 6.

import java.io.ByteArrayInputStream;
public class AvailableData {

  public static void main(String[] args) {

    byte[] b = {20,30,40,50,60,70,80,90,100};
    ByteArrayInputStream by = new ByteArrayInputStream(b);
    
    System.out.println("Available number of bytes to read: " + by.available());
    System.out.println(by.read());
    System.out.println(by.read());
    System.out.println(by.read());
    System.out.println("Available number of bytes to read: " + by.available());

  }

}
Available number of bytes to read: 9
20
30
40
Available number of bytes to read: 6

 

Reference

Array Interview Questions
Graph Interview Questions
LinkedList Interview Questions
String Interview Questions
Tree Interview Questions
Core Java Interview Questions