Procházet zdrojové kódy

Add buffer size option to read stream

Ben Hsieh před 9 roky
rodič
revize
4b97e0f04f
3 změnil soubory, kde provedl 23 přidání a 8 odebrání
  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 Zobrazit soubor

20
 // const emitter = (Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter)
20
 // const emitter = (Platform.OS === 'android' ? DeviceEventEmitter : NativeAppEventEmitter)
21
 const emitter = DeviceEventEmitter
21
 const emitter = DeviceEventEmitter
22
 const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
22
 const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
23
+const pathPrefix = Platform.OS === 'android' ? 'file://' : ''
23
 
24
 
24
 emitter.addListener("RNFetchBlobMessage", (e) => {
25
 emitter.addListener("RNFetchBlobMessage", (e) => {
25
 
26
 
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
   if(!path)
117
   if(!path)
113
     throw Error('RNFetchBlob could not open file stream with empty `path`')
118
     throw Error('RNFetchBlob could not open file stream with empty `path`')
126
 
131
 
127
   // register for file stream event
132
   // register for file stream event
128
   let subscription = emitter.addListener(`RNFetchBlobStream+${path}`, (e) => {
133
   let subscription = emitter.addListener(`RNFetchBlobStream+${path}`, (e) => {
134
+
129
     let {event, detail} = e
135
     let {event, detail} = e
130
     if(stream._onData && event === 'data')
136
     if(stream._onData && event === 'data')
131
       stream._onData(detail)
137
       stream._onData(detail)
142
 
148
 
143
   })
149
   })
144
 
150
 
145
-  RNFetchBlob.readStream(path, encoding)
146
-
151
+  RNFetchBlob.readStream(path, encoding, (bufferSize || "0").toString())
147
   return stream
152
   return stream
148
 
153
 
149
 }
154
 }

+ 2
- 0
src/ios/RNFetchBlob/RNFetchBlob.h Zobrazit soubor

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

+ 13
- 5
src/ios/RNFetchBlob/RNFetchBlob.m Zobrazit soubor

41
 @synthesize callback;
41
 @synthesize callback;
42
 @synthesize taskId;
42
 @synthesize taskId;
43
 @synthesize path;
43
 @synthesize path;
44
+@synthesize bufferSize;
44
 
45
 
45
 
46
 
46
 
47
 
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
     self.inStream = [[NSInputStream alloc] initWithFileAtPath:path];
121
     self.inStream = [[NSInputStream alloc] initWithFileAtPath:path];
121
     self.inStream.delegate = self;
122
     self.inStream.delegate = self;
122
     self.encoding = encoding;
123
     self.encoding = encoding;
123
     self.path = path;
124
     self.path = path;
125
+    self.bufferSize = bufferSize;
124
     
126
     
125
     // NSStream needs a runloop so let's create a run loop for it
127
     // NSStream needs a runloop so let's create a run loop for it
126
     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
128
     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
185
         case NSStreamEventHasBytesAvailable:
187
         case NSStreamEventHasBytesAvailable:
186
         {
188
         {
187
             NSMutableData * chunkData = [[NSMutableData data] init];
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
             unsigned int len = 0;
196
             unsigned int len = 0;
190
-            len = [(NSInputStream *)stream read:buf maxLength:1024];
197
+
198
+            len = [(NSInputStream *)stream read:buf maxLength:chunkSize];
191
             // still have data in stream
199
             // still have data in stream
192
             if(len) {
200
             if(len) {
193
                 [chunkData appendBytes:(const void *)buf length:len];
201
                 [chunkData appendBytes:(const void *)buf length:len];
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
     FetchBlobFS *fileStream = [[FetchBlobFS alloc] initWithBridgeRef:self.bridge];
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
 RCT_EXPORT_METHOD(flush:(NSString *)path) {
594
 RCT_EXPORT_METHOD(flush:(NSString *)path) {