Skip to content

Commit fe5d1c7

Browse files
authored
Merge pull request #583 from jonnynfb/fix-Android-downloadFile-overflow
fix Android downloadFile overflow contentLength and bytesWritten
2 parents e46f687 + 30898ca commit fe5d1c7

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

android/src/main/java/com/rnfs/DownloadParams.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ public interface OnTaskCompleted {
1212
}
1313

1414
public interface OnDownloadBegin {
15-
void onDownloadBegin(int statusCode, int contentLength, Map<String, String> headers);
15+
void onDownloadBegin(int statusCode, long contentLength, Map<String, String> headers);
1616
}
1717

1818
public interface OnDownloadProgress {
19-
void onDownloadProgress(int contentLength, int bytesWritten);
19+
void onDownloadProgress(long contentLength, long bytesWritten);
2020
}
2121

2222
public URL src;

android/src/main/java/com/rnfs/DownloadResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public class DownloadResult {
44
public int statusCode;
5-
public int bytesWritten;
5+
public long bytesWritten;
66
public Exception exception;
77
}

android/src/main/java/com/rnfs/Downloader.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
import com.facebook.react.bridge.ReadableMapKeySetIterator;
2121

22-
public class Downloader extends AsyncTask<DownloadParams, int[], DownloadResult> {
22+
public class Downloader extends AsyncTask<DownloadParams, long[], DownloadResult> {
2323
private DownloadParams mParam;
2424
private AtomicBoolean mAbort = new AtomicBoolean(false);
2525
DownloadResult res;
@@ -64,7 +64,7 @@ private void download(DownloadParams param, DownloadResult res) throws Exception
6464
connection.connect();
6565

6666
int statusCode = connection.getResponseCode();
67-
int lengthOfFile = connection.getContentLength();
67+
long lengthOfFile = connection.getContentLengthLong();
6868

6969
boolean isRedirect = (
7070
statusCode != HttpURLConnection.HTTP_OK &&
@@ -85,7 +85,7 @@ private void download(DownloadParams param, DownloadResult res) throws Exception
8585
connection.connect();
8686

8787
statusCode = connection.getResponseCode();
88-
lengthOfFile = connection.getContentLength();
88+
lengthOfFile = connection.getContentLengthLong();
8989
}
9090
if(statusCode >= 200 && statusCode < 300) {
9191
Map<String, List<String>> headers = connection.getHeaderFields();
@@ -107,7 +107,7 @@ private void download(DownloadParams param, DownloadResult res) throws Exception
107107
output = new FileOutputStream(param.dest);
108108

109109
byte data[] = new byte[8 * 1024];
110-
int total = 0;
110+
long total = 0;
111111
int count;
112112
double lastProgressValue = 0;
113113

@@ -116,14 +116,14 @@ private void download(DownloadParams param, DownloadResult res) throws Exception
116116

117117
total += count;
118118
if (param.progressDivider <= 0) {
119-
publishProgress(new int[]{lengthOfFile, total});
119+
publishProgress(new long[]{lengthOfFile, total});
120120
} else {
121121
double progress = Math.round(((double) total * 100) / lengthOfFile);
122122
if (progress % param.progressDivider == 0) {
123123
if ((progress != lastProgressValue) || (total == lengthOfFile)) {
124124
Log.d("Downloader", "EMIT: " + String.valueOf(progress) + ", TOTAL:" + String.valueOf(total));
125125
lastProgressValue = progress;
126-
publishProgress(new int[]{lengthOfFile, total});
126+
publishProgress(new long[]{lengthOfFile, total});
127127
}
128128
}
129129
}
@@ -146,7 +146,7 @@ protected void stop() {
146146
}
147147

148148
@Override
149-
protected void onProgressUpdate(int[]... values) {
149+
protected void onProgressUpdate(long[]... values) {
150150
super.onProgressUpdate(values);
151151
mParam.onDownloadProgress.onDownloadProgress(values[0][0], values[0][1]);
152152
}

android/src/main/java/com/rnfs/RNFSManager.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ public void onTaskCompleted(DownloadResult res) {
654654

655655
infoMap.putInt("jobId", jobId);
656656
infoMap.putInt("statusCode", res.statusCode);
657-
infoMap.putInt("bytesWritten", res.bytesWritten);
657+
infoMap.putDouble("bytesWritten", (double)res.bytesWritten);
658658

659659
promise.resolve(infoMap);
660660
} else {
@@ -664,7 +664,7 @@ public void onTaskCompleted(DownloadResult res) {
664664
};
665665

666666
params.onDownloadBegin = new DownloadParams.OnDownloadBegin() {
667-
public void onDownloadBegin(int statusCode, int contentLength, Map<String, String> headers) {
667+
public void onDownloadBegin(int statusCode, long contentLength, Map<String, String> headers) {
668668
WritableMap headersMap = Arguments.createMap();
669669

670670
for (Map.Entry<String, String> entry : headers.entrySet()) {
@@ -675,20 +675,20 @@ public void onDownloadBegin(int statusCode, int contentLength, Map<String, Strin
675675

676676
data.putInt("jobId", jobId);
677677
data.putInt("statusCode", statusCode);
678-
data.putInt("contentLength", contentLength);
678+
data.putDouble("contentLength", (double)contentLength);
679679
data.putMap("headers", headersMap);
680680

681681
sendEvent(getReactApplicationContext(), "DownloadBegin-" + jobId, data);
682682
}
683683
};
684684

685685
params.onDownloadProgress = new DownloadParams.OnDownloadProgress() {
686-
public void onDownloadProgress(int contentLength, int bytesWritten) {
686+
public void onDownloadProgress(long contentLength, long bytesWritten) {
687687
WritableMap data = Arguments.createMap();
688688

689689
data.putInt("jobId", jobId);
690-
data.putInt("contentLength", contentLength);
691-
data.putInt("bytesWritten", bytesWritten);
690+
data.putDouble("contentLength", (double)contentLength);
691+
data.putDouble("bytesWritten", (double)bytesWritten);
692692

693693
sendEvent(getReactApplicationContext(), "DownloadProgress-" + jobId, data);
694694
}

0 commit comments

Comments
 (0)