Przeglądaj źródła

correct android readstream function

Ben Hsieh 8 lat temu
rodzic
commit
3f5c2759dc

+ 11
- 4
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java Wyświetl plik

@@ -37,7 +37,7 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
37 37
     }
38 38
 
39 39
     @ReactMethod
40
-    public ReadableArray getSystemDirs() {
40
+    public void getEnvironmentDirs(Callback callback) {
41 41
 
42 42
         WritableArray results = Arguments.createArray();
43 43
         ReactApplicationContext ctx = this.getReactApplicationContext();
@@ -47,7 +47,7 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
47 47
         results.pushString(String.valueOf(ctx.getCacheDir()));
48 48
         results.pushString(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)));
49 49
         results.pushString(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)));
50
-        return results;
50
+        callback.invoke(results);
51 51
     }
52 52
 
53 53
     @ReactMethod
@@ -65,14 +65,21 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
65 65
     }
66 66
 
67 67
     @ReactMethod
68
-    public void readStream(String path, String encoding) {
68
+    /**
69
+     * @param path Stream file path
70
+     * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
71
+     * @param bufferSize Stream buffer size, default to 1024 or 1026(base64).
72
+     */
73
+    public void readStream(String path, String encoding, String bufferSize) {
69 74
         RNFetchBlobFS fs = new RNFetchBlobFS(this.getReactApplicationContext());
70
-        fs.readStream(path, encoding);
75
+        fs.readStream(path, encoding, bufferSize);
71 76
     }
72 77
 
73 78
     @ReactMethod
74 79
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
80
+
75 81
         RNFetchBlobConfig config = new RNFetchBlobConfig(options);
82
+
76 83
         try {
77 84
             Uri uri = Uri.parse(url);
78 85
             AsyncHttpClient req = new AsyncHttpClient();

+ 5
- 4
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConfig.java Wyświetl plik

@@ -12,10 +12,11 @@ public class RNFetchBlobConfig {
12 12
     public String appendExt;
13 13
 
14 14
     RNFetchBlobConfig(ReadableMap options) {
15
-
16
-        this.fileCache = options.getBoolean("fileCache");
17
-        this.path = options.getString("path");
18
-        this.appendExt = options.getString("appendExt");
15
+        if(options == null)
16
+            return;
17
+        this.fileCache = options.hasKey("fileCache") ? options.getBoolean("fileCache") : false;
18
+            this.path = options.hasKey("path") ? options.getString("path") : null;
19
+        this.appendExt = options.hasKey("appendExt") ? options.getString("appendExt") : "";
19 20
 
20 21
     }
21 22
 

+ 28
- 14
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Wyświetl plik

@@ -9,6 +9,7 @@ import com.facebook.react.bridge.WritableMap;
9 9
 import com.facebook.react.modules.core.DeviceEventManagerModule;
10 10
 import com.loopj.android.http.Base64;
11 11
 
12
+import java.io.File;
12 13
 import java.io.FileInputStream;
13 14
 
14 15
 import cz.msebera.android.httpclient.util.EncodingUtils;
@@ -31,50 +32,63 @@ public class RNFetchBlobFS {
31 32
         this.emitter = ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
32 33
     }
33 34
     
34
-    // TODO : make it an AsyncTask
35
-    public void readStream(final String path, String encoding) {
35
+    public void readStream( String path, String encoding, String bufferSize) {
36 36
         AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
37 37
             @Override
38 38
             protected Integer doInBackground(String ... args) {
39 39
                 String path = args[0];
40 40
                 String encoding = args[1];
41
+                int bufferSize = Integer.parseInt(args[2]);
41 42
                 String eventName = "RNFetchBlobStream+" + path;
42 43
                 try {
43
-                    FileInputStream fs = mCtx.openFileInput(mCtx.getFilesDir() + "/"+ path);
44
-                    byte[] buffer = new byte[1024];
44
+
45
+                    int chunkSize = encoding.equalsIgnoreCase("base64") ? 1026 : 1024;
46
+                    if(bufferSize > 0)
47
+                        chunkSize = bufferSize;
48
+                    FileInputStream fs = new FileInputStream(new File(path));
49
+                    byte[] buffer = new byte[chunkSize];
45 50
                     int cursor = 0;
46 51
                     boolean error = false;
47 52
 
48
-                    if (encoding.toLowerCase() == "utf8") {
53
+                    if (encoding.equalsIgnoreCase("utf8")) {
49 54
                         while ((cursor = fs.read(buffer)) != -1) {
50 55
                             String chunk = new String(buffer, 0, cursor, "UTF-8");
51
-                            emitFSData(eventName, "data", chunk);
56
+                            emitStreamEvent(eventName, "data", chunk);
52 57
                         }
53
-                    } else if (encoding.toLowerCase() == "ascii") {
58
+                    } else if (encoding.equalsIgnoreCase("ascii")) {
54 59
                         while ((cursor = fs.read(buffer)) != -1) {
55 60
                             String chunk = EncodingUtils.getAsciiString(buffer, 0, cursor);
56
-                            emitFSData(eventName, "data", chunk);
61
+                            emitStreamEvent(eventName, "data", chunk);
57 62
                         }
58
-                    } else if (encoding.toLowerCase() == "base64") {
63
+                    } else if (encoding.equalsIgnoreCase("base64")) {
59 64
                         while ((cursor = fs.read(buffer)) != -1) {
60
-                            emitFSData(eventName, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
65
+                            emitStreamEvent(eventName, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
61 66
                         }
62 67
                     } else {
63 68
                         String msg = "unrecognized encoding `" + encoding + "`";
64
-                        emitFSData(eventName, "error", msg);
69
+                        emitStreamEvent(eventName, "error", msg);
65 70
                         error = true;
66 71
                     }
67 72
 
68 73
                     if(!error)
69
-                        emitFSData(eventName, "end", "");
74
+                        emitStreamEvent(eventName, "end", "");
75
+                    fs.close();
76
+
70 77
 
71 78
                 } catch (Exception err) {
72
-                    emitFSData(eventName, "error", err.getLocalizedMessage());
79
+                    emitStreamEvent(eventName, "error", err.getLocalizedMessage());
73 80
                 }
74 81
                 return null;
75 82
             }
76 83
         };
77
-        task.execute(path, encoding);
84
+        task.execute(path, encoding, bufferSize);
85
+    }
86
+
87
+    void emitStreamEvent(String streamName, String event, String data) {
88
+        WritableMap eventData = Arguments.createMap();
89
+        eventData.putString("event", event);
90
+        eventData.putString("detail", data);
91
+        this.emitter.emit(streamName, eventData);
78 92
     }
79 93
 
80 94
     void emitFSData(String taskId, String event, String data) {