Browse Source

Add new attribute to fs.readStream #118

Introduce new argument which can limit readStream event frequency
Ben Hsieh 8 years ago
parent
commit
016e3c49f7

+ 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 4096 or 4095(base64).
208
      * @param bufferSize Stream buffer size, default to 4096 or 4095(base64).
209
      */
209
      */
210
-    public void readStream(final String path, final String encoding, final int bufferSize, final String streamId) {
210
+    public void readStream(final String path, final String encoding, final int bufferSize, final int tick, final String streamId) {
211
         final ReactApplicationContext ctx = this.getReactApplicationContext();
211
         final ReactApplicationContext ctx = this.getReactApplicationContext();
212
         threadPool.execute(new Runnable() {
212
         threadPool.execute(new Runnable() {
213
             @Override
213
             @Override
214
             public void run() {
214
             public void run() {
215
                 RNFetchBlobFS fs = new RNFetchBlobFS(ctx);
215
                 RNFetchBlobFS fs = new RNFetchBlobFS(ctx);
216
-                fs.readStream(path, encoding, bufferSize, streamId);
216
+                fs.readStream(path, encoding, bufferSize, tick, streamId);
217
             }
217
             }
218
         });
218
         });
219
     }
219
     }

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

11
 import android.os.AsyncTask;
11
 import android.os.AsyncTask;
12
 import android.os.Environment;
12
 import android.os.Environment;
13
 import android.os.Looper;
13
 import android.os.Looper;
14
+import android.os.SystemClock;
14
 import android.provider.MediaStore;
15
 import android.provider.MediaStore;
15
 import android.util.Base64;
16
 import android.util.Base64;
16
 
17
 
229
      * @param encoding  File stream decoder, should be one of `base64`, `utf8`, `ascii`
