question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Jackson: ObjectMapper closes stream after writing value

See original GitHub issue

Currently 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:closed
  • Created 8 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

19reactions
boricecommented, Aug 21, 2018

Leaving a comment for future Googlers looking for a solution to the auto-closed stream:

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;

JsonFactory jsonFactory = new JsonFactory();
jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
ObjectMapper mapper = new ObjectMapper(jsonFactory);

// use mapper as needed... it won't automatically close your stream anymore.
4reactions
AaronSharp5commented, Mar 23, 2020

Adding to @borice comment to address @Sergey123456 issue, which I also encountered: In addition to JsonGenerator.Feature.AUTO_CLOSE_TARGET, which controls OutputStreams when serializing Object to JSON, there is another feature, JsonParser.Feature.AUTO_CLOSE_SOURCE, which controls InputStreams when deserializing JSON to Object. So if you want to handle closing both Input- and OutputStreams yourself:

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;

JsonFactory jsonFactory = new JsonFactory();
jsonFactory.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
jsonFactory.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
ObjectMapper mapper = new ObjectMapper(jsonFactory);

// use mapper as needed... it won't automatically close output OR input streams anymore.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found