Skip to content

Commit

Permalink
Added getContentLength with backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
anshul-kai authored Dec 21, 2018
1 parent c7342c5 commit 48eca16
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions android/src/main/java/com/rnfs/Downloader.java
Original file line number Diff line number Diff line change
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 = getContentLength(connection);

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 = getContentLength(connection);
}
if(statusCode >= 200 && statusCode < 300) {
Map<String, List<String>> headers = connection.getHeaderFields();
Expand Down Expand Up @@ -141,6 +141,13 @@ private void download(DownloadParams param, DownloadResult res) throws Exception
}
}

private long getContentLength(HttpURLConnection connection){
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
return connection.getContentLengthLong();
}
return connection.getContentLength();
}

protected void stop() {
mAbort.set(true);
}
Expand Down

0 comments on commit 48eca16

Please sign in to comment.