02-25-2010 05:28 AM
Some more info. I do an HTTP GET over WiFi. The content length comes back as -1, so I read a byte at a time. I read all the available bytes, then when I do the final read that should return -1 to indicate there is no more data I get the ConnectionClosedException instead.
Look, this is just a guess, but perhaps you're getting ConnectionClosed because there is no more data, not that your read() is returning -1? Are you actually getting a partial read? And as also said by others, seeing some code would help too.
It looks like I always get all my data, it is just that sometimes I get the read returning -1 at the end and sometimes I get a ConnectionClosedException after reading the last byte and before the read returns -1. So I guess I have two questions
1. Why is the behaviour inconsistent?
2. Is ConnectionClosedException ever thrown for an abnormal shutdown of the connection?
I guess if the exception never indicates an abnormal shutdown then I can just handle and ignore the exception and use the data I have in hand, just as for my current work-around.
02-25-2010 05:42 AM - edited 02-25-2010 05:43 AM
What if the BlackBerry network stack receives a TCP RST instead of a proper shutdown sequence? In this case it would have to assume that the connection was torn down prematurely. Thus, it would actually be wrong to return -1 from InputStream.read methods as it would be wrong to say that the complete stream had been received. A ConnectionClosedException ("reset by peer" detail message) would actually be quite appropriate.
Since you're on Wi-Fi, can you check whether the connection is terminated with an RST instead of FIN?
P.S. To elaborate, the stack itself (for some obscure reason) may decide to abruptly teminate the connection, in which case the above reasoning of -1 vs ConnectionClosedException would still apply.
02-25-2010 05:52 AM
Definitely always normal FIN ends to the streams, not RST, in all the cases I've looked at so far. But, yes, I'm also concerned that an RST could result in the same exception. I guess I should try some experiments to find out what an RST would actually result in - looking at the exception lists I can't see anything more likely than this one, and pre-5.0 there doesn't appear to be even a string detail message....
Thanks everyone for your attention to this issue.