Przeglądaj źródła

correct android readstream function

Ben Hsieh 9 lat temu
rodzic
commit
3f5c2759dc

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

37
     }
37
     }
38
 
38
 
39
     @ReactMethod
39
     @ReactMethod
40
-    public ReadableArray getSystemDirs() {
40
+    public void getEnvironmentDirs(Callback callback) {
41
 
41
 
42
         WritableArray results = Arguments.createArray();
42
         WritableArray results = Arguments.createArray();
43
         ReactApplicationContext ctx = this.getReactApplicationContext();
43
         ReactApplicationContext ctx = this.getReactApplicationContext();
47
         results.pushString(String.valueOf(ctx.getCacheDir()));
47
         results.pushString(String.valueOf(ctx.getCacheDir()));
48
         results.pushString(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)));
48
         results.pushString(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)));
49
         results.pushString(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)));
49
         results.pushString(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)));
50
-        return results;
50
+        callback.invoke(results);
51
     }
51
     }
52
 
52
 
53
     @ReactMethod
53
     @ReactMethod
65
     }
65
     }
66
 
66
 
67
     @ReactMethod
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
         RNFetchBlobFS fs = new RNFetchBlobFS(this.getReactApplicationContext());
74
         RNFetchBlobFS fs = new RNFetchBlobFS(this.getReactApplicationContext());
70
-        fs.readStream(path, encoding);
75
+        fs.readStream(path, encoding, bufferSize);
71
     }
76
     }
72
 
77
 
73
     @ReactMethod
78
     @ReactMethod
74
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
79
     public void fetchBlob(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, final Callback callback) {
80
+
75
         RNFetchBlobConfig config = new RNFetchBlobConfig(options);
81
         RNFetchBlobConfig config = new RNFetchBlobConfig(options);
82
+
76
         try {
83
         try {
77
             Uri uri = Uri.parse(url);
84
             Uri uri = Uri.parse(url);
78
             AsyncHttpClient req = new AsyncHttpClient();
85
             AsyncHttpClient req = new AsyncHttpClient();

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

12
     public String appendExt;
12
     public String appendExt;
13
 
13
 
14
     RNFetchBlobConfig(ReadableMap options) {
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
 import com.facebook.react.modules.core.DeviceEventManagerModule;
9
 import com.facebook.react.modules.core.DeviceEventManagerModule;
10
 import com.loopj.android.http.Base64;
10
 import com.loopj.android.http.Base64;
11
 
11
 
12
+import java.io.File;
12
 import java.io.FileInputStream;
13
 import java.io.FileInputStream;
13
 
14
 
14
 import cz.msebera.android.httpclient.util.EncodingUtils;
15
 import cz.msebera.android.httpclient.util.EncodingUtils;
31
         this.emitter = ctx.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
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
         AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
36
         AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
37
             @Override
37
             @Override
38
             protected Integer doInBackground(String ... args) {
38
             protected Integer doInBackground(String ... args) {
39
                 String path = args[0];
39
                 String path = args[0];
40
                 String encoding = args[1];
40
                 String encoding = args[1];
41
+                int bufferSize = Integer.parseInt(args[2]);
41
                 String eventName = "RNFetchBlobStream+" + path;
42
                 String eventName = "RNFetchBlobStream+" + path;
42
                 try {
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
                     int cursor = 0;
50
                     int cursor = 0;
46
                     boolean error = false;
51
                     boolean error = false;
47
 
52
 
48
-                    if (encoding.toLowerCase() == "utf8") {
53
+                    if (encoding.equalsIgnoreCase("utf8")) {
49
                         while ((cursor = fs.read(buffer)) != -1) {
54
                         while ((cursor = fs.read(buffer)) != -1) {
50
                             String chunk = new String(buffer, 0, cursor, "UTF-8");
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
                         while ((cursor = fs.read(buffer)) != -1) {
59
                         while ((cursor = fs.read(buffer)) != -1) {
55
                             String chunk = EncodingUtils.getAsciiString(buffer, 0, cursor);
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
                         while ((cursor = fs.read(buffer)) != -1) {
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
                     } else {
67
                     } else {
63
                         String msg = "unrecognized encoding `" + encoding + "`";
68
                         String msg = "unrecognized encoding `" + encoding + "`";
64
-                        emitFSData(eventName, "error", msg);
69
+                        emitStreamEvent(eventName, "error", msg);
65
                         error = true;
70
                         error = true;
66
                     }
71
                     }
67
 
72
 
68
                     if(!error)
73
                     if(!error)
69
-                        emitFSData(eventName, "end", "");
74
+                        emitStreamEvent(eventName, "end", "");
75
+                    fs.close();
76
+
70
 
77
 
71
                 } catch (Exception err) {
78
                 } catch (Exception err) {
72
-                    emitFSData(eventName, "error", err.getLocalizedMessage());
79
+                    emitStreamEvent(eventName, "error", err.getLocalizedMessage());
73
                 }
80
                 }
74
                 return null;
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
     void emitFSData(String taskId, String event, String data) {
94
     void emitFSData(String taskId, String event, String data) {