How to Convert InputStream to Byte Array in Java?
In Java, input stream refers to an ordered flow of data in the form of bytes. This flow of data can be coming from various resources such as files, network programs, input devices, etc. In order to read such data, we have a Java InputStream Class in the Java IO API. There are several methods to convert this input stream into a byte array (or byte) which can be used as and when required. We’ll now have a look at some methods to do the same which are listed as follows:
- Using read(byte) or readAllBytes()
- Using ByteArrayOutputStream Class
- Using ByteStreams utility class
- Using Apache Commons IO Library
Now we will be discussing each and every method to detail in order and understanding the implementation the same via clean java programs as follows:
Method 1: Using read(byte) or readAllBytes()
In the InputStream class, we have a read() method where a byte array can be passed as a parameter to get the input stream data in the form of a byte array. But this method has a shortcoming that it can read the data at most the size of the array passed as a parameter. It works well only if we know the size of incoming data beforehand. The method returns an int value equal to the number of bytes read into the array or -1 if the stream end is reached.
public int read(byte byteArray) throws IOException
To overcome the drawback of having to know the input size beforehand, we have another method called readAllBytes() since Java 9. This method can read all bytes available in the input stream. The method is way more faster and efficient in converting input stream to a byte array.
public byte readAllBytes() throws IOException
G e e k s F o r G e e k s
Example 2: Using ByteArrayOutputStream Class
This is an indirect method of conversion of input stream data into a byte array. Here we will be using an object of ByteArrayOutputStream class as a buffer. For this, we read each byte from a InputStream class and write it to a ByteArrayOutputStreamclass. Later we call toByteArray() that returns the output stream in the form of a byte array.
G e e k s F o r G e e k s
Method 3: Using ByteStreams utility class
ByteStreams utility class from the Guava Library has a direct method to convert input stream to a byte array.
Google Guava is an open-source(a decentralized software-development model that encourages open collaboration) set of common libraries for Java, mainly developed by Google engineers. It helps in reducing coding errors. It provides utility methods for collections, caching, primitives support, concurrency, common annotations, string processing, I/O, and validations.
Method 4: Using Apache Commons IO Library
This method is very similar to using ByteStreams class. Here we make use of IOUtils class which has a similar method with the same name toByteArray() that returns the inputstream data as a byte array. The usage same we just need to use import org.apache.commons.io.IOUtils rather that ByteStreams of Guava Library.
byte byteArray = IOUtils.toByteArray(inputStream);
Note: However, this method often needs the addition of dependency to the editor.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.