JavaTechnology

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. There are a plenty of ways 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 how to create an InputStream instance from a byte[] (Byte Array)

Convert Byte Array to InputStream

We create a byte[] from a plain String and use it to create ByteArrayInputStream, which is a subclass of InputStream.

byte[] bytes = string.getBytes(UTF_8);
InputStream inputStream = new ByteArrayInputStream(bytes);

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

Using Apache Commons IO Library

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

byte[] bytes = IOUtils.toByteArray(inputStream);

Using Guava Library

Similar to that, the Guava Library also provides a simple and concise way. We can make use of the ByteStreams utility class, as shown next.

byte[] bytes = ByteStreams.toByteArray(inputStream);

Using Plain Java

The InputStream provides a read(byte[]) method, which copies all available bytes array 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 can read the number of available bytes from the input stream and initialise an array bucket of the same size. Then, we can provide the bucket to the read method.

int size = inputStream.available();
byte[] bytes = new byte[size];
inputStream.read(bytes);

This works correctly when the InputStream has a fixed size.

However, in case of a buffered stream, the the available method returns the size of current buffer. Thus, we cannot ascertain the size of the total bytes. For such Buffered Streams we can use readAllBytes method provided by the InputStream.

Next, is example of reading all bytes from an InputStream.

byte[] bytes = inputStream.readAllBytes();

The plain java examples we covered so far, make use of simple abstractions provided by the Java File IO. However, without using these abstractions we can still do the conversion by iteratively reading a fixed numbers of bytes from the InputStream and copying into a buffer. Next example shows it practically.

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();

Let’s understand what is happening in the code block.

First, we create an instance of ByteArrayOutputStream, which will serve a purpose of a buffer. Next, we create a byte[] bucket of a fixed size. During each iteration, we fill the bucket (of fixed size) from the InputStream and write onto the buffer. And continue to do so until no more byes are left into the input stream.

Note that, the read method on the InputStream takes the bucket, the initial position and a number indicating how many bytes a required to be copied. The method returns a number of bytes actually copied during the operation.

Finally, once all of the byes are written on the buffer, we convert it to a array of bytes.

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.