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 )
March 16, 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 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.
🌐
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.
🌐
TutorialsPoint
tutorialspoint.com › when-to-use-the-readallbytes-method-of-inputstream-in-java-9
When to use the readAllBytes() method of InputStream in Java 9?
June 12, 2025 - import java.nio.*; import java.nio.file.*; import java.io.*; import java.util.stream.*; import java.nio.charset.StandardCharsets; public class ReadAllBytesMethodTest { public static void main(String args[]) { try(InputStream stream = Files.newInputStream(Paths.get("C://Temp//Technology.txt"))) { // Convert stream to string String contents = new String(stream.readAllBytes(), StandardCharsets.UTF_8); // To print the string content System.out.println(contents); } catch(IOException ioe) { ioe.printStackTrace(); } } }
🌐
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 - Using the same approach as the above sections, we’re going to take a look at how to convert an InputStream to a ByteBuffer – first using plain Java, then using Guava and Commons IO. In the case of a byte stream – we know the exact size of the underlying data. Let’s use the ByteArrayInputStream#available method to read the byte stream into a ByteBuffer:
🌐
Tabnine
tabnine.com › home page › code › java › java.io.inputstream
java.io.InputStream.readAllBytes java code examples | Tabnine
/** * Convert to response, with body as byte array */ public Response<byte[]> toBinaryResponse() { return handle((charset, ri) -> ri.body().readAllBytes()); }
Find elsewhere
🌐
Programiz
programiz.com › java-programming › examples › inputstream-to-bytearray-conversion
Java Program to Convert the InputStream into Byte Array
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}; InputStream stream = new ByteArrayInputStream(input); 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(); } catch (Exception e) { e.getStackTrace(); } } }
🌐
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 - @Test public void givenUsingJava9_whenConvertingAnInputStreamToAString_thenCorrect() throws IOException { String originalString = randomAlphabetic(DEFAULT_SIZE); InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); String text = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); assertThat(text, equalTo(originalString)); } We need to be aware that this simple code is intended for simple cases where it’s convenient to read all bytes into a byte array. We shouldn’t use it for reading input streams with large amounts of data. Next, let’s look at a plain Java example using a standard text Scanner:
🌐
Linux Hint
linuxhint.com › java-input-stream-read-all-bytes
Java Input Stream ReadAllBytes
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
🌐
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
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › src › java.base › share › classes › java › io › InputStream.java
openjdk-jdk11/src/java.base/share/classes/java/io/InputStream.java at master · AdoptOpenJDK/openjdk-jdk11
* @see java.io.InputStream#reset() */ public boolean markSupported() { return false; } · /** * Reads all bytes from this input stream and writes the bytes to the · * given output stream in the order that they are read.
Author   AdoptOpenJDK
🌐
Mkyong
mkyong.com › home › java › how to convert inputstream to string in java
How to convert InputStream to String in Java - Mkyong.com
February 13, 2022 - // @Java 9 -> inputStream.readAllBytes() // max bytes Integer.MAX_VALUE, 2147483647, which is 2G private static String convertInputStreamToString(InputStream inputStream) throws IOException { return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); } 2.2 Review the InputStream source code; it reads all bytes into a byte array, which has a limit of Integer.MAX_VALUE or 2G; Since then, it is not intended for reading input streams with large amounts of data.
🌐
GitHub
github.com › aws › aws-sdk-java-v2 › issues › 4274
InputStreamSubscriber changes read() behaviour when passed len=0. Breaks InputStream.readAllBytes · Issue #4274 · aws/aws-sdk-java-v2
August 8, 2023 - ResponseInputStream.readAllBytes() -> InputStream.readAllBytes() ->InputStream.readNBytes(2147483647) -> FilterInputStream.read(byte[8192] ,0, 8192) -> InputStreamSubscriber.read(byte[8192], 0, 8192)
Author   mgrundie-r7
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8194957
new method InputStream.readAllBytes(int n) to read up to n ...
Limiting the number of bytes to ... array allocation for the full remaining size of the stream. Add to InputStream a method readAllBytes(int) which returns an array containing at most the number of bytes specified....
🌐
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.