Browse Source

Change Android fs.readStream implementation

Move readStream to thread pool
Ben Hsieh 8 years ago
parent
commit
3661446832

+ 10
- 4
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java View File

@@ -205,11 +205,17 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
205 205
     /**
206 206
      * @param path Stream file path
207 207
      * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
208
-     * @param bufferSize Stream buffer size, default to 1024 or 1026(base64).
208
+     * @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
209 209
      */
210
-    public void readStream(String path, String encoding, int bufferSize, String streamId) {
211
-        RNFetchBlobFS fs = new RNFetchBlobFS(this.getReactApplicationContext());
212
-        fs.readStream(path, encoding, bufferSize, streamId);
210
+    public void readStream(final String path, final String encoding, final int bufferSize, final String streamId) {
211
+        final ReactApplicationContext ctx = this.getReactApplicationContext();
212
+        threadPool.execute(new Runnable() {
213
+            @Override
214
+            public void run() {
215
+                RNFetchBlobFS fs = new RNFetchBlobFS(ctx);
216
+                fs.readStream(path, encoding, bufferSize, streamId);
217
+            }
218
+        });
213 219
     }
214 220
 
215 221
     @ReactMethod

+ 50
- 60
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

@@ -231,74 +231,64 @@ public class RNFetchBlobFS {
231 231
      */
232 232
     public void readStream(String path, String encoding, int bufferSize, final String streamId) {
233 233
         path = normalizePath(path);
234
-        AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
235
-            @Override
236
-            protected Integer doInBackground(String ... args) {
237
-                String path = args[0];
238
-                String encoding = args[1];
239
-                int bufferSize = Integer.parseInt(args[2]);
240
-                try {
234
+        try {
241 235
 
242
-                    int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
243
-                    if(bufferSize > 0)
244
-                        chunkSize = bufferSize;
236
+            int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
237
+            if(bufferSize > 0)
238
+                chunkSize = bufferSize;
245 239
 
246
-                    InputStream fs;
247
-                    if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
248
-                        fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
249
-                    }
250
-                    else {
251
-                        fs = new FileInputStream(new File(path));
252
-                    }
240
+            InputStream fs;
241
+            if(path.startsWith(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET)) {
242
+                fs = RNFetchBlob.RCTContext.getAssets().open(path.replace(RNFetchBlobConst.FILE_PREFIX_BUNDLE_ASSET, ""));
243
+            }
244
+            else {
245
+                fs = new FileInputStream(new File(path));
246
+            }
253 247
 
254
-                    byte[] buffer = new byte[chunkSize];
255
-                    int cursor = 0;
256
-                    boolean error = false;
248
+            byte[] buffer = new byte[chunkSize];
249
+            int cursor = 0;
250
+            boolean error = false;
257 251
 
258
-                    if (encoding.equalsIgnoreCase("utf8")) {
259
-                        while ((cursor = fs.read(buffer)) != -1) {
260
-                            String chunk = new String(buffer, 0, cursor, "UTF-8");
261
-                            emitStreamEvent(streamId, "data", chunk);
262
-                        }
263
-                    } else if (encoding.equalsIgnoreCase("ascii")) {
264
-                        while ((cursor = fs.read(buffer)) != -1) {
265
-                            WritableArray chunk = Arguments.createArray();
266
-                            for(int i =0;i<cursor;i++)
267
-                            {
268
-                                chunk.pushInt((int)buffer[i]);
269
-                            }
270
-                            emitStreamEvent(streamId, "data", chunk);
271
-                        }
272
-                    } else if (encoding.equalsIgnoreCase("base64")) {
273
-                        while ((cursor = fs.read(buffer)) != -1) {
274
-                            if(cursor < chunkSize) {
275
-                                byte [] copy = new byte[cursor];
276
-                                for(int i =0;i<cursor;i++) {
277
-                                    copy[i] = buffer[i];
278
-                                }
279
-                                emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
280
-                            }
281
-                            else
282
-                                emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
252
+            if (encoding.equalsIgnoreCase("utf8")) {
253
+                while ((cursor = fs.read(buffer)) != -1) {
254
+                    String chunk = new String(buffer, 0, cursor, "UTF-8");
255
+                    emitStreamEvent(streamId, "data", chunk);
256
+                }
257
+            } else if (encoding.equalsIgnoreCase("ascii")) {
258
+                while ((cursor = fs.read(buffer)) != -1) {
259
+                    WritableArray chunk = Arguments.createArray();
260
+                    for(int i =0;i<cursor;i++)
261
+                    {
262
+                        chunk.pushInt((int)buffer[i]);
263
+                    }
264
+                    emitStreamEvent(streamId, "data", chunk);
265
+                }
266
+            } else if (encoding.equalsIgnoreCase("base64")) {
267
+                while ((cursor = fs.read(buffer)) != -1) {
268
+                    if(cursor < chunkSize) {
269
+                        byte [] copy = new byte[cursor];
270
+                        for(int i =0;i<cursor;i++) {
271
+                            copy[i] = buffer[i];
283 272
                         }
284
-                    } else {
285
-                        String msg = "unrecognized encoding `" + encoding + "`";
286
-                        emitStreamEvent(streamId, "error", msg);
287
-                        error = true;
273
+                        emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
288 274
                     }
289
-
290
-                    if(!error)
291
-                        emitStreamEvent(streamId, "end", "");
292
-                    fs.close();
293
-                    buffer = null;
294
-
295
-                } catch (Exception err) {
296
-                    emitStreamEvent(streamId, "error", err.getLocalizedMessage());
275
+                    else
276
+                        emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
297 277
                 }
298
-                return null;
278
+            } else {
279
+                String msg = "unrecognized encoding `" + encoding + "`";
280
+                emitStreamEvent(streamId, "error", msg);
281
+                error = true;
299 282
             }
300
-        };
301
-        task.execute(path, encoding, String.valueOf(bufferSize));
283
+
284
+            if(!error)
285
+                emitStreamEvent(streamId, "end", "");
286
+            fs.close();
287
+            buffer = null;
288
+
289
+        } catch (Exception err) {
290
+            emitStreamEvent(streamId, "error", err.getLocalizedMessage());
291
+        }
302 292
     }
303 293
 
304 294
     /**