HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavaMinor

How to increase download speed using the following method?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
themethodfollowingusingincreasehowdownloadspeed

Problem

I have written an Android application which downloads pdf files through web service. I am using kSoap2 android library to parse the response of web service which basically contains file name & file data.

I have written following code. Please review & tell how to increase the speed I think there is some small defect in code which reduces the speed. I am using version 2.5.1 of kSoap2.

```
public void downloadPdfFiles(File fileDocsDir, int noOfFiles)
throws NullPointerException, SoapFault, XmlPullParserException,
FileNotFoundException, IOException, Exception {

System.gc();

// Download files from web service and save them in the folder.
soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
soapObject.addProperty("startingFile", noOfFiles);
soapObject.addProperty("deviceId", deviceId);
soapObject.addProperty("loginId", loginId);
soapObject.addProperty("byoinId", "1");

envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(soapObject);

// Calling the web service
System.gc();
androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);

// Getting Response through xml mainly generated as soap:reponse.
responseBean = (SoapObject) envelope.getResponse();

// Array of PDFInfoBean.
list = (SoapObject) responseBean.getProperty(0);

FileOutputStream outputStream = null;
BufferedOutputStream bufferedOutputStream = null;
// Get Individual PDF Details from Array.
SoapObject pdfDetails = null;

mIncrement = noOfFiles;
// Log.i("Increment Values", String.valueOf(mIncrement));

File pdfFile = null;
for (int i = 0; i < list.getPropertyCount(); i++) {
pdfDetails = (SoapObject) list.getProperty(i);

// Get PDF File Name.
pdfDocName = pdfDetails.getPrope

Solution

-
Forcing System.gc() isn't a good practice.

-
outputStream = new FileOutputStream(pdfFile);
        bufferedOutputStream = new BufferedOutputStream(outputStream);


should be wrapped into 1 variable to do a single close() afterwards:

outputStream = new BufferedOutputStream(new FileOutputStream(pdfFile));


-
All operations with outputStream should be put in try-finally block:

outputStream = new ...
try {
    outputStream.write(...);
    ...
} finally {
    outputStream.close();
}


-
Agree with the first comment: your file may simply not fit in memory. In SOAP API there should be some mean of getting the InputStream instead of an in-memory string.

Sorry for the formatting, couldn't fight the editor.

Good luck!

Code Snippets

outputStream = new FileOutputStream(pdfFile);
        bufferedOutputStream = new BufferedOutputStream(outputStream);
outputStream = new BufferedOutputStream(new FileOutputStream(pdfFile));
outputStream = new ...
try {
    outputStream.write(...);
    ...
} finally {
    outputStream.close();
}

Context

StackExchange Code Review Q#943, answer score: 5

Revisions (0)

No revisions yet.