If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

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
New Contributor
p3ngu3n
Posts: 3
Registered: ‎04-22-2013
My Device: simulator
My Carrier: avea
Accepted Solution

Download so slow

Hi,
I want download some json files and write on path. My problem is ,this happened soo slow and i cant understand why. is anyway to do that different? i working on OS5.
Best regards. thanks
 
 
public void downloadFile(String Path , String fileName, String _url){
 
HttpConnection conn = null;
DataInputStream  in = null;
FileConnection fconn = null;
FileConnection fc = null;
OutputStream os = null;
 
try {
fc = (FileConnection)Connector.open(Path);
if (!fc.exists())
{
   fc.mkdir();
   
}
fconn = (FileConnection) Connector.open(Path + fileName ,Connector.READ_WRITE);          
if (!fconn.exists()) {
fconn.create();
}
fconn.setReadable(true);
fconn.setWritable(true);
 
os = fconn.openOutputStream();
 
 
conn = (HttpConnection) Connector.open( _url + getConnectionTags(), Connector.READ);
conn.setRequestMethod(HttpConnection.GET);
 
int code = conn.getResponseCode();
 
if (code == HttpConnection.HTTP_OK) {
in = new DataInputStream(conn.openInputStream());
 
StringBuffer buffer=new StringBuffer();
 
int ch = 0;
while ((ch = in.read()) != -1) {
buffer.append((char)ch);
 
}
 
 
os.write(String.valueOf(buffer).getBytes());
 
os.flush();
 
}
 
os.close();
 
} catch (Exception e) {
e.printStackTrace();
} finally {
       if (fconn!=null) {
           try {
               fconn.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
       if(conn != null){
                try {
conn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
       }
       if(fc != null){
        try {
fc.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
       }
       if(in != null){
        try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
       }
       if(os != null){
        try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
       }
   }
 
 
}
Please use plain text.
Developer
simon_hain
Posts: 15,892
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Download so slow

why don't you write the inputstream directly into the outputstream?
otherwise check http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/io/IOUtilities.html#streamToBy... to read the stream more effectively, char by char is not a very efficient method.

i would also suggest to log some timestamps to see which part takes what time, this makes it easier to improve.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
p3ngu3n
Posts: 3
Registered: ‎04-22-2013
My Device: simulator
My Carrier: avea

Re: Download so slow

i used that like os.write(IOUtilities.streamToBytes(in)); but with this method sometimes cant read anything and write something meaningless string like this : [B@cce4375 

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

Re: Download so slow

If you are seeing something like:

[B@cce4375

then you have used a toString method on an Object that does not support toString and as a result, you are getting the object reference.

 

In this case, I agree completely with Simon's suggestions.  But instead of using StreamToBytes, I would create a 1024 byte read buffer, and the read and write 1K blocks until the last block, and then you will process whatever is left.  This has the added advantage in that you d not convert the bytes to String and back again, which might loose some encoding. 

 

But do put some timings to to find out where the time is actually spent. 

Please use plain text.
New Contributor
p3ngu3n
Posts: 3
Registered: ‎04-22-2013
My Device: simulator
My Carrier: avea

Re: Download so slow

So much thanks for your replies. I understand my problem and streamtobytes really change download speed.


for other problem 

 

os.write(("blabla" +IOUtilities.streamToBytes(in) +"blabla"  ).getBytes() );

 

i write this but this code totally wrong and i change like this

 

os.write(("blabla").getBytes() );
os.write(IOUtilities.streamToBytes(in));
os.write(("blabla").getBytes());

 

thanks again. best regards

 

 

Please use plain text.