You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xml.apache.org by Ahmad Morad <mo...@db.informatik.uni-kassel.de> on 2000/12/20 09:11:18 UTC
Reading (Parsing) XML/Text from Sockets
In my code i read XML Data from the InputStream of a socket. But in some
cases, i read also standard text data from the socket. So i need a method,
which reads the complete message from the stream and returns the result as
a string. I use the following code in my application:
public synchronized String read()
throws TclDBEmptyServerMessageException
{
// theInput is theSocket.getInputStream();
WrappedInputStream wf = new WrappedInputStream(theInput);
byte[] fBuffer = new byte[2048]; // restrictet from theInput Stream!
String s= null;
localBuffer.delete(0,localBuffer.length());
try {
int count;
int length = fBuffer.length;
int testC = 1;
while ((count = wf.read(fBuffer,0, length)) > 0) {
s = new String(fBuffer,0,count);
System.out.println("Buffer Nr. " + testC + ": " + s);//_debug
localBuffer.append(s);
testC++;
}
wf.close();
}
catch (InterruptedIOException e) {}
catch (IOException e) {
e.printStackTrace();
}
String message = new String(localBuffer);
return message;
}
But this "cut" some bytes of the data. So I tried to test the procedure with
a plain file, and it works currectly!!. By comparing the outputs of both codes
i found that the read method by the socket code reads less than 2048.
Counting the difference i became 63 bytes less than 2048 (could be 64!). I
can't fix the problem. Anybody have an Idea about a possible reason for the
bug!.
##
Here some output (examples sending the XSL-File to the Client):
1. The socket code:
fPacketCount : (was 0) 7779
packet Count : 7779,length : 2048
readed data (value of count): 2048 // after calling super.in.read()
rest of packet= : 5731 // 7779 - 2048
Buffer Nr. 1: <?xml version="1.0"?> // some data (should be 2048 bytes)
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1"/>
....
end of data is :
<xsl:when test="@state='empty'">
<xsl:attribute name="align">
<xsl
2. The same output I got from the test with a plain file but in the buffer
i got more data:
...
end of data
<xsl:when test="@state='empty'">
<xsl:attribute name="align">
<xsl:text>center</xsl:text>
</xsl:attribute>
<xs
##
The diffrence is exactly the text:
text>center</xsl:text>
</xsl:attribute>
<xs
which counts 63 bytes (64!). I have really no Idea about the reason!!
(Note1): the code in the socket example reads the second time at the correct
point, tell the end of the file! Here became I the strange fPacketCount of
29548. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
(Note2) The Input is produced with Tcl. for writing the packetcount i use
the "binary format S [string length $message]". which produce a 2-Byte Integer
in big-endian byte order.
Thanks.