How to Convert InputStream to byte[]

This article illustrates different ways to Convert InputStream to a byte[] (or Byte Array) using Apache Common IO, Guava, and Plain Java.

Overview

The InputStream is a byte stream, which we read from almost anything in Java. Many ways are available to convert an InputStream to a byte[] or a ByteBuffer. In this tutorial, we will cover them one by one.

Before we do that, let’s quickly have a look at how to create an InputStream instance from a byte[] (Byte Array)

Converting a Byte Array to an InputStream

Let’s see a simple example of converting a byte[] to an InputStream in plain Java. We create a byte[] instance from a String object and use it to create an instance of the ByteArrayInputStream, a subclass of InputStream.

byte[] bytes = string.getBytes(UTF_8);
InputStream inputStream = 
    new ByteArrayInputStream(bytes);Code language: Java (java)

In the next part, we will see various ways of Converting an InputStream to a byte[].

InputStream to Byte Array Using Apache Commons IO

The Apache Commons IO Library provides many useful abstractions for basic File IO operations in Java. Next is a very simple and short way of creating byte[] from an InputStream.

Example of using Apache Commons IO to convert an InputStream to byte[]

byte[] bytes = IOUtils
    .toByteArray(inputStream);Code language: Java (java)

InputStream to Byte Array Using Guava Library

Similarly, the Guava Library also provides a simple and concise way. We can use the ByteStreams utility class, as shown in the following example. The toByteArray() method returns a byte[] containing all the bytes from the given InputStream.
Example of using Guava to convert an InputStream to byte[]

byte[] bytes = ByteStreams
    .toByteArray(inputStream);Code language: Java (java)

InputStream to Byte Array Using Plain Java

The Java InputStream provides the read(byte[]) method, which copies all available bytes into the provided array of bytes. However, to use this method, we need to provide an array of the exact size.

And to do that, we read the number of available bytes from the InputStream instance and initialize a byte[] of the same size.

Example of using plain Java to convert an InputStream to a byte[].

int size = inputStream.available();
byte[] bytes = new byte[size];
inputStream.read(bytes);Code language: Java (java)

This solution works for an InputStream of a fixed size. However, if the InputStream is a BufferedInputStream, the available() method returns the current buffer size and not the size of all bytes.

For such a BufferedInputStream instance, we can use the readAllBytes() method that returns the size of all bytes.

byte[] bytes = inputStream.readAllBytes();Code language: Java (java)

InputStream to ByteArray Manually

We saw several ways of converting an InputStream to a Byte Array, and they used different abstractions to simplify the process. However, if we want more control over the size of the buffer and the amount of memory being used, we can do the conversion manually.

We can iteratively read a fixed number of bytes from the InputStream instance and consume them before reading the next set of bytes.

Example of converting an InputSteram to byte[] manually.

ByteArrayOutputStream byteArrayOutputStream = 
    new ByteArrayOutputStream();

byte[] bucket = new byte[1024];
int nReadBytes;
while((nReadBytes = inputStream
    .read(bucket, 0, bucket.length)) !=-1){
  byteArrayOutputStream.write(bucket, 0, nReadBytes);
}

byte[] bytes = 
    byteArrayOutputStream.toByteArray();Code language: Java (java)

Firstly, we create an instance of the ByteArrayOutputStream, which is the consumer of our byte[]. Next, we make a byte[] bucket of a fixed size. During each iteration, we fill the bucket (of fixed size) from the InputStream and write it onto the ByteArrayOutputStream. And continue to do so until no more byes are left in the InputStream.

Summary

In this short tutorial, we covered different ways of Converting an InputStream to an array of bytes. We covered examples of using Apache Commons IO Library, Guava Library, and a couple of examples using Core Java. For more on Java, please visit Java Tutorials.

Please refer to our GitHub Repository for the complete source code of the examples.