I would use a ByteArrayOutputStream. And on finish you can call:

new String( baos.toByteArray(), codepage );

or better:

baos.toString( codepage );

For the String constructor, the codepage can be a String or an instance of java.nio.charset.Charset. A possible value is java.nio.charset.StandardCharsets.UTF_8.

The method toString() accepts only a String as a codepage parameter (stand Java 8).

Answer from Horcrux7 on Stack Overflow
🌐
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
🌐
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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java Platform SE 8 )
March 16, 2026 - Converts the buffer's contents into a string by decoding the bytes using the named charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.bytearrayoutputstream.tostring
ByteArrayOutputStream.ToString Method (Java.IO) | Microsoft Learn
Converts the buffer's contents into a string by decoding the bytes using the specified java.nio.charset.Charset charset.
🌐
Apache Commons
commons.apache.org › proper › commons-io › javadocs › api-2.5 › org › apache › commons › io › output › ByteArrayOutputStream.html
ByteArrayOutputStream (Apache Commons IO 2.5 API)
ByteArrayOutputStream.toByteArray() @Deprecated public String toString() Deprecated. 2.5 use toString(String) instead · Gets the curent contents of this byte stream as a string using the platform default charset. Overrides: toString in class Object · Returns: the contents of the byte array ...
🌐
Vultr Docs
docs.vultr.com › java › examples › convert-outputstream-to-string
Java Program to Convert OutputStream to String | Vultr Docs
December 17, 2024 - This class implements an output stream in which the data is written into a byte array, which can then be converted to a string. Write some data to the ByteArrayOutputStream.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java Platform SE 7 )
Converts the buffer's contents into a string by decoding the bytes using the specified charsetName. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.
Find elsewhere
🌐
Coderanch
coderanch.com › t › 329194 › java › ByteArrayOutputStream-String
ByteArrayOutputStream to String (Java in General forum at Coderanch)
What is the SQL datatype of the field, and how did you get the data into a ByteArrayOutputStream? I'm guessing it was a BLOB, in which case it's irrelevant that the database uses UTF-8 - you need to know what encoding was used by whoever created the BLOB. ... I think your problem is that java Strings are unicode format and utf8 is not.
🌐
Oracle
docs.oracle.com › javase › 9 › docs › api › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java SE 9 & JDK 9 )
Converts the buffer's contents into a string by decoding the bytes using the named charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-convert-outputstream-to-string
Java Program to Convert OutputStream to String - GeeksforGeeks
July 23, 2025 - The java.io.ByteArrayOutputStream.toString() method converts the stream using the character set. ... Create an object of ByteArrayoutputStream. Create a String variable and initialize it.
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java SE 11 & JDK 11 )
January 20, 2026 - Converts the buffer's contents into a string by decoding the bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java SE 10 & JDK 10 )
Converts the buffer's contents into a string by decoding the bytes using the specified charset. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.
🌐
Bureau of Economic Geology
beg.utexas.edu › lmod › agi.servlet › doc › detail › java › io › ByteArrayOutputStream.html
java.io Class ByteArrayOutputStream
Converts the buffer's contents into a string, translating bytes into characters according to the specified character encoding.
🌐
Programiz
programiz.com › java-programming › bytearrayoutputstream
Java ByteArrayOutputStream (With Examples)
In this tutorial, we will learn about Java ByteArrayOutputStream and its methods with the help of examples to write an array of output data.
🌐
TechVidvan
techvidvan.com › tutorials › java-bytearrayoutputstream-class
Java ByteArrayOutputStream Class with Examples - TechVidvan
May 2, 2024 - It is employed to transform the material into a string by utilizing a given charsetName to decode the bytes. To save the data, ByteArrayOutputStream keeps an internal array of bytes.
🌐
W3Docs
w3docs.com › java
Get an OutputStream into a String
Here's an example of how to use the toString(String charsetName) method to get a string in a specific character encoding: import java.io.ByteArrayOutputStream; import java.io.OutputStream; public class Main { public static void main(String[] ...
🌐
GitHub
github.com › openjdk-mirror › jdk7u-jdk › blob › master › src › share › classes › java › io › ByteArrayOutputStream.java
jdk7u-jdk/src/share/classes/java/io/ByteArrayOutputStream.java at master · openjdk-mirror/jdk7u-jdk
* @see java.io.ByteArrayOutputStream#count · */ public synchronized int size() { return count; } · /** * Converts the buffer's contents into a string decoding bytes using the · * platform's default character set. The length of the new <tt>String</tt> * is a function of the character set, and hence may not be equal to the ·
Author   openjdk-mirror