Convert InputStream to byte array in Java
This post will discuss how to convert InputStream
to byte array in Java.
1. Using java.io.ByteArrayOutputStream
The idea is to read each byte from the specified InputStream
and write it to a ByteArrayOutputStream
, then call toByteArray()
to get the current contents of this output stream as a byte array. Since the total number of bytes to be read is unknown, we have allocated the buffer of size 1024.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; // Convert `InputStream` to byte array in Java class Main { public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; // read bytes from the input stream and store them in the buffer while ((len = in.read(buffer)) != -1) { // write bytes from the buffer into the output stream os.write(buffer, 0, len); } return os.toByteArray(); } public static void main(String[] args) throws IOException { // input stream InputStream in = new ByteArrayInputStream("Techie Delight" .getBytes(StandardCharsets.UTF_8)); // byte array byte[] bytes = toByteArray(in); System.out.println(new String(bytes)); } } |
Output:
Techie Delight
2. Using Guava Library
We can use ByteStreams class from Guava, whose toByteArray()
method reads all bytes from an input stream into a byte array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import com.google.common.io.ByteStreams; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; // Convert `InputStream` to byte array in Java using Guava class Main { public static byte[] toByteArray(InputStream in) throws IOException { byte[] bytes = ByteStreams.toByteArray(in); return bytes; } public static void main(String[] args) throws IOException { // input stream InputStream in = new ByteArrayInputStream("Techie Delight" .getBytes(StandardCharsets.UTF_8)); // byte array byte[] bytes = toByteArray(in); System.out.println(new String(bytes)); } } |
Output:
Techie Delight
3. Using Apache Commons IO
Like Guava, Apache Commons IO has IOUtils class whose toByteArray()
method returns the contents of an InputStream
as a byte array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import org.apache.commons.io.IOUtils; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; // Convert `InputStream` to byte array in Java using Apache Commons class Main { public static byte[] toByteArray(InputStream in) throws IOException { byte[] bytes = IOUtils.toByteArray(in); return bytes; } public static void main(String[] args) throws IOException { // input stream InputStream in = new ByteArrayInputStream("Techie Delight" .getBytes(StandardCharsets.UTF_8)); // byte array byte[] bytes = toByteArray(in); System.out.println(new String(bytes)); } } |
Output:
Techie Delight
4. Using sun.misc.IOUtils
We can also use sun.misc.IOUtils
, which is one of Sun’s proprietary Java
classes. We should avoid their use as it is not guaranteed to work on all Java-compatible platforms or even in future versions on the same platform.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import sun.misc.IOUtils; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; // Convert `InputStream` to byte array in Java class Main { public static byte[] toByteArray(InputStream in) throws IOException { byte[] bytes = IOUtils.readFully(in, -1, false); return bytes; } public static void main(String[] args) throws IOException { // input stream InputStream in = new ByteArrayInputStream("Techie Delight" .getBytes(StandardCharsets.UTF_8)); // byte array byte[] bytes = toByteArray(in); System.out.println(new String(bytes)); } } |
Output:
Techie Delight
5. Using Java 9
In Java 9, we can do something like:
1 2 3 |
public static byte[] toByteArray(InputStream in) throws IOException { return in.readAllBytes(); } |
Or use ByteArrayOutputStream.transferTo()
method:
1 2 3 4 5 6 7 |
public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); in.transferTo(bos); return bos.toByteArray(); } |
That’s all about converting InputStream to byte array in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)