You can use Apache Commons IO to handle this and similar tasks.

The IOUtils type has a static method to read an InputStream and return a byte[].

InputStream is;
byte[] bytes = IOUtils.toByteArray(is);

Internally this creates a ByteArrayOutputStream and copies the bytes to the output, then calls toByteArray(). It handles large files by copying the bytes in blocks of 4KiB.

Answer from Rich Seller on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › java inputstream to byte array and bytebuffer
Java InputStream to Byte Array and ByteBuffer | Baeldung
January 8, 2024 - We are going to take a look at how to convert a simple input stream to a byte[] – first using plain Java, then using Guava and Apache Commons IO. Let’s start with a Java solution focused on dealing with a fixed size stream: @Test public void givenUsingPlainJavaOnFixedSizeStream_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException { InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 }); byte[] targetArray = new byte[is.available()]; is.read(targetArray); }
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-inputstream-to-byte-array-in-java
How to Convert InputStream to Byte Array in Java? - GeeksforGeeks
July 23, 2025 - This is an indirect method of ... ByteArrayOutputStream class as a buffer. For this, we read each byte from a InputStream class and write it to a ByteArrayOutputStreamclass....
🌐
Programiz
programiz.com › java-programming › examples › inputstream-to-bytearray-conversion
Java Program to Convert the InputStream into Byte Array
Here, the readAllBytes() method returns all the data from the stream and stores in the byte array. Note: We have used the Arrays.toString() method to convert all the entire array into a string. import java.io.InputStream; import java.util.Arrays; import java.io.ByteArrayInputStream; import ...
🌐
Blogger
javarevisited.blogspot.com › 2013 › 04 › how-to-convert-inputstream-to-byte-array-java-example-tutorial.html
How to Convert InputStream to Byte Array in Java - 2 Examples
June 11, 2025 - Earlier we have seen 5 ways to convert InputStream to String in Java, we can use some of the techniques from there while getting the yte array from InputStream in Java. If you like to use Apache commons library, which I think you should, there is a utility class called IOUtils, which can be used to easily convert InputStream to byte array in Java.
🌐
Vultr Docs
docs.vultr.com › java › examples › convert-the-inputstream-into-byte-array
Java Program to Convert the InputStream into Byte Array | Vultr Docs
December 19, 2024 - Use the transferTo method from an InputStream directly into a ByteArrayOutputStream. Convert the ByteArrayOutputStream's content to a byte array.
🌐
W3Docs
w3docs.com › java
Convert InputStream to byte array in Java
The readAllBytes() method reads all the bytes from an InputStream and returns them as a byte array. Here is an example of how you can use the readAllBytes() method to convert an InputStream to a byte array in Java:
Find elsewhere
🌐
amitph
amitph.com › home › java › how to convert inputstream to byte[]
How to Convert InputStream to byte[] - amitph
November 22, 2024 - This article illustrates different ways to Convert InputStream to a byte[] (or Byte Array) using Apache Common IO, Guava, and Plain Java.
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-an-input-stream-to-byte-array-in-java
How to convert an input stream to byte array in java?
December 26, 2024 - 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.
🌐
Delft Stack
delftstack.com › home › howto › java › java inputstream to byte array
How to Convert Inputstream to Byte Array in Java | Delft Stack
February 2, 2024 - This tutorial introduces how to convert inputstream to byte array in Java and lists some example codes to understand the topic.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java Platform SE 8 )
April 21, 2026 - Reads up to len bytes of data into an array of bytes from this input stream. If pos equals count, then -1 is returned to indicate end of file. Otherwise, the number k of bytes read is equal to the smaller of len and count-pos. If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied ...
🌐
Gregbugaj
gregbugaj.com › proper-way-to-read-inputstream-to-byte-array
Proper way to read InputStream to byte array – Adventures of a Software Engineer
for (byte[] tmp = new byte[255], read = new byte[Integer.BYTES], buffer = {}; new BigInteger( read = BigInteger.valueOf(inputStream.read(tmp)).toByteArray()).intValue() > -1 || buffer.length > 0; buffer = new byte[Math.max(0,new BigInteger(read).intValue())], System.arraycopy(tmp, 0, buffer, 0, buffer.length)) { System.out.print(new String(buffer)); }
🌐
Tutorialspoint
tutorialspoint.com › java › java_bytearrayinputstream.htm
Java - ByteArrayInputStream
Following is the example to demonstrate ByteArrayInputStream and ByteArrayOutputStream. import java.io.*; public class ByteStreamTest { public static void main(String args[])throws IOException { ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12); while( bOutput.size()!= 10 ) { // Gets the inputs from the user bOutput.write("hello".getBytes()); } byte b [] = bOutput.toByteArray(); System.out.println("Print the content"); for(int x = 0 ; x < b.length; x++) { // printing the characters System.out.print((char)b[x] + " "); } System.out.println(" "); int c; ByteArrayInputStream bInput = new ByteArrayInputStream(b); System.out.println("Converting characters to Upper case " ); for(int y = 0 ; y < 1; y++) { while(( c = bInput.read())!= -1) { System.out.println(Character.toUpperCase((char)c)); } bInput.reset(); } } }
🌐
Coderunbox
coderunbox.com › example › example.java › inputstream-to-bytearray-conversion
Java Program to Convert the InputStream into Byte Array | CodeRunBox - Code Example
To understand this example, you ... System.out.println("Input Stream: " + stream); // convert the input stream to byte array byte[] array = stream.readAllBytes(); System.out.println("Byte Array: " + Arrays.toString(array)); stream.close(); ...
🌐
Jenkov
jenkov.com › tutorials › java-io › inputstream.html
Java InputStream
October 7, 2020 - The BufferedInputStream reads a chunk of bytes into a byte array from the underlying InputStream. You can then read the bytes one by one from the BufferedInputStream and still get a lot of the speedup that comes from reading an array of bytes ...
🌐
GitHub
gist.github.com › ke4ktz › 80c8f92439a23763fb39
Convert InputStream to Byte Array - Java - Gist - GitHub
Java: Convert InputStream to Byte Array. GitHub Gist: instantly share code, notes, and snippets.
🌐
RoseIndia
roseindia.net › java › java-conversion › InputStreamToByteArrayInputStream.shtml
Convert Inputstream to ByteArrayInputStream
In this example we are going to convert InputStream to ByteArrayInputStream. To do so first read the file as input stream using FileInputStream. Then convert this input stream into string and then string into byte by using the toString() and getBytes() methods respectively.
🌐
GitHub
gist.github.com › ph1ee › a633983aab3a9e1ca0fe
convert InputStream to byte array #java · GitHub
convert InputStream to byte array #java. GitHub Gist: instantly share code, notes, and snippets.