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
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ByteArrayOutputStream.html
ByteArrayOutputStream (Java Platform SE 8 )
1 week ago - Closing a ByteArrayOutputStream has no effect.
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 ·
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 ...
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; }
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).
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.
Top answer 1 of 3
63
byte[] bytes = ....;
ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length);
baos.write(bytes, 0, bytes.length);
Method description:
Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
2 of 3
0
You can't display a ByteArrayOutputStream. What I suspect you are trying to do is
byte[] bytes = ...
String text = new String(bytes, "UTF-8"); // or some other encoding.
// display text.
You can make ByteArrayOutputStream do something similar but this is not obvious, efficient or best practice (as you cannot control the encoding used)