Skip to content

Commit efe3382

Browse files
committed
Make ParseAWSRequest.onResponse run in io thread
1 parent 79d753c commit efe3382

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

Parse/src/main/java/com/parse/ParseAWSRequest.java

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
import java.io.File;
1212
import java.io.FileOutputStream;
13-
import java.io.IOException;
1413
import java.io.InputStream;
14+
import java.util.concurrent.Callable;
1515

1616
import bolts.Task;
1717

@@ -30,7 +30,7 @@ public ParseAWSRequest(Method method, String url, File tempFile) {
3030
}
3131

3232
@Override
33-
protected Task<Void> onResponseAsync(ParseHttpResponse response,
33+
protected Task<Void> onResponseAsync(final ParseHttpResponse response,
3434
final ProgressCallback downloadProgressCallback) {
3535
int statusCode = response.getStatusCode();
3636
if (statusCode >= 200 && statusCode < 300 || statusCode == 304) {
@@ -45,29 +45,32 @@ protected Task<Void> onResponseAsync(ParseHttpResponse response,
4545
return null;
4646
}
4747

48-
int totalSize = response.getTotalSize();
49-
int downloadedSize = 0;
50-
InputStream responseStream = null;
51-
try {
52-
responseStream = response.getContent();
53-
FileOutputStream tempFileStream = new FileOutputStream(tempFile);
48+
return Task.call(new Callable<Void>() {
49+
@Override
50+
public Void call() throws Exception {
51+
int totalSize = response.getTotalSize();
52+
int downloadedSize = 0;
53+
InputStream responseStream = null;
54+
try {
55+
responseStream = response.getContent();
56+
FileOutputStream tempFileStream = new FileOutputStream(tempFile);
5457

55-
int nRead;
56-
byte[] data = new byte[32 << 10]; // 32KB
58+
int nRead;
59+
byte[] data = new byte[32 << 10]; // 32KB
5760

58-
while ((nRead = responseStream.read(data, 0, data.length)) != -1) {
59-
tempFileStream.write(data, 0, nRead);
60-
downloadedSize += nRead;
61-
if (downloadProgressCallback != null && totalSize != -1) {
62-
int progressToReport = Math.round((float) downloadedSize / (float) totalSize * 100.0f);
63-
downloadProgressCallback.done(progressToReport);
61+
while ((nRead = responseStream.read(data, 0, data.length)) != -1) {
62+
tempFileStream.write(data, 0, nRead);
63+
downloadedSize += nRead;
64+
if (downloadProgressCallback != null && totalSize != -1) {
65+
int progressToReport = Math.round((float) downloadedSize / (float) totalSize * 100.0f);
66+
downloadProgressCallback.done(progressToReport);
67+
}
68+
}
69+
return null;
70+
} finally {
71+
ParseIOUtils.closeQuietly(responseStream);
6472
}
6573
}
66-
return Task.forResult(null);
67-
} catch (IOException e) {
68-
return Task.forError(e);
69-
} finally {
70-
ParseIOUtils.closeQuietly(responseStream);
71-
}
74+
}, ParseExecutors.io());
7275
}
7376
}

0 commit comments

Comments
 (0)