Get the bytes of the string:
obj.toString().getBytes(theCharset);
Answer from MByD on Stack OverflowGitHub
github.com › FasterXML › jackson-databind › issues › 3493
Can't serialize `ByteArrayOutputStream` value when using jackson 2.13.0 · Issue #3493 · FasterXML/jackson-databind
May 24, 2022 - ByteArrayOutputStream bos = new ByteArrayOutputStream() ObjectMapper om = objectMapper(); String json = om.writeValueAsString(bos); public static ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.setVisibilit...
Published May 24, 2022
Author JavaRukawa
InformIT
informit.com › articles › article.aspx
Processing Input and Output | 9.1. Input/Output Streams, Readers, and Writers | InformIT
var out = new ByteArrayOutputStream(); Write to out byte[] bytes = out.toByteArray();
Wordpress
myadventuresincoding.wordpress.com › 2020 › 07 › 11 › java-using-jackson-jsongenerator-to-write-json-to-an-outputstream
Java – Using Jackson JsonGenerator to write JSON to an OutputStream | My Adventures in Coding
August 8, 2021 - import com.fasterxml.jackson.core.JsonEncoding; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import java.io.ByteArrayOutputStream; public class JsonGeneratorExample { public static void main(String[] args) throws Exception { // Create the OutputStream we will be writing JSON to ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); // Create a background thread to print out the contents of the OutputStream PrintOutputStreamRunnable runnable = new PrintOutputStreamRunnable(outputStream); Thread thread = new Thread(runnable); thread.s
Top answer 1 of 5
113
Create a ByteArrayOutputStream.
Grab its content by calling toByteArray()
CopyByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.writeTo(myOutputStream);
baos.toByteArray();
Reference
2 of 5
7
If the OutputStream object supplied is not already a ByteArrayOutputStream, one can wrap it inside a delegate class that will "grab" the bytes supplied to the write() methods, e.g.
Copypublic class DrainableOutputStream extends FilterOutputStream {
private final ByteArrayOutputStream buffer;
public DrainableOutputStream(OutputStream out) {
super(out);
this.buffer = new ByteArrayOutputStream();
}
@Override
public void write(byte b[]) throws IOException {
this.buffer.write(b);
super.write(b);
}
@Override
public void write(byte b[], int off, int len) throws IOException {
this.buffer.write(b, off, len);
super.write(b, off, len);
}
@Override
public void write(int b) throws IOException {
this.buffer.write(b);
super.write(b);
}
public byte[] toByteArray() {
return this.buffer.toByteArray();
}
}
To reduce the overhead, the calls to super in the above class can be omitted - e.g., if only the "conversion" to a byte array is desired.
A more detailed discussion can be found in another StackOverflow question.
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java Platform SE 8 )
March 16, 2026 - Closing a ByteArrayOutputStream has no effect.
Stack Overflow
stackoverflow.com › questions › 54109268 › getting-json-string-with-streaming-gson-with-bytearrayoutputstream-gets-outofmem
android - Getting JSON String with streaming GSON with ByteArrayOutputStream gets OutOfMemoryError - Stack Overflow
January 9, 2019 - @Nullable private static String streamBlaModelsIntoJsonString(List<BlaModel> blaModels) { try { Gson gson = new Gson(); ByteArrayOutputStream out = new ByteArrayOutputStream(); JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8")); writer.setIndent(" "); writer.beginArray(); for (BlaModel blaModel : blaModels) { gson.toJson(blaModel, BlaModel.class, writer); } writer.endArray(); writer.close(); return out.toString("UTF-8"); } catch (IOException e) { e.printStackTrace(); } return null; }
GitHub
github.com › FasterXML › jackson-databind › issues › 3522
Support serializing `ByteArrayOutputStream` as "simple" Binary value · Issue #3522 · FasterXML/jackson-databind
June 20, 2022 - As a result, ByteArrayOutputStream values are no longer serializable. But it might make sense to make it serializable again. However, the representation as POJO does not make sense: if serialization is to be used, it should: Use public ByteArrayOutStream.toByteArray() to access content · Serialize contents as a Binary value: for JSON backend this means Base64-encoded String; for other backends whatever their representation of Binary values is ·
Programiz
programiz.com › java-programming › bytearrayoutputstream
Java ByteArrayOutputStream (With Examples)
The ByteArrayOutputStream class of the java.io package can be used to write an array of output data (in bytes).
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)
This is an alternative implementation of the ByteArrayOutputStream class. The original implementation only allocates 32 bytes at the beginning. As this class is designed for heavy duty it starts at 1024 bytes. In contrast to the original it doesn't reallocate the whole memory block but allocates ...
Javatpoint
javatpoint.com › java-bytearrayoutputstream-class
Java ByteArrayOutputStream Class - javatpoint
Java ByteArrayOutputStream Class for beginners and professionals with examples on Java IO or Input Output in Java with input stream, output stream, reader and writer class. The java.io package provides api to reading and writing data.
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 - Creates a new ByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.