In programming when we need to send a file over the network, we need to transfer the file as a byte array. Many times we need to send streams of bytes over the network.

In this tutorial, we will see how to convert InputStream to Byte Array in java. There are different ways to convert InputStream to Byte Array. We will see them one by one.

Table of Content :

1). Using Core Java

In the case of core java, We have to read each byte from the InputStream and write it to a ByteArrayOutputStream then call toByteArray() to get the current contents output stream, as a byte array. As the number of bytes to be read is unknown, we have allocated the buffer of size 1024.

package com.javacodestuffs.io.stream.inputstream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class ByteArrayUsingCoreJava { public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } return outputStream.toByteArray(); } public static void main(String[] args) throws IOException { InputStream inputStream = new ByteArrayInputStream( "Hello World!!! Welcome to java programming !!!".getBytes(StandardCharsets.UTF_8)); byte[] bytes = toByteArray(inputStream); System.out.println("the converted bytes are: "); System.out.println(new String(bytes)); } } output: the converted bytes are: Hello World!!! Welcome to java programming !!!

2). InputStream to Byte Array Conversion Using Google Guava

The Guava has ByteStreams class which contains the toByteArray method which reads all bytes from an input stream and converts it into a byte array.

Maven Dependency

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -- > <dependency > <groupId >com.google.guava </groupId > <artifactId >guava </artifactId > <version >29.0-jre </version > </dependency >

package com.javacodestuffs.io.stream.inputstream; import com.google.common.io.ByteStreams; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class ByteArrayUsingGuava { public static byte[] toByteArray(InputStream in) throws IOException { byte[] bytes = ByteStreams.toByteArray(in); return bytes; } public static void main(String[] args) throws IOException { InputStream in = new ByteArrayInputStream( "Hello World!!! Welcome to java programming !!!".getBytes(StandardCharsets.UTF_8)); byte[] bytes = toByteArray(in); System.out.println("the converted bytes are: "); System.out.println(new String(bytes)); } } output: the converted bytes are: Hello World!!! Welcome to java programming !!!

3). InputStream to Byte Array Conversion Using Commons IO

Apache Commons IO has IOUtils.contains toByteArray method convert InputStream into a byte array.

Maven Dependency

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -- > <dependency > <groupId >commons-io </groupId > <artifactId >commons-io </artifactId > <version >2.8.0 </version > </dependency >

package com.javacodestuffs.io.stream.inputstream; import org.apache.commons.io.IOUtils; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class ByteArrayUsingApacheIO { public static void main(String[] args) throws IOException { InputStream in = new ByteArrayInputStream( "Hello World!!! Welcome to java programming !!!".getBytes(StandardCharsets.UTF_8)); byte[] bytes = IOUtils.toByteArray(in); System.out.println("the converted bytes are: "); System.out.println(new String(bytes)); } } output: the converted bytes are: Hello World!!! Welcome to java programming !!!

4). InputStream to Byte Array Conversion Using Java 8

In java 8 we have new way to convert it.We need to use BufferedReader and Collectors.

private static byte[] getBytes(InputStream inputStream) throws IOException { try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream))) { return buffer.lines().collect(Collectors.joining("\n")).getBytes(StandardCharsets.UTF_8); } }

package com.javacodestuffs.io.stream.inputstream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.stream.Collectors; public class ByteArrayUsingJava8 { public static void main(String[] args) throws IOException { InputStream in = new ByteArrayInputStream( "Hello World!!! Welcome to java programming !!!".getBytes(StandardCharsets.UTF_8)); byte[] bytes = getBytes(in); System.out.println("the converted bytes are: "); System.out.println(new String(bytes)); } private static byte[] getBytes(InputStream inputStream) throws IOException { try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream))) { return buffer.lines().collect(Collectors.joining("\n")).getBytes(StandardCharsets.UTF_8); } } }

Post/Questions related to Convert InputStream to Byte Array in Java

Read file to byte[] array in Java How to convert the input stream to byte array in java?
Create New File in Java
How to convert byte array to input stream in java?
Which of the below stream is used to write data to any destination byte by byte?
How do you create an InputStream from a byte array?

In this article, we have seen InputStream to Byte Array Conversion in Java. You can download code from GitHub as well..