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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › InputStream.html
InputStream (Java Platform SE 8 )
April 21, 2026 - The general contract of mark is that, if the method markSupported returns true, the stream somehow remembers all the bytes read after the call to mark and stands ready to supply those same bytes again if and whenever the method reset is called. However, the stream is not required to remember any data at all if more than readlimit bytes are read from the stream before reset is called. Marking a closed stream should not have any effect on the stream. The mark method of InputStream does nothing.
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › io › InputStream.html
InputStream (Java SE 9 & JDK 9 )
The general contract of mark is that, if the method markSupported returns true, the stream somehow remembers all the bytes read after the call to mark and stands ready to supply those same bytes again if and whenever the method reset is called. However, the stream is not required to remember any data at all if more than readlimit bytes are read from the stream before reset is called. Marking a closed stream should not have any effect on the stream. The mark method of InputStream does nothing.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 11 & JDK 11 )
January 20, 2026 - The close method of InputStream does nothing. ... Marks the current position in this input stream. A subsequent call to the reset method repositions this stream at the last marked position so that subsequent reads re-read the same bytes. The readlimit arguments tells this input stream to allow that many bytes to be read before the mark position gets invalidated.
🌐
amitph
amitph.com › home › java › how to convert inputstream to byte[]
How to Convert InputStream to byte[] - amitph
November 22, 2024 - For such a BufferedInputStream instance, we can use the readAllBytes() method that returns the size of all bytes. byte[] bytes = inputStream.readAllBytes();Code language: Java (java)
🌐
Baeldung
baeldung.com › home › java › java io › java inputstream to string
Java InputStream to String | Baeldung
January 5, 2024 - Here we’re using the java.nio.file.Files class to create a temporary file, as well as to copy the content of the InputStream to the file. Then the same class is used to convert the file content to a String with the readAllBytes() method.
🌐
Linux Hint
linuxhint.com › java-input-stream-read-all-bytes
Linux Hint – Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
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 - We can use the readAllBytes() method to get all the data into a byte array. This method returns a byte array that can be passed further into the String constructor to print textual data. import java.io.FileInputStream; import java.io.IOException; ...
Find elsewhere
🌐
CodingTechRoom
codingtechroom.com › question › java-read-all-bytes-inputstream-java-1-8-below
How to Read All Bytes from an InputStream in Java 1.8 and Earlier - CodingTechRoom
import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.IOException; public byte[] readAllBytes(InputStream inputStream) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; // Buffer size int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, bytesRead); } return byteArrayOutputStream.toByteArray(); } Lack of native support for readAllBytes() in Java 1.8 and below.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.inputstream.readallbytes
InputStream.ReadAllBytes Method (Java.IO) | Microsoft Learn
[<Android.Runtime.Register("readAllBytes", "()[B", "GetReadAllBytesHandler", ApiSince=33)>] abstract member ReadAllBytes : unit -> byte[] override this.ReadAllBytes : unit -> byte[] ... Reads all remaining bytes from the input stream.
🌐
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 - In this quick tutorial, we’re going to take a look at how to convert an InputStream to a byte[] and ByteBuffer – first using plain Java, then using Guava and Commons IO.
🌐
Sentry
sentry.io › sentry answers › java › how do i read / convert an inputstream into a string in java?
How do I Read / Convert an InputStream into a String in Java? | Sentry
July 12, 2022 - Without the readAllBytes() method, the most efficient alternative is to make use of a ByteArrayOutputStream and using InputStream.read() to read chunks of data at a time as shown in the example below:
🌐
Dotnetcodr
dotnetcodr.com › 2016 › 09 › 03 › various-ways-to-read-bytes-from-an-input-stream-in-java
Various ways to read bytes from an input stream in Java | Exercises in .NET with Andras Nemes
September 3, 2016 - int bytesExtracted = 0; int bytesToExtract = 20; byte[] inputThree = new byte[bytesToExtract]; List<Byte> byteList = new ArrayList<>(); while (bytesExtracted < bytesToExtract) { int temporaryBytesReadCount = inputStream.read(inputThree, bytesExtracted, bytesToExtract - bytesExtracted); if (temporaryBytesReadCount == -1) { break; } for (int i = 0; i < temporaryBytesReadCount; i++) { byteList.add(inputThree[i]); } } View all posts related to Java networking here.
🌐
Jenkov
jenkov.com › tutorials › java-io › inputstream.html
Java InputStream
Here is an InputStream read() example: ... To read all bytes in a Java InputStream you must keep reading until the value -1 is returned. This value means that there are no more bytes to read from the InputStream.
🌐
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 - InputStream inputStream = new ByteArrayInputStream( "GeeksForGeeks".getBytes( StandardCharsets.UTF_8)); // Taking the InputStream data into a byte array // output stream // Buffer size taken to be 1000 say. byte[] buffer = new byte[1000]; // Creating an object of ByteArrayOutputStream class ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // Try block to check for exceptions try { int temp; while ((temp = inputStream.read(buffer)) != -1) { byteArrayOutputStream.write(buffer, 0, temp); } } // Catch block to handle the exceptions catch (IOException e) { // Display the e
🌐
o7planning
o7planning.org › 13527 › java-inputstream
Java InputStream Tutorial with Examples | o7planning.org
package org.o7planning.inputstream.ex; import java.io.IOException; import java.io.InputStream; import java.net.URL; public class InputStream_readAllBytes_ex1 { public static void main(String[] args) throws IOException { String url = "https://s3.o7planning.com/txt/utf8-file-without-bom.txt"; InputStream is = new URL(url).openStream(); byte[] allBytes = is.readAllBytes(); String content = new String(allBytes, "UTF-8"); System.out.println(content); is.close(); } }
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java Platform SE 8 )
April 21, 2026 - The methods in this class can be called after the stream has been closed without generating an IOException. ... An array of bytes that was provided by the creator of the stream. Elements buf[0] through buf[count-1] are the only bytes that can ever be read from the stream; element buf[pos] is ...
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java SE 17 & JDK 17)
April 21, 2026 - Unlike the overridden method of InputStream, this method returns -1 instead of zero if the end of the stream has been reached and len == 0. This read method cannot block. ... the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. ... NullPointerException - If b is null. IndexOutOfBoundsException - If off is negative, len is negative, or len is greater than b.length - off ... Reads all remaining bytes from the input stream.