瀏覽代碼

Change Android readStream API

Ben Hsieh 8 年之前
父節點
當前提交
760abbedd3

+ 2
- 2
src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java 查看文件

@@ -207,9 +207,9 @@ public class RNFetchBlob extends ReactContextBaseJavaModule {
207 207
      * @param encoding Stream encoding, should be one of `base64`, `ascii`, and `utf8`
208 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 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 215
     @ReactMethod

+ 8
- 9
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java 查看文件

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