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 › article › 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....
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › io › InputStream.html
InputStream (Java SE 9 & JDK 9 )
The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, the skip method for class InputStream always returns 0, and no bytes are skipped.
🌐
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; } }
🌐
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.
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - Get the answers and suggestions you need from our AI code assistant. Get started in minutes with a free 90 day trial of Tabnine Pro.
Find elsewhere
🌐
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
🌐
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 17 & JDK 17)
April 21, 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....
🌐
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 › en › java › javase › 17 › docs › api › java.base › java › io › ByteArrayInputStream.html
ByteArrayInputStream (Java SE 17 & JDK 17)
April 21, 2026 - readAllBytes in class InputStream · Returns: a byte array containing the bytes read from this input stream · public int readNBytes · (byte[] b, int off, int len) Description copied from class: InputStream · Reads the requested number of bytes from the input stream into the given 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....
🌐
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.
🌐
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; ...
🌐
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
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8194957
[JDK-8194957] new method InputStream.readAllBytes(int n ...
Add to InputStream a method readAllBytes(int) which returns an array containing at most the number of bytes specified.
🌐
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