Jackson: ObjectMapper closes stream after writing value
See original GitHub issueCurrently the ObjectMapper closes the stream which means that you can’t write two values to the underlying stream which mean that the following fails because the stream has been closed
PipedOutputStream pipedOutputStream = new PipedOutputStream();
PipedInputStream pipedInputStream = new PipedInputStream();
pipedInputStream.connect(pipedOutputStream);
ArrayList<Object> list = new ArrayList<>();
ObjectMapper objectMapper = new ObjectMapper(new MessagePackFactory());
objectMapper.writeValue(pipedOutputStream, list);
objectMapper.writeValue(pipedOutputStream, list);
with an IOException saying the pipe closed (same happens with socket)
java.io.IOException: Pipe closed
at java.io.PipedInputStream.checkStateForReceive(PipedInputStream.java:260)
at java.io.PipedInputStream.receive(PipedInputStream.java:226)
at java.io.PipedOutputStream.write(PipedOutputStream.java:149)
at org.msgpack.core.buffer.OutputStreamBufferOutput.flush(OutputStreamBufferOutput.java:46)
at org.msgpack.core.MessagePacker.flush(MessagePacker.java:134)
at org.msgpack.core.MessagePacker.close(MessagePacker.java:143)
at org.msgpack.jackson.dataformat.MessagePackGenerator.close(MessagePackGenerator.java:344)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:2883)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:2304)
For now I’m writing to a temporary array and then writing that value to the stream, but that seems inefficient.
byte[] buffer = objectMapper.writeValueAsBytes(packet);
OutputStream output = <from somewhere>
output.write(buffer);
output.flush();
Would it be possible to leave the stream open after writing the value?
Issue Analytics
- State:
- Created 8 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
How to fix Stream Closed Exception - java - Stack Overflow
I think Jackson is auto closing your stream after the call to writeValue . You can turn this off via: MessagePackFactory messagePackFactory ...
Read more >ObjectMapper (jackson-databind 2.6.0 API) - FasterXML
Method that can be used to serialize any Java value as JSON output, using output stream provided (using encoding JsonEncoding.UTF8 ). void, writeValue(Writer...
Read more >Proper resource management examples
In this example, the object mapper used to parse the input stream has been explicitly configured to close the stream in a finally...
Read more >Java - Write to File - Baeldung
In output stream, the close() method calls flush() before releasing the resources, which forces any buffered bytes to be written to the stream....
Read more >com.fasterxml.jackson.core.JsonGenerator.close java code ...
JsonGenerator.close (Showing top 20 results out of 1,989) ... JSONBean.write(...) ... Serialize a list of objects to a JSON String.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Leaving a comment for future Googlers looking for a solution to the auto-closed stream:
Adding to @borice comment to address @Sergey123456 issue, which I also encountered: In addition to
JsonGenerator.Feature.AUTO_CLOSE_TARGET
, which controlsOutputStream
s when serializingObject
to JSON, there is another feature,JsonParser.Feature.AUTO_CLOSE_SOURCE
, which controlsInputStream
s when deserializing JSON toObject
. So if you want to handle closing bothInput-
andOutputStream
s yourself: