Skip to content

Commit

Permalink
add read method with lenght and position params
Browse files Browse the repository at this point in the history
  • Loading branch information
simitii committed Jul 16, 2017
1 parent ec8dfc3 commit a39c22b
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
30 changes: 30 additions & 0 deletions FS.common.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,36 @@ var RNFS = {
return readFileGeneric(filepath, encodingOrOptions, RNFSManager.readFile);
},

read(filepath: string, length = 0, position = 0, encodingOrOptions?: any): Promise<string> {
var options = {
encoding: 'utf8'
};

if (encodingOrOptions) {
if (typeof encodingOrOptions === 'string') {
options.encoding = encodingOrOptions;
} else if (typeof encodingOrOptions === 'object') {
options = encodingOrOptions;
}
}

return RNFSManager.read(normalizeFilePath(filepath), length, position).then((b64) => {
var contents;

if (options.encoding === 'utf8') {
contents = utf8.decode(base64.decode(b64));
} else if (options.encoding === 'ascii') {
contents = base64.decode(b64);
} else if (options.encoding === 'base64') {
contents = b64;
} else {
throw new Error('Invalid encoding type "' + String(options.encoding) + '"');
}

return contents;
});
},

// Android only
readFileAssets(filepath: string, encodingOrOptions?: any): Promise<string> {
if (!RNFSManager.readFileAssets) {
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ Reads the file at `path` and return contents. `encoding` can be one of `utf8` (d

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

### `read(filepath: string, length = 0, position = 0, encodingOrOptions?: any): Promise<string>`

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.

Note: reading big files piece by piece using this method may be useful in terms of performance.

### `readFileAssets(filepath:string, encoding?: string): Promise<string>`

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.
Expand Down
45 changes: 45 additions & 0 deletions RNFSManager.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,51 @@ - (dispatch_queue_t)methodQueue
resolve(base64Content);
}

RCT_EXPORT_METHOD(read:(NSString *)filepath
length: (NSInteger *)length
position: (NSInteger *)position
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filepath];

if (!fileExists) {
return reject(@"ENOENT", [NSString stringWithFormat:@"ENOENT: no such file or directory, open '%@'", filepath], nil);
}

NSError *error = nil;

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filepath error:&error];

if (error) {
return [self reject:reject withError:error];
}

if ([attributes objectForKey:NSFileType] == NSFileTypeDirectory) {
return reject(@"EISDIR", @"EISDIR: illegal operation on a directory, read", nil);
}

// Open the file handler.
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:filepath];
if (file == nil) {
return reject(@"EISDIR", @"EISDIR: Could not open file for reading", nil);
}

// Seek to the position if there is one.
[file seekToFileOffset: (int)position];

NSData *content;
if ((int)length > 0) {
content = [file readDataOfLength: (int)length];
} else {
content = [file readDataToEndOfFile];
}

NSString *base64Content = [content base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

resolve(base64Content);
}

RCT_EXPORT_METHOD(hash:(NSString *)filepath
algorithm:(NSString *)algorithm
resolver:(RCTPromiseResolveBlock)resolve
Expand Down
29 changes: 29 additions & 0 deletions android/src/main/java/com/rnfs/RNFSManager.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,35 @@ public void readFile(String filepath, Promise promise) {
}
}

@ReactMethod
public void read(String filepath, int length, int position, Promise promise){
try {
File file = new File(filepath);

if (file.isDirectory()) {
rejectFileIsDirectory(promise);
return;
}

if (!file.exists()) {
rejectFileNotFound(promise, filepath);
return;
}

FileInputStream inputStream = new FileInputStream(filepath);
byte[] buffer = new byte[length];
inputStream.skip(position);
inputStream.read(buffer,0,length);

String base64Content = Base64.encodeToString(buffer, Base64.NO_WRAP);

promise.resolve(base64Content);
} catch (Exception ex) {
ex.printStackTrace();
reject(promise, filepath, ex);
}
}

@ReactMethod
public void readFileAssets(String filepath, Promise promise) {
InputStream stream = null;
Expand Down

0 comments on commit a39c22b

Please sign in to comment.