Tuesday, April 8, 2014

Convert Input Stream to ByteArray

Converting the Input Stream to Byte Array is very simple. You can org.apache.commons.io.IOUtils to get the job done quickly.

The IOUtils contains a method toByteArray which takes in a InputStream and converts the input stream into a byte Array.


byte[] byteArray = IOUtils.toByteArray(in);

How to change Byte array byte[] to InputStream



The byte array can be converted into the input stream using the java.io.ByteArrayInputStream class. The ByteArrayInputStream class extends the InputStream class.

In the constructor of the ByteArrayInputStream just pass the byte array (byte[]) which you want to convert into InputStream.

InputStream stream = new ByteArrayInputStream(myBytes);