01-25-2010 12:23 PM - edited 01-25-2010 12:26 PM
Yes, InputStream.read can block -- see the javadocs.
Regarding your reworked getNextSize(), you may find it easier/cleaner to use DataInputStream methods for easily reading fixed-length entries. For example, your code has a loop to read four bytes, and, if the stream ends earlier, the loop returns -1. You could replace the loop with creating a byte array of length 4, invoking one single DataInputStream.readFully(your array), and returning -1 if the method throws an IOException -- no loop required. Similarly, raedMessage can be implemented using a one single DataInputStream.readFully operation, since you know exactly how many bytes you'd like to read.
You can combine the above approach with first checking InputStream.available() to see whether the number of bytes you're about to read is available without blocking. This will ensure that your read operations won't block. However, keep in mind that some InputStream implementation (e.g., the one from an SSL connection) may always return 0 as available().
01-25-2010 05:37 PM
I don't want to jinx it, but I'm pretty sure that worked. =)
I made the changes you mentioned regarding using a DataInputStream and using its readFully() method. I also changed my OutputStreamWriter to a DataOutputStream. These changes along with some cleaning up of my code somehow made it work.
Thanks for the help! This has been an issue I've been trying to solve for well over a week now.