React Native FS download file to memory (dev/null) | Android | IOS

Spread the love

I had to download a file and didn’t want it to be saved (I will tell the purpose in later article). I wanted that whatever the contents that I receive are discarded.

Initially Axios was being used for the same thing. But it is better for AJAX requests because they are lightweight and not much traffic is moved through the bridge. But while downloading files, there will a lot of traffic that will be flowing through bridge.

So, we should use the React Native FS for that. But React native FS doesn’t support only downloading to files. Even if you give the ‘/dev/null’ for download path, then in Android 11+, you will be getting an error. Look at ‘What is Android 11’s equivalent of ‘/dev/null’ – Stack Overflow

So, we have to modify the implementation of ‘React-native-fs’ library. Below is the patch that I have created to discard the data.

diff --git a/node_modules/react-native-fs/android/src/main/java/com/rnfs/Downloader.java b/node_modules/react-native-fs/android/src/main/java/com/rnfs/Downloader.java
index 4da698e..8cba9f9 100644
--- a/node_modules/react-native-fs/android/src/main/java/com/rnfs/Downloader.java
+++ b/node_modules/react-native-fs/android/src/main/java/com/rnfs/Downloader.java
@@ -2,6 +2,7 @@ package com.rnfs;
 
 import java.io.FileOutputStream;
 import java.io.BufferedInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
@@ -102,7 +103,7 @@ public class Downloader extends AsyncTask<DownloadParams, long[], DownloadResult
         }
 
         input = new BufferedInputStream(connection.getInputStream(), 8 * 1024);
-        output = new FileOutputStream(param.dest);
+        output = param.dest.getPath().equals("") ? new NullOutputStream() : new FileOutputStream(param.dest);
 
         byte data[] = new byte[8 * 1024];
         long total = 0;
diff --git a/node_modules/react-native-fs/android/src/main/java/com/rnfs/NullOutputStream.java b/node_modules/react-native-fs/android/src/main/java/com/rnfs/NullOutputStream.java
new file mode 100644
index 0000000..19e374c
--- /dev/null
+++ b/node_modules/react-native-fs/android/src/main/java/com/rnfs/NullOutputStream.java
@@ -0,0 +1,10 @@
+package com.rnfs;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class NullOutputStream extends OutputStream {
+    @Override
+    public void write(int b) throws IOException {
+    }
+}

You have to use package ‘patch-package’ to apply this patch. Now you can pass empty string as the file download destination, and your download will start.

For IOS you have be aware that it is only supported in IOS 15.0+. I haven’t written the code for it but you have to use the following IOS API to make it work:

bytes(for:delegate:) | Apple Developer Documentation

You can modify the IOS implementation of ‘react-native-fs’ to use this API, if your app is only supported on IOS 15.0+.

Else you have to implement your own logic using low level API: Network | Apple Developer Documentation.

If you some lib that has already implemented this, then please comment.

Cheers And Peace out!!!