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

The documentation says that you should not use the available() method to determine the size of an InputStream . How can I read the entire contents of an InputStream into an array of bytes?

 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 several times into a buffer of a fixed size, but then I would have to combine the data that I read into one byte array, which is a problem for me.

+46
java inputstream
Aug 30 '11 at 21:50
source share
6 answers

The simplest IMO approach 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 do not want to use Guava), you can create a ByteArrayOutputStream and repeatedly read into a byte array and write to ByteArrayOutputStream (allowing you to resize the handle) and then call ByteArrayOutputStream.toByteArray() .

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

+62
Aug 30 2018-11-21T00:
source share
— -

Remember that the answers given here assume that the file length is less than or equal to Integer.MAX_VALUE (2147483647).

If you are reading 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 to the java.nio.file package, which you can use to make this example a few lines shorter. See the readAllBytes () method in the java.nio.file.Files class. Here is a quick 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 with Api Level 26 (8.0.0, Oreo).

+56
Aug 30 2018-11-21T00:
source share

You can use Apache commons-io for this task:

Refer to this method :

 public static byte[] readFileToByteArray(File file) throws IOException 

Update:

Java 7 :

 byte[] bytes = Files.readAllBytes(Paths.get(filename)); 

and if it is a text file, and you want to convert it to String (change the encoding if necessary):

 StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString() 
+9
Aug 30 2018-11-21T00:
source share

I believe that the length of the buffer should be indicated, since the memory is finite, and you can run out of it

Example:

 InputStream in = new FileInputStream(strFileName); long length = fileFileName.length(); if (length > Integer.MAX_VALUE) { throw new IOException("File is too large!"); } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + fileFileName.getName()); } in.close(); 
+5
Aug 30 2018-11-21T00:
source share

You can read it in pieces ( byte buffer[] = new byte[2048] ) and write the pieces in ByteArrayOutputStream. From ByteArrayOutputStream, you can get the contents as a byte [] without hesitation in determining its size in advance.

+5
Aug 30 2018-11-21T00:
source share

The maximum value for the array index is Integer.MAX_INT - about 2 GB (2 ^ 31/2 147 483 647). Your input stream may be more than 2 GB, so you have to process the data in pieces, sorry.

  InputStream is; final byte[] buffer = new byte[512 * 1024 * 1024]; // 512Mb while(true) { final int read = is.read(buffer); if ( read < 0 ) { break; } // do processing } 
+3
Aug 30 2018-11-21T00:
source share



All Articles