Here's one way to solve this without relying on third-party libraries:

inputStream.reset();
byte[] bytes = new byte[inputStream.available()];
DataInputStream dataInputStream = new DataInputStream(inputStream);
dataInputStream.readFully(bytes);

Or if you don't mind using thirdparties (Commons IO):


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

Guava also helps:

byte[] bytes = ByteStreams.toByteArray(inputStream);
Answer from Mark Bramnik on Stack Overflow
🌐
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 - Returns a new InputStream that reads no bytes. The returned stream is initially open. The stream is closed by calling the close() method. Subsequent calls to close() have no effect. While the stream is open, the available(), read(), read(byte[]), read(byte[], int, int), readAllBytes(), ...
🌐
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(); } } }
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 21 & JDK 21)
January 20, 2026 - Returns a new InputStream that ... up to len bytes of data from the input stream into an array of bytes. byte[] readAllBytes() 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 - @Test public void givenUsingPlainJava9_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException { InputStream is = new ByteArrayInputStream(new byte[] { 0, 1, 2 }); byte[] data = is.readAllBytes(); } Let’s now look at the simple Guava based solution – using the convenient ByteStreams utility class: @Test public void givenUsingGuava_whenConvertingAnInputStreamToAByteArray_thenCorrect() throws IOException { InputStream initialStream = ByteSource.wrap(new byte[] { 0, 1, 2 }).openStream(); byte[] targetArray = ByteStreams.toByteArray(initialStream); } And finally – a straight
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8194957
new method InputStream.readAllBytes(int n) to read up to n ...
* * @param len the maximum number of bytes to read * @return a byte array containing the bytes read from this input stream * @throws IndexOutOfBoundsException if {@code length} is negative * @throws IOException if an I/O error occurs * @throws OutOfMemoryError if an array of the required size cannot be * allocated. For example, if an array larger than {@code 2GB} would * be required to store the bytes. * * @since 11 */ public byte[] readAllBytes(int len) throws IOException {}
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › io › InputStream.html
InputStream (Java SE 9 & JDK 9 )
Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input · A subclass must provide an implementation of this method
🌐
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
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › test › jdk › java › io › InputStream › ReadAllBytes.java
openjdk-jdk11/test/jdk/java/io/InputStream/ReadAllBytes.java at master · AdoptOpenJDK/openjdk-jdk11
Mirror of the jdk/jdk11 Mercurial forest at OpenJDK - openjdk-jdk11/test/jdk/java/io/InputStream/ReadAllBytes.java at master · AdoptOpenJDK/openjdk-jdk11
Author   AdoptOpenJDK
🌐
Program Creek
programcreek.com › java-api-examples
Java Code Examples for java.io.InputStream#readAllBytes()
@Override public void handle(HttpExchange he) throws IOException { String method = he.getRequestMethod(); InputStream is = he.getRequestBody(); if (method.equalsIgnoreCase("POST")) { String requestBody = new String(is.readAllBytes(), US_ASCII); if (!requestBody.equals(POST_BODY)) { he.sendResponseHeaders(500, -1); ok = false; } else { he.sendResponseHeaders(200, -1); ok = true; } } else { // GET he.sendResponseHeaders(200, RESPONSE.length()); OutputStream os = he.getResponseBody(); os.write(RESPONSE.getBytes(US_ASCII)); os.close(); ok = true; } }
🌐
Jenkov
jenkov.com › tutorials › java-io › inputstream.html
Java InputStream
The Java InputStream class contains a method called readAllBytes() (since Java 9). This method reads all the bytes available in the InputStream and returns a single byte array with the bytes in.
🌐
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
* Returns a new {@code InputStream} that reads no bytes. The returned · * stream is initially open. The stream is closed by calling the · * {@code close()} method. Subsequent calls to {@code close()} have no · * effect. * * <p> While the stream is open, the {@code available()}, {@code read()}, * {@code read(byte[])}, {@code read(byte[], int, int)}, * {@code readAllBytes()}, {@code readNBytes(byte[], int, int)}, * {@code readNBytes(int)}, {@code skip(long)}, and ·
Author   AdoptOpenJDK
🌐
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 - If you are working with JDK 9 (or later), you are in luck as the readAllBytes() method was added in the InputStream class. This method provides one-line solution to convert an InputStream to a byte array.
🌐
Java
download.java.net › java › early_access › valhalla › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 23 & JDK 23 [build 1])
Returns a new InputStream that ... up to len bytes of data from the input stream into an array of bytes. byte[] readAllBytes() Reads all remaining bytes from the input stream....
🌐
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 - To overcome the drawback of having to know the input size beforehand, we have another method called readAllBytes() since Java 9. This method can read all bytes available in the input stream.
🌐
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 17 & JDK 17)
January 20, 2026 - Returns a new InputStream that ... up to len bytes of data from the input stream into an array of bytes. byte[] readAllBytes() Reads all remaining bytes from the input stream....
🌐
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