Browse Source

Change Android readStream API

Ben Hsieh 8 years ago
parent
commit
760abbedd3

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

207
      * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
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 1024 or 1026(base64).
209
      */
209
      */
210
-    public void readStream(String path, String encoding, int bufferSize) {
210
+    public void readStream(String path, String encoding, int bufferSize, String streamId) {
211
         RNFetchBlobFS fs = new RNFetchBlobFS(this.getReactApplicationContext());
211
         RNFetchBlobFS fs = new RNFetchBlobFS(this.getReactApplicationContext());
212
-        fs.readStream(path, encoding, bufferSize);
212
+        fs.readStream(path, encoding, bufferSize, streamId);
213
     }
213
     }
214
 
214
 
215
     @ReactMethod
215
     @ReactMethod

+ 8
- 9
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

229
      * @param encoding  File stream decoder, should be one of `base64`, `utf8`, `ascii`
229
      * @param encoding  File stream decoder, should be one of `base64`, `utf8`, `ascii`
230
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
230
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
231
      */
231
      */
232
-    public void readStream( String path, String encoding, int bufferSize) {
232
+    public void readStream(String path, String encoding, int bufferSize, final String streamId) {
233
         path = normalizePath(path);
233
         path = normalizePath(path);
234
         AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
234
         AsyncTask<String, Integer, Integer> task = new AsyncTask<String, Integer, Integer>() {
235
             @Override
235
             @Override
237
                 String path = args[0];
237
                 String path = args[0];
238
                 String encoding = args[1];
238
                 String encoding = args[1];
239
                 int bufferSize = Integer.parseInt(args[2]);
239
                 int bufferSize = Integer.parseInt(args[2]);
240
-                String eventName = "RNFetchBlobStream+" + path;
241
                 try {
240
                 try {
242
 
241
 
243
                     int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
242
                     int chunkSize = encoding.equalsIgnoreCase("base64") ? 4095 : 4096;
259
                     if (encoding.equalsIgnoreCase("utf8")) {
258
                     if (encoding.equalsIgnoreCase("utf8")) {
260
                         while ((cursor = fs.read(buffer)) != -1) {
259
                         while ((cursor = fs.read(buffer)) != -1) {
261
                             String chunk = new String(buffer, 0, cursor, "UTF-8");
260
                             String chunk = new String(buffer, 0, cursor, "UTF-8");
262
-                            emitStreamEvent(eventName, "data", chunk);
261
+                            emitStreamEvent(streamId, "data", chunk);
263
                         }
262
                         }
264
                     } else if (encoding.equalsIgnoreCase("ascii")) {
263
                     } else if (encoding.equalsIgnoreCase("ascii")) {
265
                         while ((cursor = fs.read(buffer)) != -1) {
264
                         while ((cursor = fs.read(buffer)) != -1) {
268
                             {
267
                             {
269
                                 chunk.pushInt((int)buffer[i]);
268
                                 chunk.pushInt((int)buffer[i]);
270
                             }
269
                             }
271
-                            emitStreamEvent(eventName, "data", chunk);
270
+                            emitStreamEvent(streamId, "data", chunk);
272
                         }
271
                         }
273
                     } else if (encoding.equalsIgnoreCase("base64")) {
272
                     } else if (encoding.equalsIgnoreCase("base64")) {
274
                         while ((cursor = fs.read(buffer)) != -1) {
273
                         while ((cursor = fs.read(buffer)) != -1) {
277
                                 for(int i =0;i<cursor;i++) {
276
                                 for(int i =0;i<cursor;i++) {
278
                                     copy[i] = buffer[i];
277
                                     copy[i] = buffer[i];
279
                                 }
278
                                 }
280
-                                emitStreamEvent(eventName, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
279
+                                emitStreamEvent(streamId, "data", Base64.encodeToString(copy, Base64.NO_WRAP));
281
                             }
280
                             }
282
                             else
281
                             else
283
-                                emitStreamEvent(eventName, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
282
+                                emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
284
                         }
283
                         }
285
                     } else {
284
                     } else {
286
                         String msg = "unrecognized encoding `" + encoding + "`";
285
                         String msg = "unrecognized encoding `" + encoding + "`";
287
-                        emitStreamEvent(eventName, "error", msg);
286
+                        emitStreamEvent(streamId, "error", msg);
288
                         error = true;
287
                         error = true;
289
                     }
288
                     }
290
 
289
 
291
                     if(!error)
290
                     if(!error)
292
-                        emitStreamEvent(eventName, "end", "");
291
+                        emitStreamEvent(streamId, "end", "");
293
                     fs.close();
292
                     fs.close();
294
                     buffer = null;
293
                     buffer = null;
295
 
294
 
296
                 } catch (Exception err) {
295
                 } catch (Exception err) {
297
-                    emitStreamEvent(eventName, "error", err.getLocalizedMessage());
296
+                    emitStreamEvent(streamId, "error", err.getLocalizedMessage());
298
                 }
297
                 }
299
                 return null;
298
                 return null;
300
             }
299
             }