A ByteArrayOutputStream can read from any InputStream and at the end yield a byte[].

However with a ByteArrayInputStream it is simpler:

int n = in.available();
byte[] bytes = new byte[n];
in.read(bytes, 0, n);
String s = new String(bytes, StandardCharsets.UTF_8); // Or any encoding.

For a ByteArrayInputStream available() yields the total number of bytes.


Addendum 2021-11-16

Since java 9 you can use the shorter readAllBytes.

byte[] bytes = in.readAllBytes();

Answer to comment: using ByteArrayOutputStream

ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[8192];
for (;;) {
    int nread = in.read(buf, 0, buf.length);
    if (nread <= 0) {
        break;
    }
    baos.write(buf, 0, nread);
}
in.close();
baos.close();
byte[] bytes = baos.toByteArray();

Here in may be any InputStream.


Since java 10 there also is a ByteArrayOutputStream#toString(Charset).

String s = baos.toString(StandardCharsets.UTF_8);
Answer from Joop Eggen on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java io › java inputstream to string
Java InputStream to String | Baeldung
January 5, 2024 - Let’s see how we can make use of it to convert an InputStream to a String: @Test public void givenUsingJava8_whenConvertingAnInputStreamToAString_thenCorrect() { String originalString = randomAlphabetic(DEFAULT_SIZE); InputStream inputStream = new ByteArrayInputStream(originalString.getBytes()); String text = new BufferedReader( new InputStreamReader(inputStream, StandardCharsets.UTF_8)) .lines() .collect(Collectors.joining("\n")); assertThat(text, equalTo(originalString)); } It’s important to mention that lines() uses the readLine() method under the hood.
🌐
Tutorialspoint
tutorialspoint.com › java › io › bytearrayoutputstream_tostring_string.htm
Java - ByteArrayOutputStream toString(String charsetName) method
The Java ByteArrayOutputStream toString(String charsetName) method converts the stream's contents using the specified charsetName. The malformed-input and unmappable-character sequences are replaced by the default replacement string for the
People also ask

