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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


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

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

Re: Socket connection fails soon after being created

You're welcome!