Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a binary input stream into a single byte array in Java

The documentation says that one should not use available() method to determine the size of an InputStream. How can I read the whole content of an InputStream into a byte array?

InputStream in; //assuming already present byte[] data = new byte[in.available()]; in.read(data);//now data is filled with the whole content of the InputStream 

I could read multiple times into a buffer of a fixed size, but then, I will have to combine the data I read into a single byte array, which is a problem for me.

like image 214
Qiang Li Avatar asked Aug 30 '11 21:08

Qiang Li


People also ask

How do you read all bytes from InputStream?

Since Java 9, we can use the readAllBytes() method from InputStream class to read all bytes into a byte array. This method reads all bytes from an InputStream object at once and blocks until all remaining bytes have read and end of a stream is detected, or an exception is thrown.

How do you convert an InputStream to Bytearray in java?

Example 1: Java Program to Convert InputStream to Byte Arraybyte[] array = stream. readAllBytes(); Here, the readAllBytes() method returns all the data from the stream and stores in the byte array. Note: We have used the Arrays.

What is byte array input stream in java?

The ByteArrayInputStream class of the java.io package can be used to read an array of input data (in bytes). It extends the InputStream abstract class. Note: In ByteArrayInputStream , the input stream is created using the array of bytes. It includes an internal array to store data of that particular byte array.


2 Answers

The simplest approach IMO is to use Guava and its ByteStreams class:

byte[] bytes = ByteStreams.toByteArray(in); 

Or for a file:

byte[] bytes = Files.toByteArray(file); 

Alternatively (if you didn't want to use Guava), you could create a ByteArrayOutputStream, and repeatedly read into a byte array and write into the ByteArrayOutputStream (letting that handle resizing), then call ByteArrayOutputStream.toByteArray().

Note that this approach works whether you can tell the length of your input or not - assuming you have enough memory, of course.

like image 184
Jon Skeet Avatar answered Sep 24 '22 06:09

Jon Skeet


Please keep in mind that the answers here assume that the length of the file is less than or equal to Integer.MAX_VALUE(2147483647).

If you are reading in from a file, you can do something like this:

    File file = new File("myFile");     byte[] fileData = new byte[(int) file.length()];     DataInputStream dis = new DataInputStream(new FileInputStream(file));     dis.readFully(fileData);     dis.close(); 

UPDATE (May 31, 2014):

Java 7 adds some new features in the java.nio.file package that can be used to make this example a few lines shorter. See the readAllBytes() method in the java.nio.file.Files class. Here is a short example:

import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path;  // ...         Path p = FileSystems.getDefault().getPath("", "myFile");         byte [] fileData = Files.readAllBytes(p); 

Android has support for this starting in Api level 26 (8.0.0, Oreo).

like image 35
President James K. Polk Avatar answered Sep 23 '22 06:09

President James K. Polk