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: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: ConnectionClosedException: Stream Closed

Have you actually dumped out the data to make sure that you are correctly getting the data you expect?

Developer
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: ConnectionClosedException: Stream Closed

[ Edited ]

I have determined the problem is most def inside of the .parse in docBuilder.

 

I read in every byte into a string and checked the string on my device and the entier XML doc is in the string.

 

 

			int rc = conn.getResponseCode();
if( rc == HttpConnection.HTTP_OK ){

//read the response

InputStream in = conn.openInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
while ((c = in.read()) >= 0)
{
baos.write(c);
}
response = new String (baos.toByteArray());

 

Now I just need to find a way to get that string into the docBuilder (or something similar) so I can put this back into production for OS5.

 

Developer
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: ConnectionClosedException: Stream Closed

oh sorry peter didn't see your reply.

 

actually i *JUST* did what you suggested... and yes, im getting the data.

 

and this has been working on thousands (well approximately 900 devices) since I launched it. I keep a record of PINS and OS's using the software at startup. It most definately works and has been forever.

Developer
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: ConnectionClosedException: Stream Closed

holy **bleep**, I dont wanna jynx it, but I think i fixed it.

 

Developer
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: ConnectionClosedException: Stream Closed

[ Edited ]

Ok, it seems thatI have determined (and fixed) the issue.

 

It looks like there is an issue with the "read()" function for input stream. The read() will sometimes throw an exception instead of doing a "-1" when it's finished reading a stream.

 

I couldn't override the docBuilder.parse, so what I did was read each byte of the InputStream manually, then caught any exception Read() was making by replacing it with -1. then i made a secondary InputStream and read in the bytes to that, and then used the "parse" and it now works.

 

Basically I replaced this code:

 

 

doc = docBuilder.parse(conn.openDataInputStream());

 

 

With this function:

 

 

	public static Document getDocument(InputStream in) throws Exception
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is;

try
{
int c = 0;

try { c = in.read(); } catch (Exception ex) { c = -1; }

while (c >= 0)
{
baos.write(c);
try { c = in.read(); } catch (Exception ex) { c = -1; }
}

String response = new String (baos.toByteArray());

is = new ByteArrayInputStream(response.getBytes());

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();

return docBuilder.parse(is);
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}

 

And I called it like so:

 

 

				InputStream is = conn.openInputStream();

// get the document
doc = ConUtils.getDocument(is);

 

 

 

Note: This was happening on Bold 9000 Hardware w/ OS 5.0.0.411

 

Thanks goes out to Albert @  ITookthisonmyphone.com for giving me an idea on where to look.

 

 

 

Developer
Posts: 90
Registered: ‎09-07-2010
My Device: Not Specified

Re: ConnectionClosedException: Stream Closed

MAGIC!!!!

GOOOOOOOOOOTEBORG!!!

 

 

You are my hero! Thanks mate!

Contributor
Posts: 16
Registered: ‎05-27-2010
My Device: any
My Carrier: any

Re: ConnectionClosedException: Stream Closed

Hi everybody.

 

I am having this issue and my solution was similar to the one posted here. The ConnectionClosedException is thrown instead of the -1, always for the last read, that's why I'm simple ignoring this exception and treating it as an EOF or end of stream.

 

Now, my problem is: the connection which retrieves some JSON data (not too long really) lasts at least 30 seconds and from logs I could see that data itself and transfering is NOT the cause of this delay, this delay exists just for the last read, the one that should return -1 but it's throwing ConnectionClosedException.

 

Here's a log that's showing this (one implemented on my own, logging in the device store to detect this problem):

 

2010-09-24 16:02:47.844 [DBG] Util.readFromHTTPConnection: starting to read stream
2010-09-24 16:02:47.903 [DBG] Util.readFromHTTPConnection: output stream created
2010-09-24 16:02:47.946 [DBG] Util.readFromHTTPConnection: buffer created, about to read first bytes . . .
2010-09-24 16:02:47.991 [DBG] Util.readFromHTTPConnection: first bytes read, length: 6444, data: [B@e74d3793
2010-09-24 16:02:48.033 [DBG] Util.readFromHTTPConnection: bytes written to output, length: 6444, data: [B@e74d3793
2010-09-24 16:03:17.066 [DBG] Util.readFromHTTPConnection Exception detected while reading, maybe EOF: Connection closed, net.rim.device.api.io.ConnectionClosedException: Connection closed
2010-09-24 16:03:17.215 [DBG] Util.readFromHTTPConnection: finished of reading stream
2010-09-24 16:03:17.292 [DBG] Util.readFromHTTPConnection: total bytes written to output length: 6444

 

Note the timestamp difference, between the last successfull read and the one that throws the exception (I'm reding in blocks of 8K) lines marked in red color: 02:48 to 03:17 (around 30 seconds in delay)

 

Code that logs that is like this:

 

               baos = new ByteArrayOutputStream();
                Log.D("Util.readFromHTTPConnection: output stream created");
                byte[] buffer = new byte[BUFFER_SIZE]  ;
                Log.D("Util.readFromHTTPConnection: buffer created, about to read first bytes . . .");
                try {
                    int bytesRead = in.read(buffer);
                    Log.D("Util.readFromHTTPConnection: first bytes read, length: " + bytesRead + ", data: " + buffer);
                    while (bytesRead > 0) {
                        baos.write(buffer,0,bytesRead);
                        Log.D("Util.readFromHTTPConnection: bytes written to output, length: " + bytesRead + ", data: " + buffer);
                        bytesRead = in.read(buffer);
                        Log.D("Util.readFromHTTPConnection: bytes read again from input, length: " + bytesRead + ", data: " + buffer);
                    }
                }
                catch(ConnectionClosedException e) {
                    System.out.println("Util.readFromHTTPConnection Exception detected while reading, maybe EOF: " + e.getMessage() + ", " + e);
                    //Dialoguete.alertUser("Util.readFromHTTPConnection Exception detected while reading, maybe EOF: " + e.getMessage() + ", " + e);
                    Log.D("Util.readFromHTTPConnection Exception detected while reading, maybe EOF: " + e.getMessage() + ", " + e);
                }
                
                Log.D("Util.readFromHTTPConnection: finished of reading stream");
                
                Log.D("Util.readFromHTTPConnection: total bytes written to output length: " + baos.size());

 

I am executing http requests in a separate thread (not in the event thread) and I tried to read the response code before and after opening the input stream but problems still happens. by the way I'm using https, but that should not cause these problems, I think.

 

Could anybody help me, please?

Thanks in advance,

Ezequiel


 

Contributor
Posts: 15
Registered: ‎02-28-2010
My Device: Bold 9000
My Carrier: Orange

Re: ConnectionClosedException: Stream Closed

Hi ezequield,

 

Did you find the problem?

I experience the similar issue.

Contributor
Posts: 16
Registered: ‎05-27-2010
My Device: any
My Carrier: any

Re: ConnectionClosedException: Stream Closed

[ Edited ]

Hi gusak!

It's good to know somebody else have a similar problem. I'm not alone after all.

 

To tell you the thruth, I haven't found the cause of it, but I've found a solution to my particular problem.

I'll tell you so that you may do something similar to take your problem away.

 

Since there's no proper signal or control character that can tell us when the end of stream is reached, the only way to guess it is looking at data.

 

In my case, responses form server are json format, so I'm just using a counter while reading adding 1 to it for each open brace "{"  and substracting 1 for each closing brace "}". When the counter reaches zero, I assume this is the end of stream and close the connection.

My answers never return a pure json array as the parent element, otherwise, I should have considered brackets "[" ]""as well.

Another important remark is quoting with commas " " and backslash that should not count as control braces.

 

Finally, if you are using InputStream.read(byte[SIZE] buffer), have into account that no matter how much available size you give to your buffer, response may arrive chunked, for example, say your buffer is 1K in length and your response is 1K, so you may have just one read of 1K or 2 reads of 512K or any combination, it's not sure that every read will occupy the full size of the buffer. This is important that you may keep counter and state variables (for quoting) among reads, not just for each read.

 

I suspect that this strage behaviour of the connection api is due to something related the way the server responses and manages connections, that mey not be well understood by the bb sdk.

You may also try adding and/or removing headers in connection.setProperty() sucha as "Keep-Alive" "Connection-Close".

 

Other things you might try is to opening the stream before getting the response code and always write to output stream of your connection before opening the input one. ensue with try{} finall{} blocks to close all stream and connections after using them and never do connections in the Event Thread, launch your reqeusts in separate threads.

 

Hope this helps!

Regards, Ezequiel

 

Developer
Posts: 18
Registered: ‎10-30-2008
My Device: Not Specified

Re: ConnectionClosedException: Stream Closed

I have the same issue on the 9700 with OS 5

I have not been able to resolve the issue and have tried everything you have.

I wanted to smash the device as well.

 

Once the stream is closed I get a hang condition that waits permanently.

I can cancel the thread but I have to restart my program.

Did you get any solution?