Skip to content

Commit f4db52c

Browse files
committed
onProgress now returns Long results, for sake of dealing with large files, Closes android-async-http#847
1 parent 186def7 commit f4db52c

7 files changed

+22
-17
lines changed

library/src/main/java/com/loopj/android/http/AsyncHttpResponseHandler.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
* });
7979
* </pre>
8080
*/
81+
@SuppressWarnings("ALL")
8182
public abstract class AsyncHttpResponseHandler implements ResponseHandlerInterface {
8283

8384
private static final String LOG_TAG = "AsyncHttpResponseHandler";
@@ -246,7 +247,7 @@ public String getCharset() {
246247
* @param bytesWritten offset from start of file
247248
* @param totalSize total size of file
248249
*/
249-
public void onProgress(int bytesWritten, int totalSize) {
250+
public void onProgress(long bytesWritten, long totalSize) {
250251
Log.v(LOG_TAG, String.format("Progress %d from %d (%2.0f%%)", bytesWritten, totalSize, (totalSize > 0) ? (bytesWritten * 1.0 / totalSize) * 100 : -1));
251252
}
252253

@@ -313,7 +314,7 @@ public void onUserException(Throwable error) {
313314
}
314315

315316
@Override
316-
final public void sendProgressMessage(int bytesWritten, int bytesTotal) {
317+
final public void sendProgressMessage(long bytesWritten, long bytesTotal) {
317318
sendMessage(obtainMessage(PROGRESS_MESSAGE, new Object[]{bytesWritten, bytesTotal}));
318319
}
319320

@@ -379,7 +380,7 @@ protected void handleMessage(Message message) {
379380
response = (Object[]) message.obj;
380381
if (response != null && response.length >= 2) {
381382
try {
382-
onProgress((Integer) response[0], (Integer) response[1]);
383+
onProgress((Long) response[0], (Long) response[1]);
383384
} catch (Throwable t) {
384385
Log.e(LOG_TAG, "custom onProgress contains an error", t);
385386
}
@@ -480,12 +481,13 @@ byte[] getResponseData(HttpEntity entity) throws IOException {
480481
ByteArrayBuffer buffer = new ByteArrayBuffer(buffersize);
481482
try {
482483
byte[] tmp = new byte[BUFFER_SIZE];
483-
int l, count = 0;
484+
long count = 0;
485+
int l;
484486
// do not send messages if request has been cancelled
485487
while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
486488
count += l;
487489
buffer.append(tmp, 0, l);
488-
sendProgressMessage(count, (int) (contentLength <= 0 ? 1 : contentLength));
490+
sendProgressMessage(count, (contentLength <= 0 ? 1 : contentLength));
489491
}
490492
} finally {
491493
AsyncHttpClient.silentCloseInputStream(instream);

library/src/main/java/com/loopj/android/http/DataAsyncHttpResponseHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929

30+
@SuppressWarnings("ALL")
3031
public abstract class DataAsyncHttpResponseHandler extends AsyncHttpResponseHandler {
3132
private static final String LOG_TAG = "DataAsyncHttpResponseHandler";
3233

@@ -105,7 +106,7 @@ byte[] getResponseData(HttpEntity entity) throws IOException {
105106
while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
106107
buffer.append(tmp, 0, l);
107108
sendProgressDataMessage(copyOfRange(tmp, 0, l));
108-
sendProgressMessage(count, (int) contentLength);
109+
sendProgressMessage(count, contentLength);
109110
}
110111
} finally {
111112
AsyncHttpClient.silentCloseInputStream(instream);

library/src/main/java/com/loopj/android/http/FileAsyncHttpResponseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ protected byte[] getResponseData(HttpEntity entity) throws IOException {
153153
while ((l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
154154
count += l;
155155
buffer.write(tmp, 0, l);
156-
sendProgressMessage(count, (int) contentLength);
156+
sendProgressMessage(count, contentLength);
157157
}
158158
} finally {
159159
AsyncHttpClient.silentCloseInputStream(instream);

library/src/main/java/com/loopj/android/http/JsonStreamerEntity.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public JsonStreamerEntity(ResponseHandlerInterface progressHandler, boolean useG
8383
this.progressHandler = progressHandler;
8484
this.contentEncoding = useGZipCompression ? HEADER_GZIP_ENCODING : null;
8585
this.elapsedField = TextUtils.isEmpty(elapsedField)
86-
? null
87-
: escape(elapsedField);
86+
? null
87+
: escape(elapsedField);
8888
}
8989

9090
/**
@@ -283,7 +283,8 @@ private void writeToFromFile(OutputStream os, RequestParams.FileWrapper wrapper)
283283
// Send the meta data.
284284
writeMetaData(os, wrapper.file.getName(), wrapper.contentType);
285285

286-
int bytesRead, bytesWritten = 0, totalSize = (int) wrapper.file.length();
286+
int bytesRead;
287+
long bytesWritten = 0, totalSize = wrapper.file.length();
287288

288289
// Open the file for reading.
289290
FileInputStream in = new FileInputStream(wrapper.file);

library/src/main/java/com/loopj/android/http/RangeFileAsyncHttpResponseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ protected byte[] getResponseData(HttpEntity entity) throws IOException {
8888
while (current < contentLength && (l = instream.read(tmp)) != -1 && !Thread.currentThread().isInterrupted()) {
8989
current += l;
9090
buffer.write(tmp, 0, l);
91-
sendProgressMessage((int) current, (int) contentLength);
91+
sendProgressMessage(current, contentLength);
9292
}
9393
} finally {
9494
instream.close();

library/src/main/java/com/loopj/android/http/ResponseHandlerInterface.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public interface ResponseHandlerInterface {
5353
* @param bytesWritten number of written bytes
5454
* @param bytesTotal number of total bytes to be written
5555
*/
56-
void sendProgressMessage(int bytesWritten, int bytesTotal);
56+
void sendProgressMessage(long bytesWritten, long bytesTotal);
5757

5858
/**
5959
* Notifies callback, that request was cancelled
@@ -148,7 +148,7 @@ public interface ResponseHandlerInterface {
148148
* This method is called once by the system when the response is about to be
149149
* processed by the system. The library makes sure that a single response
150150
* is pre-processed only once.
151-
*
151+
* <p/>
152152
* Please note: pre-processing does NOT run on the main thread, and thus
153153
* any UI activities that you must perform should be properly dispatched to
154154
* the app's UI thread.
@@ -162,7 +162,7 @@ public interface ResponseHandlerInterface {
162162
* This method is called once by the system when the request has been fully
163163
* sent, handled and finished. The library makes sure that a single response
164164
* is post-processed only once.
165-
*
165+
* <p/>
166166
* Please note: post-processing does NOT run on the main thread, and thus
167167
* any UI activities that you must perform should be properly dispatched to
168168
* the app's UI thread.

library/src/main/java/com/loopj/android/http/SimpleMultipartEntity.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ class SimpleMultipartEntity implements HttpEntity {
6969

7070
private final ResponseHandlerInterface progressHandler;
7171

72-
private int bytesWritten;
72+
private long bytesWritten;
7373

74-
private int totalSize;
74+
private long totalSize;
7575

7676
public SimpleMultipartEntity(ResponseHandlerInterface progressHandler) {
7777
final StringBuilder buf = new StringBuilder();
@@ -117,6 +117,7 @@ public void addPart(String key, File file) {
117117
public void addPart(String key, File file, String type) {
118118
fileParts.add(new FilePart(key, file, normalizeContentType(type)));
119119
}
120+
120121
public void addPart(String key, File file, String type, String customFileName) {
121122
fileParts.add(new FilePart(key, file, normalizeContentType(type), customFileName));
122123
}
@@ -167,7 +168,7 @@ private byte[] createContentDisposition(String key, String fileName) {
167168
"; filename=\"" + fileName + "\"" + STR_CR_LF).getBytes();
168169
}
169170

170-
private void updateProgress(int count) {
171+
private void updateProgress(long count) {
171172
bytesWritten += count;
172173
progressHandler.sendProgressMessage(bytesWritten, totalSize);
173174
}

0 commit comments

Comments
 (0)