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: 101
Registered: ‎05-11-2009
My Device: 9000
Accepted Solution

HTTP Post DataOutputStream flush and chunked encoding issues

[ Edited ]

Hi,

 

I am finally back to the issue I had a few months ago, but I still cannot fix it Smiley Sad

 

I used this article for understanding how to download files in chunks from a server: http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800431/How_To_...

 

The download was easy to implement, but I am having issues with uploading. I am trying to upload one big file in smaller chunks. I tried different options. The one that worked the best was when I use DataOutputStream and write to it after every little portion was read from the file.

 

But when I used flush() afterwards it threw OutOfMemoryException() ! But only if the file is large enough.

 

Some sample code:

 

 

InputStream    is = (StreamConnection)Connector.open(url , Connector.READ_WRITE, true);
HttpConnection c = (HttpsConnection)is;

//set connection parameters
c.setRequestMethod( HttpConnection.POST );
c.setRequestProperty( "User-Agent", "MyUserAgent/0.2" );

for( ;; ) 
{
if( done ) break;

//i have removed some reading code to simlify example out.write( bytedata , 0 , bytesRead ); Thread.yield(); } out.flush(); //<-- OutOfMemory exception here! int result = c.getResponseCode(); out.close();

 

 

 

I have found that I wasn't the only one who was having this issue.

 

1: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4212479

 

2: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5026745

 

 I have generally the same code, but use only HTTPConnection for that.

 

The workaround was to use "Chunked" encoding header, and calling flush every time the data is written to the output stream.

 

However it didn't work for me. If I use chunked encoding I get "ConnectionClosedException" when flush() is used for the first time.

 

Here is a sample code:

 

 

InputStream    is = (StreamConnection)Connector.open(url , Connector.READ_WRITE, true);
HttpConnection c = (HttpsConnection)is;

//set connection parameters
c.setRequestMethod( HttpConnection.POST );
c.setRequestProperty( "User-Agent", "MyUserAgent/0.2" );
c.setRequestProperty( HttpProtocolConstants.HEADER_TRANSFER_ENCODING, "chunked" );

for( ;; ) 
{

if( done ) break;

//i have removed some reading code to simlify example out.write( bytedata , 0 , bytesRead ); Thread.yield(); out.flush(); //<-- ConnectionClosedException } int result = c.getResponseCode(); out.close();

 

 Here is a screen-shot of the stack trace at exception moment:

 

    1628i2E8FDBBCE5883FB8

 

And here are the exception details:

 

 1629i1A9AA64478A60053

 

Now I honestly don't know what to do next. I tried all kind of options and choices, but can't get that upload working for some reason.

 

Tried on BlackBerry JDE and Simulator 4.2.1

 

Please help,

 

Thanks.

 

New Developer
Posts: 12
Registered: ‎09-03-2008
My Device: Not Specified

Re: HTTP Post DataOutputStream flush and chunked encoding issues

[ Edited ]
for( ;; ) 
{
//i have removed some reading code to simlify example
out.write( bytedata , 0 , bytesRead );
Thread.yield();
}

It's repeatedly loop. You have to add a condition to exit the loop.

such as

for( ;; ){

       if(bytesRead == -1) break;

      .//your code

      .

      .

}

 

 

i-softwarelabs
New Developer
Posts: 12
Registered: ‎09-03-2008
My Device: Not Specified

Re: HTTP Post DataOutputStream flush and chunked encoding issues

I follow the links above and it's difference from your condition, I think.

i-softwarelabs
Highlighted
Developer
Posts: 101
Registered: ‎05-11-2009
My Device: 9000

Re: HTTP Post DataOutputStream flush and chunked encoding issues

[ Edited ]

Thanks for reply, but I have removed the condition for breaking out of the loop to simplify example. (the actual code is much bigger)

 

 

The problem is not in infinitive loop and as you can see from my comments the program reaches the point from outside of the loop.

 

But thank you for pointing it out, I have changed my post and added break condition also.

New Developer
Posts: 12
Registered: ‎09-03-2008
My Device: Not Specified

Re: HTTP Post DataOutputStream flush and chunked encoding issues

Follow this steps then I THINK your problem will be solved:

1. Upload smallest file to server. File size = CHUNK_SIZE.

2. Now use integer CHUNCK_SIZE to split your file into small chunks and then upload those split file.

 

Increase the CHUNK_SIZE and find the maximum to optimize performance.

As you can upload small file so the problem is the size of chuncked array.

 

I have experience to upload BlackBerry's logs to server and found that I have to split logs size to < 1-2K.

 

Hope this help.

 

 

 

i-softwarelabs
Developer
Posts: 101
Registered: ‎05-11-2009
My Device: 9000

Re: HTTP Post DataOutputStream flush and chunked encoding issues

[ Edited ]

Thanks, but I think this was not related to this problem.

 

The solution is to use SocketConnection write data to OutputStream and flush() would work. But only if Content-Length is calculated accurately and that server request is also correctly formatted.

 

If there are errors in POST request the server would send a message back and ConnectionClosedException would appear.

 

The best way to solve these kind of issues is to get a good packet sniffer like "Wireshark" and study every packet very carefully even a small typo can break everything.

 

I have another issue, but the upload itself is working with flush() as expected on both device and emulator. So I think this can be considered as fixed.

New Member
Posts: 1
Registered: ‎05-16-2010
My Device: 8110

Re: HTTP Post DataOutputStream flush and chunked encoding issues