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); }
🌐
Programiz
programiz.com › java-programming › examples › inputstream-to-bytearray-conversion
Java Program to Convert the InputStream into Byte Array
Build the coding confidence you need to become a developer companies will fight for · Stop copy pasting code you don't actually understand ... Become a certified Java programmer. ENROLL ... Created with over a decade of experience. ... Created with over a decade of experience and thousands of feedback. ... Try Programiz PRO! ... Become a certified Java programmer. Try Programiz PRO! ... import java.io.InputStream; import java.util.Arrays; import java.io.ByteArrayInputStream; public class Main { public static void main(String args[]) { try { // create an input stream byte[] input = {1, 2, 3, 4
🌐
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....
🌐
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(); ...
🌐
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:
🌐
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.
🌐
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.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 276360 › java › Convert-InputStream-byte-array
Convert InputStream to byte array (I/O and Streams forum at Coderanch)
October 24, 2003 - However, it is necessary to be sure that the ByteArrayOutputStream is not used again, once its array has been accessed by other code. Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma. ... Thanks for your response Peter. Following your suggestion I came up with: InputStream in=xmlClob().getAsciiStream(); int c; while ((c = in.read()) != -1) { byteArrayOutputStream.write((char) c); } ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); But as you had mentioned, the memory constraint remains this way.
🌐
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.
🌐
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(); } } }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java Platform SE 8 )
March 16, 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 ...
🌐
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.
🌐
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.
🌐
Programiz
programiz.com › java-programming › bytearrayinputstream
Java ByteArrayInputStream (With Examples)
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.
🌐
Jenkov
jenkov.com › tutorials › java-io › bytearrayinputstream.html
Java ByteArrayInputStream
November 15, 2019 - The Java ByteArrayInputStream is used to read bytes from a byte array as if the bytes were read from a stream.
🌐
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.
🌐
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.