10
10
11
11
import java .io .File ;
12
12
import java .io .FileOutputStream ;
13
- import java .io .IOException ;
14
13
import java .io .InputStream ;
14
+ import java .util .concurrent .Callable ;
15
15
16
16
import bolts .Task ;
17
17
@@ -30,7 +30,7 @@ public ParseAWSRequest(Method method, String url, File tempFile) {
30
30
}
31
31
32
32
@ Override
33
- protected Task <Void > onResponseAsync (ParseHttpResponse response ,
33
+ protected Task <Void > onResponseAsync (final ParseHttpResponse response ,
34
34
final ProgressCallback downloadProgressCallback ) {
35
35
int statusCode = response .getStatusCode ();
36
36
if (statusCode >= 200 && statusCode < 300 || statusCode == 304 ) {
@@ -45,29 +45,32 @@ protected Task<Void> onResponseAsync(ParseHttpResponse response,
45
45
return null ;
46
46
}
47
47
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 );
54
57
55
- int nRead ;
56
- byte [] data = new byte [32 << 10 ]; // 32KB
58
+ int nRead ;
59
+ byte [] data = new byte [32 << 10 ]; // 32KB
57
60
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 );
64
72
}
65
73
}
66
- return Task .forResult (null );
67
- } catch (IOException e ) {
68
- return Task .forError (e );
69
- } finally {
70
- ParseIOUtils .closeQuietly (responseStream );
71
- }
74
+ }, ParseExecutors .io ());
72
75
}
73
76
}
0 commit comments