Skip to content

Jackson: ObjectMapper closes stream after writing value #233

@fdinoff

Description

@fdinoff

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?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions