Hi Christopher,

Thanks for the reply. :-)

Christopher Schultz wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sebastiaan,

Sebastiaan van Erk wrote:
I'm having a problem reading data in my Comet servlet.

In the BEGIN event I have the following loop:

   while (request.getInputStream().available() > 0) {
      // log that in read loop, log available()
      // read some data
   }
   // log that read loop is done, log available()
   return;

Are you sure this is what you want to do? This loop is not guaranteed to
read all of the incoming data.

Yes this is what I do, because of what the what the Comet docs say (http://tomcat.apache.org/tomcat-6.0-doc/aio.html):

EventType.READ: This indicates that input data is available, and that one read can be made without blocking. The available and ready methods of the InputStream or Reader may be used to determine if there is a risk of blocking: the servlet should read while data is reported available, and can make one additional read should read while data is reported available.

I realize that I'm in the BEGIN event, but that's because data that arrives in the request header buffer of Tomcat internally does not cause a new READ event, so I have to start reading in the BEGIN event.
Check the javadoc for InputStream.available:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html#available():

"Returns the number of bytes that can be read (or skipped over) from
this input stream without blocking by the next caller of a method for
this input stream."

If there will be 100 bytes eventually, but only 50 are available now,
you'll get 50 as the return value. If you read those bytes, but the rest
are unavailable, then your second loop condition evaluation will return
0, and the loop will exit (with 50 bytes still on the way).

I don't see why the loop is wrong (maybe I'm sleeping). But I keep reading while there are bytes available. If the bytes are not available, then I return from the event() method, and expect a READ event when there are newly available bytes. Instead Tomcat complains (immediately!!) that I've not read all available data, even though available() returned 0!

Does anybody know what the proper way to read ALL the data is, and not
cause this exception to happen?

If you want to read /all/ data and not worry about blocking, you should
be doing this:

byte[] buffer = new byte[BUFFER_SIZE];
int read;

while(0 < (read = request.getInputStream().read(buffer))
{
    // do something with 'read' bytes of data from the buffer
}

I don't understand why this is different from what I do. As soon as no bytes are available the loop exits.

Regards,
Sebastiaan


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to