Skip to content

SeekTo crash fix when you was trying to partially read large file which is over 2 GB #1250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions RNFSManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ + (BOOL)requiresMainQueueSetup

RCT_EXPORT_METHOD(write:(NSString *)filepath
contents:(NSString *)base64Content
position:(NSInteger)position
position:(NSNumber *)position
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
Expand All @@ -197,8 +197,8 @@ + (BOOL)requiresMainQueueSetup
@try {
NSFileHandle *fH = [NSFileHandle fileHandleForUpdatingAtPath:filepath];

if (position >= 0) {
[fH seekToFileOffset:position];
if ([position unsignedLongLongValue] >= 0) {
[fH seekToFileOffset: [position unsignedLongLongValue]];
} else {
[fH seekToEndOfFile];
}
Expand Down Expand Up @@ -294,8 +294,8 @@ + (BOOL)requiresMainQueueSetup
}

RCT_EXPORT_METHOD(read:(NSString *)filepath
length: (NSInteger *)length
position: (NSInteger *)position
length: (NSNumber *)length
position: (NSNumber *)position
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
Expand Down Expand Up @@ -324,11 +324,11 @@ + (BOOL)requiresMainQueueSetup
}

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

NSData *content;
if ((int)length > 0) {
content = [file readDataOfLength: (int)length];
if ([length intValue] > 0) {
content = [file readDataOfLength: [length intValue]];
} else {
content = [file readDataToEndOfFile];
}
Expand Down
8 changes: 4 additions & 4 deletions android/src/main/java/com/rnfs/RNFSManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public void appendFile(String filepath, String base64Content, Promise promise) {
}

@ReactMethod
public void write(String filepath, String base64Content, int position, Promise promise) {
public void write(String filepath, String base64Content, double position, Promise promise) {
try {
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);

Expand All @@ -196,7 +196,7 @@ public void write(String filepath, String base64Content, int position, Promise p
outputStream.close();
} else {
RandomAccessFile file = new RandomAccessFile(filepath, "rw");
file.seek(position);
file.seek((long) position);
file.write(bytes);
file.close();
}
Expand Down Expand Up @@ -234,11 +234,11 @@ public void readFile(String filepath, Promise promise) {
}

@ReactMethod
public void read(String filepath, int length, int position, Promise promise) {
public void read(String filepath, int length, double position, Promise promise) {
try {
InputStream inputStream = getInputStream(filepath);
byte[] buffer = new byte[length];
inputStream.skip(position);
inputStream.skip((long) position);
int bytesRead = inputStream.read(buffer, 0, length);

String base64Content = Base64.encodeToString(buffer, 0, bytesRead, Base64.NO_WRAP);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-fs",
"version": "2.20.0",
"version": "2.20.1",
"description": "Native filesystem access for react-native",
"main": "FS.common.js",
"typings": "index.d.ts",
Expand Down