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.

[1615 byte] By [AndreasLa] at [2007-11-27 9:14:23]
# 1
Do you mean you are providindg all xml content through key board?or you have an xml file with you.?
MakAslama at 2007-7-12 22:02:33 > top of Java-index,Core,Core APIs...
# 2
the input on System.in is supposed to be terminated by a line ending, which you never send.So the decoder sits there, waiting for the input to be terminated or it receives an error.
jwentinga at 2007-7-12 22:02:33 > top of Java-index,Core,Core APIs...
# 3

The idea was to be able to send an XML file from another process. For example in Linux:

> XMLWriterApp | XMLReaderApp

Eventually I worked around the issue by reading the XML with the java.util.Scanner class by searching for the start and end root tag. Then passing the concatenated string to the XMLDecoder in a ByteArrayInputStream

AndreasLa at 2007-7-12 22:02:33 > top of Java-index,Core,Core APIs...