Browse Source

Add buffer size option to read stream

Ben Hsieh 8 years ago
parent
commit
4b97e0f04f
3 changed files with 23 additions and 8 deletions
  1. 8
    3
      src/index.js
  2. 2
    0
      src/ios/RNFetchBlob/RNFetchBlob.h
  3. 13
    5
      src/ios/RNFetchBlob/RNFetchBlob.m

+ 8
- 3
src/index.js View File

@@ -20,6 +20,7 @@ import base64 from 'base-64'
20 20
 // const emitter = (Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter)
21 21
 const emitter = DeviceEventEmitter
22 22
 const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
23
+const pathPrefix = Platform.OS === 'android' ? 'file://' : ''
23 24
 
24 25
 emitter.addListener("RNFetchBlobMessage", (e) => {
25 26
 
@@ -107,7 +108,11 @@ function fetch(...args:any) {
107 108
 
108 109
 }
109 110
 
110
-function openReadStream(path:string, encoding:'utf8' | 'ascii' | 'base64'):RNFetchBlobStream {
111
+function openReadStream(
112
+  path:string,
113
+  encoding:'utf8' | 'ascii' | 'base64',
114
+  bufferSize?:?number
115
+):RNFetchBlobStream {
111 116
 
112 117
   if(!path)
113 118
     throw Error('RNFetchBlob could not open file stream with empty `path`')
@@ -126,6 +131,7 @@ function openReadStream(path:string, encoding:'utf8' | 'ascii' | 'base64'):RNFet
126 131
 
127 132
   // register for file stream event
128 133
   let subscription = emitter.addListener(`RNFetchBlobStream+${path}`, (e) => {
134
+
129 135
     let {event, detail} = e
130 136
     if(stream._onData && event === 'data')
131 137
       stream._onData(detail)
@@ -142,8 +148,7 @@ function openReadStream(path:string, encoding:'utf8' | 'ascii' | 'base64'):RNFet
142 148
 
143 149
   })
144 150
 
145
-  RNFetchBlob.readStream(path, encoding)
146
-
151
+  RNFetchBlob.readStream(path, encoding, (bufferSize || "0").toString())
147 152
   return stream
148 153
 
149 154
 }

+ 2
- 0
src/ios/RNFetchBlob/RNFetchBlob.h View File

@@ -34,6 +34,7 @@ extern NSString *const FS_EVENT_ERROR;
34 34
     RCTBridge * bridge;
35 35
     Boolean isOpen;
36 36
     NSString * encoding;
37
+    int bufferSize;
37 38
     NSString * taskId;
38 39
     NSString * path;
39 40
 }
@@ -45,6 +46,7 @@ extern NSString *const FS_EVENT_ERROR;
45 46
 @property (nonatomic) NSString * encoding;
46 47
 @property (nonatomic) NSString * taskId;
47 48
 @property (nonatomic) NSString * path;
49
+@property (nonatomic) int bufferSize;
48 50
 
49 51
 + (NSString *) getTempPath;
50 52
 - (id) init;

+ 13
- 5
src/ios/RNFetchBlob/RNFetchBlob.m View File

@@ -41,6 +41,7 @@ NSString *const FS_EVENT_ERROR = @"error";
41 41
 @synthesize callback;
42 42
 @synthesize taskId;
43 43
 @synthesize path;
44
+@synthesize bufferSize;
44 45
 
45 46
 
46 47
 
@@ -115,12 +116,13 @@ NSString *const FS_EVENT_ERROR = @"error";
115 116
     }
116 117
 }
117 118
 
118
-- (void)readWithPath:(NSString *)path useEncoding:(NSString *)encoding {
119
+- (void)readWithPath:(NSString *)path useEncoding:(NSString *)encoding bufferSize:(int) bufferSize{
119 120
     
120 121
     self.inStream = [[NSInputStream alloc] initWithFileAtPath:path];
121 122
     self.inStream.delegate = self;
122 123
     self.encoding = encoding;
123 124
     self.path = path;
125
+    self.bufferSize = bufferSize;
124 126
     
125 127
     // NSStream needs a runloop so let's create a run loop for it
126 128
     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
@@ -185,9 +187,15 @@ void runOnMainQueueWithoutDeadlocking(void (^block)(void))
185 187
         case NSStreamEventHasBytesAvailable:
186 188
         {
187 189
             NSMutableData * chunkData = [[NSMutableData data] init];
188
-            uint8_t buf[1024];
190
+            NSInteger chunkSize = 1024;
191
+            if([[self.encoding lowercaseString] isEqualToString:@"base64"])
192
+                chunkSize = 1026;
193
+            if(self.bufferSize > 0)
194
+                chunkSize = self.bufferSize;
195
+            uint8_t buf[chunkSize];
189 196
             unsigned int len = 0;
190
-            len = [(NSInputStream *)stream read:buf maxLength:1024];
197
+
198
+            len = [(NSInputStream *)stream read:buf maxLength:chunkSize];
191 199
             // still have data in stream
192 200
             if(len) {
193 201
                 [chunkData appendBytes:(const void *)buf length:len];
@@ -578,9 +586,9 @@ RCT_EXPORT_METHOD(fetchBlob:(NSDictionary *)options
578 586
     });
579 587
 }
580 588
 
581
-RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding) {
589
+RCT_EXPORT_METHOD(readStream:(NSString *)path withEncoding:(NSString *)encoding bufferSize:(int)bufferSize) {
582 590
     FetchBlobFS *fileStream = [[FetchBlobFS alloc] initWithBridgeRef:self.bridge];
583
-    [fileStream readWithPath:path useEncoding:encoding];
591
+    [fileStream readWithPath:path useEncoding:encoding bufferSize:bufferSize];
584 592
 }
585 593
 
586 594
 RCT_EXPORT_METHOD(flush:(NSString *)path) {