SV: [jdom-interest] Building from Socket

Per Norrman pernorrman at
Mon Nov 17 23:01:58 PST 2003


The problem is that the parser never sees an EOF, it tries reading
chunks of bytes and will block when there are not enough available.
If you must have the client socket open, then your only choice is to
introduce some sort of 'protocol', albeit a very simple one will

	byte[] data = ... // Read XML source into byte array
	DataOutputStream out = new

	DataInputStream in = new
	int length = in.readInt();
	byte data[] = new byte[length];

	Document doc = new SAXBuilder().build(new

Hope this helps


-----Ursprungligt meddelande-----
Från: jdom-interest-admin at [mailto:jdom-interest-admin at]
För Sebastian Robertsson 
Skickat: den 18 november 2003 02:01
Till: jdom-interest at
Ämne: [jdom-interest] Building from Socket

Hi everyone!

I know this is an old topic - the problem of building a JDOM-doc read
from a socket without closing it. I've 
just tried this solution:

But, I can't make it work. It seems like the parser still hangs, and
when I terminate the thread on the 
sender side, I get: Connection reset
	at Source)
	at Source)
	at server.ConnectedClient.<init>(
	at Source)

On the "sender-side" I use a BufferedOutputStream, and I flush the
stream after sendning. On the recieving 
side, this is the code:

byte[] data = getRequestData(clientSocket.getInputStream());
InputStream in = new ByteArrayInputStream(data);
Document d = getXMLDocument(in); 		

private byte[] getRequestData(InputStream stream) throws IOException {
	byte[] data = null;
	// Content length not specified.
	// => Read content stream until an EOF is encountered.
	ByteArrayOutputStream baos = new ByteArrayOutputStream(4096);
		int l;
	byte[] xfer = new byte[1024];
	while ((l = != -1)
	            baos.write(xfer, 0, l);
	data = baos.toByteArray();
	return data;

I get everything to work when I close the stream on the sending side,
but when I'm not closing it it seems 
like this code doesn't work after all. Though, as far as I can tell, the
XML-doc that I'm sending is alright (it 
gets parsed correcty when using the close stream-trick, and I can write
it to a file and open without 
problems). Can somebody help me with this, please. Any other solutions,
or have I missed something in 
Laurent Bihanic:s solution?



  Vad står det om dig på nätet? -

More information about the jdom-interest mailing list