1. Introduction
In this tutorial, we are going to learn how to convert an InputStream to a byte array using plain Java and external libraries like Guava or Apache Commons IO. In many cases, there is a need to use byte[]
instead of Strings mainly because processing arrays is fast and not resource consuming.
For more Java I/O related articles, check the following links:
2. Convert InputStream to byte array using ByteArrayOutputStream
Natural way to convert an InputStream to byte array is to use plan Java solution based on ByteArrayOutputStream
:
package com.frontbackend.java.io.conversions;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamToByteArrayUsingByteArrayOutputStream {
public static void main(String[] args) throws IOException {
InputStream initialStream = new ByteArrayInputStream(new byte[] { 102, 114, 111, 110, 116, 98, 97, 99, 107, 101, 110, 100 }); // example input stream
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = initialStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] byteArray = buffer.toByteArray();
}
}
In this example, we created ByteArrayOutputStream
and write to it blocks of read data from the InputStream
. Then, we used the toByteArray()
method to get wanted byte array.
BTW. we use example list of bytes that represent our domain name:
System.out.println(new String(new byte[]{102, 114, 111, 110, 116, 98, 97, 99, 107, 101, 110, 100})); // frontbackend
3. InputStream to byte[]
using ByteArrayInputStream
When we have the opportunity to use ByteArrayInputStream
, instead of any other different InputStream
, this will be the best way to get byte array out stream. This is because ByteArrayInputStream
contains method read
that takes byte[
package com.frontbackend.java.io.conversions;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamToByteArrayUsingByteArrayInputStream {
public static void main(String[] args) throws IOException {
InputStream initialStream = new ByteArrayInputStream(new byte[] { 102, 114, 111, 110, 116, 98, 97, 99, 107, 101, 110, 100 }); // example input stream
byte[] targetArray = new byte[initialStream.available()];
initialStream.read(targetArray);
}
}
4. Conversion from InputStream to a byte array using Guava
To convert InputStream to byte[]
we can also use external libraries like Guava
. The Guava
based solution using ByteStreams
utility class looks like the following:
package com.frontbackend.java.io.conversions;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.google.common.io.ByteStreams;
public class InputStreamToByteArrayUsingGuava {
public static void main(String[] args) throws IOException {
InputStream initialStream = new ByteArrayInputStream(new byte[] { 102, 114, 111, 110, 116, 98, 97, 99, 107, 101, 110, 100 });
byte[] targetArray = ByteStreams.toByteArray(initialStream);
}
}
To start working with Guava
we should include special dependency to our project:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
</dependency>
The latest version of Guava
library is available here:
5. Convert Using Apache Commons IO
Apache Commons IO
comes with similar to Guava
solution to convert an InputStream to a byte array:
package com.frontbackend.java.io.conversions;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
public class InputStreamToByteArrayUsingIOUtils {
public static void main(String[] args) throws IOException {
InputStream initialStream = new ByteArrayInputStream(new byte[] { 102, 114, 111, 110, 116, 98, 97, 99, 107, 101, 110, 100 });
byte[] byteArray = IOUtils.toByteArray(initialStream);
}
}
Of course, first we need to include Apache Commons IO dependency to our project or download JAR
. The following presents the way we could include library in POM.xml
file:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
The latest version of the library could be found here: Maven Apache Commons IO 2.6
6. Conclusion
This article presented several ways to convert an InputStream
to byte array
in Java using built-in methods from Java IO and using libraries like: Guava
or Apache Commons IO
.
The examples illustrated in this tutorial can be found in our GitHub project.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}