[jdom-interest] SAXBuilder hang when using InputStream

Ken Rune Helland kenh at csc.no
Thu Feb 28 01:47:48 PST 2002

> Hi,
> i try to build a document with SAXBuilder.build(java.io.InputStream);
> The Inputstrem object is from a socket object.
> My client create a document and send it with XMLOutputter to a server.
> <<< Client >>>
> XMLOutputter printer = new XMLOutputter();
> BufferedOutputStream out = new BufferedOutputStream(
> tester.getOutputStream() ); // tester is a socket object
> printer.output(doc,out);
> out.flush();
> <<< Server >>>
> SAXBuilder builder = new SAXBuilder(false);
> Document doc = builder.build(in);  <<-- it never comes back from
> builder.build().
> XMLOutputter printer = new org.jdom.output.XMLOutputter();
> try
> {
>     printer.output(data,System.out);
> }
> catch( java.io.IOException e ){e.printStackTrace();}
> If the client closes the socket, I receive a the exception
> org.jdom.JDOMException: Error in building: socket closed (code=0)
> Sending the data (XMLOutputter.putput(doc,out) is working, I checked
> these with a simple socket read on server side.
> I tried these with JDOM 1.0Beta7 and with the latest version from CVS,
> same error.
> Has anybody an idear whats going wrong ?
> Thanks,
> Uli Waibel

The problem is probably that the builder never get any EOF from
the inputstream. In XML it is legal to have PI's and comments after
the end of the root element so the builder has to wait for the
EOF from the inputstream.

You need to wrapp the sockets inputstream in a inputstream (a 
filterinputstream subclass you write yourself) witch
discovers the end of the document and returns a EOF when the
builder tries to read past the end of the document.

How the FilterInputStream is to know the end of the document is up to you,
one option is to make the client send a character that is not legal in XML
as an escape character after the outputter has output the complete document.

This is a common enough case that you shoud donate the filterinputstream to 
JDOM contrib afterwards if nobody has already done so. 


e-mail : kenh at csc.no
Tlf    : +47 22 95 86 45
Tlf Mob: +47 91 87 27 38

More information about the jdom-interest mailing list