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
Andiamo
Posts: 39
Registered: ‎05-26-2009
My Device: BlackBerry Storm 9550

inputStream.read hangs occasionally when reading data from internet

Hi, here's a weird issue...

 

I've experienced this problem for some time and lately it's really been getting on my nerves. My app downloads images and text files from a server. Currently I'm doing that by opening the inputstream, creating a buffer array, and filling that buffer using inputStream.read() until there's no more data.

 

 

                    inputStream = connection.openInputStream();
                    int length = 0;
                    byte[] responseBuffer = new byte[10240]; // 10 kilobytes
                    StringBuffer rawResponse = new StringBuffer();
                    while (-1 != (length = inputStream.read(responseBuffer)))
                    {
                        Mango.logLine("Reading 10kb (" + url + ")");
                        rawResponse.append(new String(responseBuffer, 0, length));
                        // reset the timeout, since we have received some data.
                        currentRequest.timeout = System.currentTimeMillis() + DateTimeUtilities.ONESECOND * MangoData.getInt(MangoData.KEY_CONNTIMEOUT);
                    }

 The problem is, for some strange reason, inputStream.read will sometimes just decide to hang, for no apparent reason. During these situations my debug log will read:

 

Mango: 040910 220533135> Opening inputStream (http://media.onemanga.com/mangas/00000550/000027749/36.jpg)
Mango: 040910 220533343> Reading 10kb (http://media.onemanga.com/mangas/00000550/000027749/36.jpg)
Mango: 040910 220533386> Reading 10kb (http://media.onemanga.com/mangas/00000550/000027749/36.jpg)
Mango: 040910 220533420> Reading 10kb (http://media.onemanga.com/mangas/00000550/000027749/36.jpg)

 

 

And then just stop for 10 seconds until my timeout thread can force-kill the request. Before I implemented the timeout thread, read() would hang for up to 2 minutes before failing with a request canceled exception.

 

Like I said, it happens only occasionally. Maybe 1 in every 5-15 .read()'s. It does not seem to have anything to do with how much data it downloads... I've played with the buffer sizes by decreasing them to 1kb or increasing them all the way to 32kb, and the problem still happens at about the same frequency.

 

It's not a problem with the server. I'm getting data from two different sources, the images from one server and the text files (sometimes as big as 100kb) from another, and both experience the problem. The Android version of my app doesn't experience the issue. So I'm either doing something wrong in how I'm reading the data or there's something weird going on in the OS.

 

It happens most frequently on the simulator. It also happens on a real device, but not as often.

 

Is there any way I can stop this from happening? How can I change my method of reading the data from the stream to aleviate the problem?

 

Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: inputStream.read hangs occasionally when reading data from internet

Not sure this will help, but have a look:

 

How To - Download large files using the BlackBerry Mobile Data System
Article Number: DB-00619
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800431/How_To_...

Please use plain text.
Developer
Andiamo
Posts: 39
Registered: ‎05-26-2009
My Device: BlackBerry Storm 9550

Re: inputStream.read hangs occasionally when reading data from internet

Hi peter, thanks for your reply.

 

I don't think the problem is the size of the file. It's most noticible when downloading images and longer text file lists, but only really because there's more of an opportunity for it to fail. It also happens when downloading a small 5-15kb list, where read() will hang the first or second time it's called.

 

Also, I do not have any way to alter the http headers for the image server, so I can't use splitting or HTTP-Range. But thanks for the suggestions.

 

 

I can't understand why it works sometimes but not others. I've tried setting the timeout to 1 second, and my thread will kill the request again and again until it eventually, through sheer "luck", manages to download the entire thing fast enough. But it's quite random.

Please use plain text.
Contributor
vishalspatil
Posts: 40
Registered: ‎07-14-2010
My Device: Not Specified

Re: inputStream.read hangs occasionally when reading data from internet

Same issue for me too...

 

Very strange issue it is. I am getting this randomly after every 6-7 server hit and only for Wi-Fi network and not in direct TCP.

I debugged my connection code, I found it hangs while reading last chunk. E.g. if response size is 24142 bytes then it always hangs after reading 23552 bytes.

 

My response reading code is:

int readLength = 0;
byte[] responseData = new byte[1024];
ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
while ((readLength = inputStream.read(responseData, 0, 1024)) != -1) {
     byteArrayStream.write(responseData, 0, readLength);
     responseLength += readLength;
}

byte[] response = byteArrayStream.toByteArray();

Also I found client always succeed if data size is other than 24142 bytes. I tried more than 20 times for data sizes: 6560bytes, 25068 bytes and I get success always.

 

Any idea why this happens only for Wi-Fi and for particular response size?

 

Please help.



Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: inputStream.read hangs occasionally when reading data from internet

Is the server sending a content-length header with the response?




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: inputStream.read hangs occasionally when reading data from internet

Is this OS 5.0?

 

Does this (and the referenced Thread) seem like your problem too?

http://supportforums.blackberry.com/t5/Java-Development/Raw-socket-InputStream-availalble-in-WiFi-ne...

Please use plain text.
Contributor
vishalspatil
Posts: 40
Registered: ‎07-14-2010
My Device: Not Specified

Re: inputStream.read hangs occasionally when reading data from internet

@

 


Please use plain text.
Contributor
vishalspatil
Posts: 40
Registered: ‎07-14-2010
My Device: Not Specified

Re: inputStream.read hangs occasionally when reading data from internet

@peter,

 

Yes it is happening with 5.0 device 9700.

 

Same issue, which is there in link.

Please use plain text.