Details
Description
Hi,
I get an exception when using an XStream instance from different threads that pass the same output stream to the XStream instance. I'll attach a small example to illustrate the problem. Please see the stack trace below.
Is this a bug or am I misreading the documentation? It says that the XStream instance can be shared across multiple threads and doesn't specify any constraints on the parameters given to the serializing/deserializing methods.
Thanks!
Exception in thread "Thread-19" com.thoughtworks.xstream.io.StreamException: : Writing text without an opened node
at com.thoughtworks.xstream.io.StatefulWriter.setValue(StatefulWriter.java:120)
at com.thoughtworks.xstream.io.WriterWrapper.setValue(WriterWrapper.java:45)
at com.thoughtworks.xstream.converters.extended.EncodedByteArrayConverter.marshal(EncodedByteArrayConverter.java:43)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:59)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:44)
at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:79)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.marshal(AbstractTreeMarshallingStrategy.java:37)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:877)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:866)
at com.thoughtworks.xstream.XStream$1.writeToStream(XStream.java:1626)
at com.thoughtworks.xstream.core.util.CustomObjectOutputStream.write(CustomObjectOutputStream.java:138)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:276)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:122)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:212)
at com.thoughtworks.xstream.core.util.QuickWriter.flush(QuickWriter.java:75)
at com.thoughtworks.xstream.io.xml.PrettyPrintWriter.endNode(PrettyPrintWriter.java:316)
at com.thoughtworks.xstream.io.WriterWrapper.endNode(WriterWrapper.java:37)
at com.thoughtworks.xstream.io.path.PathTrackingWriter.endNode(PathTrackingWriter.java:48)
at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:80)
at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.marshal(AbstractTreeMarshallingStrategy.java:37)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:877)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:866)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:854)
at XStreamTest4$2.run(XStreamTest4.java:27)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalStateException: Writing text without an opened node
... 28 more
You cannot expect something useful when you write concurrently into the same stream. This is true for XStream as well as for the JDK serialization:
You can safely use the same XStream instance in parallel to write into different streams. What should it good for, to write two different object graphs in parallel into the same stream anyway?
Please, if you have questions, use the user's list.