Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert an input stream to byte array in java?
The InputStream class in Java provides read() method. This method accepts a byte array and it reads the contents of the input stream to the given byte array.
To convert an InputStream to a byte array, We can use the ByteArrayOutputStream class to read data from the InputStream and write it into a byte array.
Methods to Convert InputStream to Byte Array
Let's compare the methods to convert InputStream to Byte Array in the below table ?
| Method | Simplicity | Performance | Ease of Use | Recommended For |
|---|---|---|---|---|
| Direct InputStream.read | Low | Good | Moderate | Simple use cases, small fixed-size data |
| Apache Commons IOUtils.toByteArray | High | Good | Very Easy | Simple, robust utility functions |
| ByteArrayOutputStream | High | Good | Easy | General purpose, flexible buffer size |
To convert an input stream to byte array in Java is quite easy. Let us learn the following methods:
Converting InputStream to Byte Array
This method demonstrates how to read data from the InputStream and convert it into a byte array, then print the contents of the byte stream as a string using a BufferedInputStream.
Example
The program reads data from the standard input using a BufferedInputStream and stores it in a byte array of size 1024. Then it converts the byte array to a string.
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
public class StreamToByteArray {
public static void main(String args[]) throws IOException{
InputStream is = new BufferedInputStream(System.in);
byte [] byteArray = new byte[1024];
System.out.println("Enter some data");
is.read(byteArray);
String s = new String(byteArray);
System.out.println("Contents of the byte stream are :: "+ s);
}
}
Output
Following is the output of the above program ?
Enter some data hello how are you Contents of the byte stream are :: hello how are you
Alternative Solution Using Apache Commons IOUtils
Apache Commons provides a library named org.apache.commons.io and, the following is the maven dependency to add the library to your project.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
This package provides a class known as IOUtils. the toByteArray () method of this class accepts an InputStream object and returns the contents in the stream in the form of a byte array:
Example
Following is another example that reads the contents of a file named "data" into a byte array using FileInputStream and the Apache Commons IO IOUtils.toByteArray() method. It converts the byte array to a string.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.IOUtils;
public class StreamToByteArray2IOUtils {
public static void main(String args[]) throws IOException{
File file = new File("data");
FileInputStream fis = new FileInputStream(file);
byte [] byteArray = IOUtils.toByteArray(fis);
String s = new String(byteArray);
System.out.println("Contents of the byte stream are :: "+ s);
}
}
Output
The above program produce the following result ?
Contents of the byte stream are :: hello how are you
Converting InputStream to Byte Array Using OutputStream
The following method is used to read data from an InputStream, converting it into a byte array using OutputStream.
Example
In this program, we convert an 'InputStream' to a byte array using 'ByteArrayOutputStream'. It reads data from the input stream, writes it to the output stream, and then converts the accumulated data into a byte array as a string.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class InputStreamToByteArray {
public static byte[] convertInputStreamToByteArray(InputStream inputStream) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int bytesRead;
byte[] data = new byte[1024];
while ((bytesRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, bytesRead);
}
buffer.flush();
return buffer.toByteArray();
}
public static void main(String[] args) {
String exampleInput = "Hello, World!";
InputStream inputStream = new ByteArrayInputStream(exampleInput.getBytes());
try {
byte[] byteArray = convertInputStreamToByteArray(inputStream);
System.out.println("Contents of the byte stream are :: " + new String(byteArray));
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
The above program produce the following result ?
Contents of the byte stream are :: Hello, World!