Skip to content

Commit 30898ca

Browse files
committed
fix Android downloadFile overflow contentLength and bytesWritten
1 parent f1e4235 commit 30898ca

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
@@ -628,7 +628,7 @@ public void onTaskCompleted(DownloadResult res) {
628628

629629
infoMap.putInt("jobId", jobId);
630630
infoMap.putInt("statusCode", res.statusCode);
631-
infoMap.putInt("bytesWritten", res.bytesWritten);
631+
infoMap.putDouble("bytesWritten", (double)res.bytesWritten);
632632

633633
promise.resolve(infoMap);
634634
} else {
@@ -638,7 +638,7 @@ public void onTaskCompleted(DownloadResult res) {
638638
};
639639

640640
params.onDownloadBegin = new DownloadParams.OnDownloadBegin() {
641-
public void onDownloadBegin(int statusCode, int contentLength, Map<String, String> headers) {
641+
public void onDownloadBegin(int statusCode, long contentLength, Map<String, String> headers) {
642642
WritableMap headersMap = Arguments.createMap();
643643

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

650650
data.putInt("jobId", jobId);
651651
data.putInt("statusCode", statusCode);
652-
data.putInt("contentLength", contentLength);
652+
data.putDouble("contentLength", (double)contentLength);
653653
data.putMap("headers", headersMap);
654654

655655
sendEvent(getReactApplicationContext(), "DownloadBegin-" + jobId, data);
656656
}
657657
};
658658

659659
params.onDownloadProgress = new DownloadParams.OnDownloadProgress() {
660-
public void onDownloadProgress(int contentLength, int bytesWritten) {
660+
public void onDownloadProgress(long contentLength, long bytesWritten) {
661661
WritableMap data = Arguments.createMap();
662662

663663
data.putInt("jobId", jobId);
664-
data.putInt("contentLength", contentLength);
665-
data.putInt("bytesWritten", bytesWritten);
664+
data.putDouble("contentLength", (double)contentLength);
665+
data.putDouble("bytesWritten", (double)bytesWritten);
666666

667667
sendEvent(getReactApplicationContext(), "DownloadProgress-" + jobId, data);
668668
}

0 commit comments

Comments
 (0)