In this short tutorial, we are going to showcase how to convert byte array to InputStream in Java.

First, we will see how to to do that using plain Java. Then, we are going to demonstrate how to use Guava and Apache Commons libraries to achieve the same purpose.

Byte Array to InputStream using Java

This section explains how to turn a byte array into an InputStream using standard Java.

Java provides ByteArrayInputStream to read an array of bytes as an InputStream.

This class extends the InputStream interface and comes with an overloaded constructor that accepts a byte array as argument:

    
        @Test
        public void convertByteArrayToInputStreamUsingJava() throws IOException {
            byte[] byteArray = { 'a', 'b', 'c' };
            InputStream targetStream = new ByteArrayInputStream(byteArray);
            
            assertEquals(targetStream.read(byteArray), byteArray.length);
        }
    

As shown above, ByteArrayInputStream takes a byte[] as a parameter and produce an object of InputStream type.

Convert an Array of Byte to InputStream using Guava

Now that we know how to convert an array of bytes into an instance of InputStream type, let’s see how to use the Guava library to accomplish the same thing.

Guava offers a convenient way to wrap a byte[] into a readable source of bytes with the help of ByteSource.

This class comes with a handy method called openStream() to return a new, independent stream:

    
        @Test
        public void convertByteArrayToInputStreamUsingGuava() throws IOException {
                
            byte[] bytes = "devwithus.com".getBytes(StandardCharsets.UTF_8);
                InputStream targetStream = ByteSource.wrap(bytes)
                                                     .openStream();
                
            assertEquals(targetStream.available(), bytes.length);
        }
    

openStream() opens a new InputStream to read data from the source.

Please bear in mind that the caller is responsible for ensuring that the returned stream is closed.

byte[] to InputStream With Apache Commons IO

Apache Commons IO provides another great way to convert a byte array to InputStream.

This library comes with a handy utility class called IOUtils for stream manipulation.

So, let’s see how we can use this class to accomplish byte[] to stream conversion:

    
        @Test
        public void convertByteArrayToInputStreamUsingApacheIo() throws IOException {
                
            byte[] initBytes = "azhwani".getBytes(StandardCharsets.UTF_8);
            InputStream in = IOUtils.toInputStream(new String(initBytes), StandardCharsets.UTF_8);
                
            byte[] resBytes = IOUtils.toByteArray(in);
                
            assertArrayEquals(resBytes, initBytes);
        }
    

First, we need to create a string object from our byte array. Then, we can use the toInputStream() method to convert our string to an input stream.

We need to keep in mind that the given string is encoded as bytes using the specified character encoding.

Conclusion

In this quick article, we covered various ways of converting byte array to InputStream in Java.

Along the way, we showed how to achieve that using core Java and third-party libraries such as Guava and Apache Commons.