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.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

Leave a Reply


× eight = 16



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books