Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Socket connection fails soon after being created

[ Edited ]

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().

Developer
ubuntaur
Posts: 41
Registered: ‎09-11-2009
My Device: Not Specified

Re: Socket connection fails soon after being created

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.

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: Socket connection fails soon after being created

You're welcome!