230
      * @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`)
231
      * @param bufferSize    Buffer size of read stream, default to 4096 (4095 when encode is `base64`)
231
      */
232
      */
232
-    public void readStream(String path, String encoding, int bufferSize, final String streamId) {
233
+    public void readStream(String path, String encoding, int bufferSize, int tick, final String streamId) {
233
         path = normalizePath(path);
234
         path = normalizePath(path);
234
         try {
235
         try {
235
 
236
 
253
                 while ((cursor = fs.read(buffer)) != -1) {
254
                 while ((cursor = fs.read(buffer)) != -1) {
254
                     String chunk = new String(buffer, 0, cursor, "UTF-8");
255
                     String chunk = new String(buffer, 0, cursor, "UTF-8");
255
                     emitStreamEvent(streamId, "data", chunk);
256
                     emitStreamEvent(streamId, "data", chunk);
257
+                    if(tick > 0)
258
+                        SystemClock.sleep(tick);
256
                 }
259
                 }
257
             } else if (encoding.equalsIgnoreCase("ascii")) {
260
             } else if (encoding.equalsIgnoreCase("ascii")) {
258
                 while ((cursor = fs.read(buffer)) != -1) {
261
                 while ((cursor = fs.read(buffer)) != -1) {
262
                         chunk.pushInt((int)buffer[i]);
265
                         chunk.pushInt((int)buffer[i]);
263
                     }
266
                     }
264
                     emitStreamEvent(streamId, "data", chunk);
267
                     emitStreamEvent(streamId, "data", chunk);
268
+                    if(tick > 0)
269
+                        SystemClock.sleep(tick);
265
                 }
270
                 }
266
             } else if (encoding.equalsIgnoreCase("base64")) {
271
             } else if (encoding.equalsIgnoreCase("base64")) {
267
                 while ((cursor = fs.read(buffer)) != -1) {
272
                 while ((cursor = fs.read(buffer)) != -1) {
274
                     }
279
                     }
275
                     else
280
                     else
276
                         emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
281
                         emitStreamEvent(streamId, "data", Base64.encodeToString(buffer, Base64.NO_WRAP));
282
+                    if(tick > 0)
283
+                        SystemClock.sleep(tick);
277
                 }
284
                 }
278
             } else {
285
             } else {
279
                 String msg = "unrecognized encoding `" + encoding + "`";
286
                 String msg = "unrecognized encoding `" + encoding + "`";

+ 16
- 1
src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java View File

17
 import com.facebook.react.bridge.ReadableArray;
17
 import com.facebook.react.bridge.ReadableArray;
18
 import com.facebook.react.bridge.ReadableMap;
18
 import com.facebook.react.bridge.ReadableMap;
19
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
19
 import com.facebook.react.bridge.ReadableMapKeySetIterator;
20
+import com.facebook.react.bridge.WritableArray;
20
 import com.facebook.react.bridge.WritableMap;
21
 import com.facebook.react.bridge.WritableMap;
21
 import com.facebook.react.modules.core.DeviceEventManagerModule;
22
 import com.facebook.react.modules.core.DeviceEventManagerModule;
22
 
23
 
32
 import java.nio.charset.CharacterCodingException;
33
 import java.nio.charset.CharacterCodingException;
33
 import java.nio.charset.Charset;
34
 import java.nio.charset.Charset;
34
 import java.nio.charset.CharsetEncoder;
35
 import java.nio.charset.CharsetEncoder;
36
+import java.util.ArrayList;
35
 import java.util.HashMap;
37
 import java.util.HashMap;
36
 import java.util.concurrent.TimeUnit;
38
 import java.util.concurrent.TimeUnit;
37
 
39
 
87
     ResponseType responseType;
89
     ResponseType responseType;
88
     WritableMap respInfo;
90
     WritableMap respInfo;
89
     boolean timeout = false;
91
     boolean timeout = false;
92
+    ArrayList<String> redirects = new ArrayList<>();
90
 
93
 
91
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
94
     public RNFetchBlobReq(ReadableMap options, String taskId, String method, String url, ReadableMap headers, String body, ReadableArray arrayBody, final Callback callback) {
92
         this.method = method.toUpperCase();
95
         this.method = method.toUpperCase();
277
             }
280
             }
278
 
281
 
279
             final Request req = builder.build();
282
             final Request req = builder.build();
280
-
283
+            clientBuilder.addNetworkInterceptor(new Interceptor() {
284
+                @Override
285
+                public Response intercept(Chain chain) throws IOException {
286
+                        redirects.add(chain.request().url().toString());
287
+                        return chain.proceed(chain.request());
288
+                    }
289
+            });
281
             // Add request interceptor for upload progress event
290
             // Add request interceptor for upload progress event
282
             clientBuilder.addInterceptor(new Interceptor() {
291
             clientBuilder.addInterceptor(new Interceptor() {
283
                 @Override
292
                 @Override
330
             clientBuilder.retryOnConnectionFailure(false);
339
             clientBuilder.retryOnConnectionFailure(false);
331
             clientBuilder.followRedirects(true);
340
             clientBuilder.followRedirects(true);
332
 
341
 
342
+
333
             OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();
343
             OkHttpClient client = clientBuilder.retryOnConnectionFailure(true).build();
334
             Call call =  client.newCall(req);
344
             Call call =  client.newCall(req);
335
             taskTable.put(taskId, call);
345
             taskTable.put(taskId, call);
508
         for(int i =0;i< resp.headers().size();i++) {
518
         for(int i =0;i< resp.headers().size();i++) {
509
             headers.putString(resp.headers().name(i), resp.headers().value(i));
519
             headers.putString(resp.headers().name(i), resp.headers().value(i));
510
         }
520
         }
521
+        WritableArray redirectList = Arguments.createArray();
522
+        for(String r : redirects) {
523
+                redirectList.pushString(r);
524
+        }
525
+        info.putArray("redirects", redirectList);
511
         info.putMap("headers", headers);
526
         info.putMap("headers", headers);
512
         Headers h = resp.headers();
527
         Headers h = resp.headers();
513
         if(isBlobResp) {
528
         if(isBlobResp) {

+ 4
- 1
src/fs.js View File

113
   path : string,
113
   path : string,
114
   encoding : 'utf8' | 'ascii' | 'base64',
114
   encoding : 'utf8' | 'ascii' | 'base64',
115
   bufferSize? : ?number
115
   bufferSize? : ?number
116
+  tick? : ?number
116
 ):Promise<RNFetchBlobReadStream> {
117
 ):Promise<RNFetchBlobReadStream> {
117
-  return Promise.resolve(new RNFetchBlobReadStream(path, encoding, bufferSize))
118
+  if(!tick)
119
+    tick = -1
120
+  return Promise.resolve(new RNFetchBlobReadStream(path, encoding, bufferSize, tick))
118
 }
121
 }
119
 
122
 
120
 /**
123
 /**

+ 2
- 2
src/ios/RNFetchBlob/RNFetchBlob.m View File

374
 })
374
 })
375
 
375
 
376
 #pragma mark - fs.readStream
376
 #pragma mark - fs.readStream
377
-RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize streamId:(NSString *)streamId
377
+RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize tick:(int)tick streamId:(NSString *)streamId
378
 {
378
 {
379
 
379
 
380
 //    RNFetchBlobFS *fileStream = [[RNFetchBlobFS alloc] initWithBridgeRef:self.bridge];
380
 //    RNFetchBlobFS *fileStream = [[RNFetchBlobFS alloc] initWithBridgeRef:self.bridge];
386
     }
386
     }
387
     
387
     
388
 //    [fileStream readWithPath:path useEncoding:encoding bufferSize:bufferSize];
388
 //    [fileStream readWithPath:path useEncoding:encoding bufferSize:bufferSize];
389
-    [RNFetchBlobFS readStream:path encoding:encoding bufferSize:bufferSize streamId:streamId bridgeRef:_bridge];
389
+    [RNFetchBlobFS readStream:path encoding:encoding bufferSize:bufferSize tick:tick streamId:streamId bridgeRef:_bridge];
390
 })
390
 })
391
 
391
 
392
 #pragma mark - fs.getEnvionmentDirs
392
 #pragma mark - fs.getEnvionmentDirs

+ 1
- 1
src/ios/RNFetchBlobFS.h View File

64
      rejecter:(RCTPromiseRejectBlock)reject;
64
      rejecter:(RCTPromiseRejectBlock)reject;
65
 //+ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append;
65
 //+ (void) writeFileFromFile:(NSString *)src toFile:(NSString *)dest append:(BOOL)append;
66
 + (void) writeAssetToPath:(ALAssetRepresentation * )rep dest:(NSString *)dest;
66
 + (void) writeAssetToPath:(ALAssetRepresentation * )rep dest:(NSString *)dest;
67
-+ (void) readStream:(NSString *)uri encoding:(NSString * )encoding bufferSize:(int)bufferSize streamId:(NSString *)streamId bridgeRef:(RCTBridge *)bridgeRef;
67
++ (void) readStream:(NSString *)uri encoding:(NSString * )encoding bufferSize:(int)bufferSize tick:(int)tick streamId:(NSString *)streamId bridgeRef:(RCTBridge *)bridgeRef;
68
 
68
 
69
 // constructor
69
 // constructor
70
 - (id) init;
70
 - (id) init;

+ 1
- 0
src/ios/RNFetchBlobFS.m View File

122
 + (void) readStream:(NSString *)uri
122
 + (void) readStream:(NSString *)uri
123
            encoding:(NSString * )encoding
123
            encoding:(NSString * )encoding
124
          bufferSize:(int)bufferSize
124
          bufferSize:(int)bufferSize
125
+               tick:(int)tick
125
            streamId:(NSString *)streamId
126
            streamId:(NSString *)streamId
126
           bridgeRef:(RCTBridge *)bridgeRef
127
           bridgeRef:(RCTBridge *)bridgeRef
127
 {
128
 {