XMLDecoder and System.in
Hi,
I have an issue when using the XMLDecoder class in conjunction with System.in.
What I want to achieve is basically to be able to send the contents of an XML file to stdin of the Java application. However, when doing so, the readObject call to the decoder does not return anything until either the input stream is closed or until an invalid character is sent to the decoder. The complete XML file is sent. How can I make the decoder understand that the complete XML file has been sent. I guess that the decoder does not understand that, even though the root element enclosing tag has been sent.
Code snippet from receiver:
-
XMLDecoder decoder = new XMLDecoder(System.in);
System.out.println("XML decoder created");
int count = (Integer) decoder.readObject();
System.out.println("Number of objects in file: " + count);
for (int i = 0; i < count; i++) {
ListItem item = (ListItem) decoder.readObject();
callback.send(getPacketFromItem(item));
}
Code snippet from sender:
-
BufferedOutputStream outputStream = new BufferedOutputStream(System.out);
try {
outputStream.write(xmlString.getBytes());
outputStream.write(0);
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
If not writing the zero to the outputstream, the reader never returns from the first readObject. If writing the zero to the output stream, the reader gets an "org.xml.sax.SAXParseException: Content is not allowed in trailing section" error and then decodes the whole XML file.

