Jeff,
Instead of waiting for the number of bytes read to be -1, identify when the
byte buffer has not been completely filled. When this condition occurs you
know you have reached the end of your xml document otherwise the call to
read() would block until more data is received to fill the buffer. I've
done this when reading XMLDocuments from a JMS ByteMessage but it will
behave in the same way for a tcp/ip stream.
int MESSAGE_BUFFER_LENGTH = 500;
private byte[] buffer = new byte[MESSAGE_BUFFER_LENGTH];
StringBuffer stringBuffer = new StringBuffer();
BytesMessage message = (BytesMessage)msg;
int length = 0;
do
{
length = message.readBytes(m_buffer);
if (length > 0)
{
String read = new String(m_buffer, 0, length, "US-ASCII");
stringBuffer.append(read);
}
}
while(length == MESSAGE_BUFFER_LENGTH);
Hope this helps,
HOWARD GAISFORD
Jeff Fisher
<[EMAIL PROTECTED]
terhouse.com> To
"jdjlist"
25/11/03 15:33 <[EMAIL PROTECTED]>
cc
Please respond to Subject
"jdjlist" [jdjlist] Reliable TCP/IP stream
<[EMAIL PROTECTED] reading
sys-con.com>
Is there a reliable way to read from a tcp/ip stream that will identify
when there is nothing left on the stream to read?
We have been using the following code
public int readData(StringBuffer buf) throws IOException
{
byte[] InBuf = new byte[2048];
int totBytes = 0;
int bytesRead = 0;
//
// Sanity check
if ( buf != null ) {
buf.delete(0, buf.length());
Arrays.fill(InBuf, (byte)0);
bytesRead = m_In.read(InBuf, 0, 2048);
while ( bytesRead != -1 ) {
totBytes += bytesRead;
buf.append(new String(InBuf, 0, bytesRead));
Arrays.fill(InBuf, (byte)0);
bytesRead = m_In.read(InBuf, 0, 2048);
}
}
return totBytes;
}
This has worked great until recently. We have been sending over larger xml
documents than we had before and now this code does not work.
Specifically, read does not return -1 even though there is nothing left on
the stream to read. This causes the code to block. If we use
m_In.available(), that returns a bogus 0 and we only get a partial
document. Besides, the available() function is not reliable based on the
java documentation.
There seems to be some size threshold here where these fail.
I've checked on the web, but almost exclusivly I've seen code similar to
the above. Does anyone have any suggestions?
Thanks
Jeff Fisher
---
You are currently subscribed to jdjlist as: [EMAIL PROTECTED]
To unsubscribe send a blank email to
[EMAIL PROTECTED]
http://www.sys-con.com/fusetalk To unsubscribe from all mailing lists,
click:
http://sys-con.com/[EMAIL PROTECTED]
DISCLAIMER: This e-mail (and any attachment) is intended only for the named
addressee and may contain confidential and/or privileged material. If you
are not the intended recipient, please notify the sender (or email
[EMAIL PROTECTED]) immediately and then delete the entire e-mail
without disclosing, copying, distributing any of its contents.
Any opinions or advice contained in this e-mail are those of the author and
not those of CMC Group plc and/or CMC Spreadbet plc (collectively 'CMC')
unless otherwise stated, but shall always be subject to CMC's terms of
business. CMC may intercept and monitor all e-mail communications for
lawful business purposes and accepts no liability for any viruses or
defects which may be contained in this e-mail.
---
You are currently subscribed to jdjlist as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]
http://www.sys-con.com/fusetalk
To unsubscribe from all mailing lists, click:
http://sys-con.com/[EMAIL PROTECTED]