How to Convert Byte Array to InputStream and OutputStream in Java

Are you stuck with your coding because you have a byte array and next method in chain needs an InputStream? don’t worry Java has solution for that, You can use
ByteArrayInputStream to convert byte array to InputStream in Java. This class takes a byte array as source and since it’s a sub-class of InputStream, you can easily pass this to any method, which accepts InputStream as parameter. Though most of the API like JDBC and File API allows you to read directly from InputStream, because this allows you to process an arbitrary content with limited heap space. You should take advantage of this and directly read from InputStream instead of getting byte array and then converting them back to InputStream. It’s only for those situation, where you have a legacy code, which is no more maintained and updated. Similarly converting byte array to OutputStream is trivial. Since we use OutputStream to write something, it allows you to directly write byte array in it. Say you have got some messages from TCP socket and want to persist in file system, you can use OutputStream and FileOutputStream to write byte array directly. Earlier we had seen how to convert InputStream to byte array, In this article, we will see opposite of that by creating a simple example of converting byte array to InputStream in action.

 

Byte Array to InputStream and OutputStream

Here is our sample program, which first take a byte array from String for testing purpose. Anyway, always provide character encoding when converting String to bytes and vice-versa. To recreate scenario, I have created a static method, which converts an InputStream to String. This method is called streamToString(), which takes an InputStream and character encoding for reading text. We recreate the same String, which we had previously converted into byte array. Next part of this example shows, how can you write a byte array to an OutputStream. By the way, if you are not using try-with-resource statement, then don’t forget to close Streams once you are through with it. It can be argued that, whether a method, which accepts an InputStream should close it or not, but I have closed it to be on safe side. Let me know what is your thought on this, because there is example of IO utility classes in both Apache Commons IO and Google Guava which closes stream and which doesn’t close the stream passed to them.

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
/**
 * Java Program to convert byte array to InputStream and OutputStream in Java.
 * Uses ByteArrayInputStream and ByteArrayOutputStream examples.
 * @author Javin Paul
 */
public class ByteArrayToStream {
    public static void main(String args[]) {
        String str = "Google is GOD";
        byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
// Creating InputStream from byte array
        // ByteArrayInputStream is sub-class of InputStream
        InputStream is = new ByteArrayInputStream(bytes);
        String stringFromBytes = streamToString(is, StandardCharsets.UTF_8);
        System.out.println("String recreated from bytes : " + str);
// Writing byte array to OutputStream
        OutputStream os = new ByteArrayOutputStream();
        try {
            os.write(bytes);
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*
     * Read String from InputStream and closes it
     */
    public static String streamToString(InputStream is, Charset encoding) {
        BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding));
        StringBuilder sb = new StringBuilder(1024);
        try {
            String line = br.readLine();
            while (line != null) {
                sb.append(line);
                line = br.readLine();
            }
        } catch (IOException io) {
            System.out.println("Failed to read from Stream");
            io.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException ioex) {
                System.out.println("Failed to close Streams");
                ioex.printStackTrace();
            }
        }
        return sb.toString();
    }
}
Output:
String recreated from bytes : Google is GOD

You can see how our streamToString() method has converted InputStream to String, but real thing is before that, we have used ByteArrayInputStream to convert our byte array to InputStream in Java. As I said ByteArrayInputStream is subclass of InputStream, you can pass or use it whenever a InputStream is required. This is also clear from class hierarchy diagram taken from java.io package, which shows different implementation of InputStream from JDK library.

InputStream Java

That’s all about how to convert byte array to InputStream in Java. It might sound difficult initially, because of limited knowledge of java.io package, but it’s become very simple once you know there is a class called ByteArrayInputStream. Since it’s a child class of InputStream, you can pass it around in place of InputStream. Don’t forget to provide character encoding if you are converting bytes to characters and close streams once you are done with them.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


× four = 16



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close