MCPcopy Create free account
hub / github.com/itinance/react-native-fs / Downloader

Class Downloader

android/src/main/java/com/rnfs/Downloader.java:18–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16import com.facebook.react.bridge.ReadableMapKeySetIterator;
17
18public class Downloader extends AsyncTask<DownloadParams, long[], DownloadResult> {
19 private DownloadParams mParam;
20 private AtomicBoolean mAbort = new AtomicBoolean(false);
21 DownloadResult res;
22
23 protected DownloadResult doInBackground(DownloadParams... params) {
24 mParam = params[0];
25 res = new DownloadResult();
26
27 new Thread(new Runnable() {
28 public void run() {
29 try {
30 download(mParam, res);
31 mParam.onTaskCompleted.onTaskCompleted(res);
32 } catch (Exception ex) {
33 res.exception = ex;
34 mParam.onTaskCompleted.onTaskCompleted(res);
35 }
36 }
37 }).start();
38
39 return res;
40 }
41
42 private void download(DownloadParams param, DownloadResult res) throws Exception {
43 InputStream input = null;
44 OutputStream output = null;
45 HttpURLConnection connection = null;
46
47 try {
48 connection = (HttpURLConnection)param.src.openConnection();
49
50 ReadableMapKeySetIterator iterator = param.headers.keySetIterator();
51
52 while (iterator.hasNextKey()) {
53 String key = iterator.nextKey();
54 String value = param.headers.getString(key);
55 connection.setRequestProperty(key, value);
56 }
57
58 connection.setConnectTimeout(param.connectionTimeout);
59 connection.setReadTimeout(param.readTimeout);
60 connection.connect();
61
62 int statusCode = connection.getResponseCode();
63 long lengthOfFile = getContentLength(connection);
64
65 boolean isRedirect = (
66 statusCode != HttpURLConnection.HTTP_OK &&
67 (
68 statusCode == HttpURLConnection.HTTP_MOVED_PERM ||
69 statusCode == HttpURLConnection.HTTP_MOVED_TEMP ||
70 statusCode == 307 ||
71 statusCode == 308
72 )
73 );
74
75 if (isRedirect) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected