Skip to content

Commit

Permalink
Merge pull request #583 from jonnynfb/fix-Android-downloadFile-overflow
Browse files Browse the repository at this point in the history
fix Android downloadFile overflow contentLength and bytesWritten
  • Loading branch information
itinance authored Dec 19, 2018
2 parents e46f687 + 30898ca commit fe5d1c7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions android/src/main/java/com/rnfs/DownloadParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public interface OnTaskCompleted {
}

public interface OnDownloadBegin {
void onDownloadBegin(int statusCode, int contentLength, Map<String, String> headers);
void onDownloadBegin(int statusCode, long contentLength, Map<String, String> headers);
}

public interface OnDownloadProgress {
void onDownloadProgress(int contentLength, int bytesWritten);
void onDownloadProgress(long contentLength, long bytesWritten);
}

public URL src;
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/java/com/rnfs/DownloadResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class DownloadResult {
public int statusCode;
public int bytesWritten;
public long bytesWritten;
public Exception exception;
}
14 changes: 7 additions & 7 deletions android/src/main/java/com/rnfs/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.facebook.react.bridge.ReadableMapKeySetIterator;

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

int statusCode = connection.getResponseCode();
int lengthOfFile = connection.getContentLength();
long lengthOfFile = connection.getContentLengthLong();

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

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

byte data[] = new byte[8 * 1024];
int total = 0;
long total = 0;
int count;
double lastProgressValue = 0;

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

total += count;
if (param.progressDivider <= 0) {
publishProgress(new int[]{lengthOfFile, total});
publishProgress(new long[]{lengthOfFile, total});
} else {
double progress = Math.round(((double) total * 100) / lengthOfFile);
if (progress % param.progressDivider == 0) {
if ((progress != lastProgressValue) || (total == lengthOfFile)) {
Log.d("Downloader", "EMIT: " + String.valueOf(progress) + ", TOTAL:" + String.valueOf(total));
lastProgressValue = progress;
publishProgress(new int[]{lengthOfFile, total});
publishProgress(new long[]{lengthOfFile, total});
}
}
}
Expand All @@ -146,7 +146,7 @@ protected void stop() {
}

@Override
protected void onProgressUpdate(int[]... values) {
protected void onProgressUpdate(long[]... values) {
super.onProgressUpdate(values);
mParam.onDownloadProgress.onDownloadProgress(values[0][0], values[0][1]);
}
Expand Down
12 changes: 6 additions & 6 deletions android/src/main/java/com/rnfs/RNFSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ public void onTaskCompleted(DownloadResult res) {

infoMap.putInt("jobId", jobId);
infoMap.putInt("statusCode", res.statusCode);
infoMap.putInt("bytesWritten", res.bytesWritten);
infoMap.putDouble("bytesWritten", (double)res.bytesWritten);

promise.resolve(infoMap);
} else {
Expand All @@ -664,7 +664,7 @@ public void onTaskCompleted(DownloadResult res) {
};

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

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

data.putInt("jobId", jobId);
data.putInt("statusCode", statusCode);
data.putInt("contentLength", contentLength);
data.putDouble("contentLength", (double)contentLength);
data.putMap("headers", headersMap);

sendEvent(getReactApplicationContext(), "DownloadBegin-" + jobId, data);
}
};

params.onDownloadProgress = new DownloadParams.OnDownloadProgress() {
public void onDownloadProgress(int contentLength, int bytesWritten) {
public void onDownloadProgress(long contentLength, long bytesWritten) {
WritableMap data = Arguments.createMap();

data.putInt("jobId", jobId);
data.putInt("contentLength", contentLength);
data.putInt("bytesWritten", bytesWritten);
data.putDouble("contentLength", (double)contentLength);
data.putDouble("bytesWritten", (double)bytesWritten);

sendEvent(getReactApplicationContext(), "DownloadProgress-" + jobId, data);
}
Expand Down

0 comments on commit fe5d1c7

Please sign in to comment.