Skip to content

Commit a39c22b

Browse files
committed
add read method with lenght and position params
1 parent ec8dfc3 commit a39c22b

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

FS.common.js

100644100755
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,36 @@ var RNFS = {
260260
return readFileGeneric(filepath, encodingOrOptions, RNFSManager.readFile);
261261
},
262262

263+
read(filepath: string, length = 0, position = 0, encodingOrOptions?: any): Promise<string> {
264+
var options = {
265+
encoding: 'utf8'
266+
};
267+
268+
if (encodingOrOptions) {
269+
if (typeof encodingOrOptions === 'string') {
270+
options.encoding = encodingOrOptions;
271+
} else if (typeof encodingOrOptions === 'object') {
272+
options = encodingOrOptions;
273+
}
274+
}
275+
276+
return RNFSManager.read(normalizeFilePath(filepath), length, position).then((b64) => {
277+
var contents;
278+
279+
if (options.encoding === 'utf8') {
280+
contents = utf8.decode(base64.decode(b64));
281+
} else if (options.encoding === 'ascii') {
282+
contents = base64.decode(b64);
283+
} else if (options.encoding === 'base64') {
284+
contents = b64;
285+
} else {
286+
throw new Error('Invalid encoding type "' + String(options.encoding) + '"');
287+
}
288+
289+
return contents;
290+
});
291+
},
292+
263293
// Android only
264294
readFileAssets(filepath: string, encodingOrOptions?: any): Promise<string> {
265295
if (!RNFSManager.readFileAssets) {

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ Reads the file at `path` and return contents. `encoding` can be one of `utf8` (d
363363

364364
Note: you will take quite a performance hit if you are reading big files
365365

366+
### `read(filepath: string, length = 0, position = 0, encodingOrOptions?: any): Promise<string>`
367+
368+
Reads length bits from the given position of the file and returns contents. `encoding` can be one of `utf8` (default), `ascii`, `base64`. Use `base64` for reading binary files.
369+
370+
Note: reading big files piece by piece using this method may be useful in terms of performance.
371+
366372
### `readFileAssets(filepath:string, encoding?: string): Promise<string>`
367373

368374
Reads the file at `path` in the Android app's assets folder and return contents. `encoding` can be one of `utf8` (default), `ascii`, `base64`. Use `base64` for reading binary files.

RNFSManager.m

100644100755
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,51 @@ - (dispatch_queue_t)methodQueue
261261
resolve(base64Content);
262262
}
263263

264+
RCT_EXPORT_METHOD(read:(NSString *)filepath
265+
length: (NSInteger *)length
266+
position: (NSInteger *)position
267+
resolver:(RCTPromiseResolveBlock)resolve
268+
rejecter:(RCTPromiseRejectBlock)reject)
269+
{
270+
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath];
271+
272+
if (!fileExists) {
273+
return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil);
274+
}
275+
276+
NSError *error = nil;
277+
278+
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error];
279+
280+
if (error) {
281+
return [self reject:reject withError:error];
282+
}
283+
284+
if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) {
285+
return reject(@"EISDIR", @"EISDIR: illegal operation on a directory, read", nil);
286+
}
287+
288+
// Open the file handler.
289+
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:filepath];
290+
if (file == nil) {
291+
return reject(@"EISDIR", @"EISDIR: Could not open file for reading", nil);
292+
}
293+
294+
// Seek to the position if there is one.
295+
[file seekToFileOffset: (int)position];
296+
297+
NSData *content;
298+
if ((int)length > 0) {
299+
content = [file readDataOfLength: (int)length];
300+
} else {
301+
content = [file readDataToEndOfFile];
302+
}
303+
304+
NSString *base64Content = [content base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
305+
306+
resolve(base64Content);
307+
}
308+
264309
RCT_EXPORT_METHOD(hash:(NSString *)filepath
265310
algorithm:(NSString *)algorithm
266311
resolver:(RCTPromiseResolveBlock)resolve

android/src/main/java/com/rnfs/RNFSManager.java

100644100755
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,35 @@ public void readFile(String filepath, Promise promise) {
150150
}
151151
}
152152

153+
@ReactMethod
154+
public void read(String filepath, int length, int position, Promise promise){
155+
try {
156+
File file = new File(filepath);
157+
158+
if (file.isDirectory()) {
159+
rejectFileIsDirectory(promise);
160+
return;
161+
}
162+
163+
if (!file.exists()) {
164+
rejectFileNotFound(promise, filepath);
165+
return;
166+
}
167+
168+
FileInputStream inputStream = new FileInputStream(filepath);
169+
byte[] buffer = new byte[length];
170+
inputStream.skip(position);
171+
inputStream.read(buffer,0,length);
172+
173+
String base64Content = Base64.encodeToString(buffer, Base64.NO_WRAP);
174+
175+
promise.resolve(base64Content);
176+
} catch (Exception ex) {
177+
ex.printStackTrace();
178+
reject(promise, filepath, ex);
179+
}
180+
}
181+
153182
@ReactMethod
154183
public void readFileAssets(String filepath, Promise promise) {
155184
InputStream stream = null;

0 commit comments

Comments
 (0)