Is it safe to convert a large InputStream to a String?
It depends. For large data, avoid reading all at once. Read it in chunks or line-by-line using BufferedReader to prevent memory issues or slow performance in Java applications.
🌐
wscubetech.com
wscubetech.com › resources › java › programs › inputstream-to-string
Convert InputStream to String in Java (5 Programs)
What’s the best way to convert InputStream into String in Java?
The best way depends on your needs. For quick solutions, Scanner is handy. For more control, use BufferedReader or InputStreamReader to read line by line and build a string.
🌐
wscubetech.com
wscubetech.com › resources › java › programs › inputstream-to-string
Convert InputStream to String in Java (5 Programs)
Can I convert InputStream to String without third-party libraries?
Yes, Java has built-in tools like BufferedReader, InputStreamReader, and Scanner. You don’t need Apache Commons or other libraries unless you want more features or cleaner one-liners.
🌐
wscubetech.com
wscubetech.com › resources › java › programs › inputstream-to-string
Convert InputStream to String in Java (5 Programs)
🌐
Coderanch
coderanch.com › t › 278987 › java › convert-Bytearray-input-stream-String
how to convert a Bytearray input stream to String (I/O and Streams forum at Coderanch)
April 26, 2008 - Hey,venkata satya: you can do it ... s=br.readLine(); i wish it helps you. ... You can also create a ByteArrayOutputStream from the ByteArrayInputStream, and then use the toString() method to create the String equivalent....
🌐
Stack Abuse
stackabuse.com › convert-inputstream-into-a-string-in-java
Convert InputStream into a String in Java
March 16, 2023 - InputStream to String with InputStream.readAllBytes() - Best Approach ... Since Java 9, this process has been simplified a lot. The InputStream is very commonly instantiated as a ByteArrayInputStream, before toByteArray() is called to retrieve the bytes from it.
🌐
Online String Tools
onlinestringtools.com › convert-bytes-to-string
Convert Bytes to a String – Online String Tools
Free online bytes to a string converter. Just load your byte array in the input area and it will automatically get converted to a string. There are no intrusive ads, popups or nonsense, just a neat converter. Load bytes – get a string.
🌐
BeginnersBook
beginnersbook.com › 2013 › 12 › how-to-convert-inputstream-to-string-in-java
How To Convert InputStream To String In Java
September 11, 2022 - 5) Finally converted the StringBuilder to String using toString() method. import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; public class Example { public static void main(String[] args) ...
Find elsewhere
🌐
GitHub
github.com › clj-commons › byte-streams › issues › 30
Bytes are read from stream to String incorrectly · Issue #30 · clj-commons/byte-streams
May 21, 2017 - I have a ByteArrayInputStream b that contains Chinese characters in UTF-8. I've found that I get bad data from byte-streams when I do this: (byte-streams/convert b String) I get a string in which some characters are corrupt (2 out of a f...
Published   May 21, 2017
Author   joelittlejohn
🌐
WsCube Tech
wscubetech.com › resources › java › programs › inputstream-to-string
Convert InputStream to String in Java (5 Programs)
October 18, 2025 - Learn 5 simple ways to convert an InputStream to a String in Java with examples. Explore BufferedReader, Scanner, readAllBytes(), and more. Read now!
🌐
LabEx
labex.io › tutorials › java-convert-inputstream-to-string-117396
Convert InputStream to String in Java | LabEx
The InputStream class introduced the readAllBytes() method in Java 9. This can efficiently convert the entire InputStream object to a string in a single line of code. However, this method is not recommended for larger inputs.
🌐
Baeldung
baeldung.com › home › kotlin › kotlin io › inputstream to string in kotlin
InputStream to String in Kotlin | Baeldung on Kotlin
April 19, 2024 - As the String to InputStream conversion is a pretty common use case, Kotlin’s standard library has wrapped the conversion logic in the String extension byteInputStream(): @kotlin.internal.InlineOnly public inline fun String.byteInputStream(charset: Charset = Charsets.UTF_8): ByteArrayInputStream = ByteArrayInputStream(toByteArray(charset))
🌐
GeeksforGeeks
geeksforgeeks.org › java › bytearrayoutputstream-tostring-method-in-java-with-examples
ByteArrayOutputStream toString() method in Java with Examples - GeeksforGeeks
July 15, 2025 - GEEKS 2. The toString(String charsetName) method of ByteArrayOutputStream class in Java is used convert the buffer content of the ByteArrayOutputStream into the string using specified charsetName which is passed as string to this method.
🌐
Codemia
codemia.io › knowledge-hub › path › how_do_i_turn_a_string_into_a_inputstreamreader_in_java
How do I turn a String into a InputStreamReader in java?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › StringBufferInputStream.html
StringBufferInputStream (Java SE 21 & JDK 21)
January 20, 2026 - The index of the next character to read from the input stream buffer. ... Deprecated. The number of valid characters in the input stream buffer. ... Deprecated. Creates a string input stream to read data from the specified string.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-string-to-inputstream
Java Program to Convert String to InputStream - GeeksforGeeks
June 12, 2021 - Given a string, the task is to convert the string to InputStream which is shown in the below illustrations. ... In order to reach the goal, we need to use ByteArrayInputStream.
🌐
TestMu AI Community
community.testmu.ai › ask a question
How to convert a String to an InputStream in Java? - TestMu AI Community
March 24, 2025 - How to convert a String to an InputStream in Java? I have a string: String exampleString = "example"; What is the best way to java string to inputstream conversion?
🌐
Programiz
programiz.com › java-programming › bytearrayinputstream
Java ByteArrayInputStream (With Examples)
In this tutorial, we will learn about Java ByteArrayInputStream and its methods with the help of examples to read an array of input data.