Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit e278660

Browse files
committed
wip commit
1 parent 762c46e commit e278660

File tree

5 files changed

+651
-474
lines changed

5 files changed

+651
-474
lines changed

src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Intent;
44
import android.net.Uri;
5+
import android.support.annotation.Nullable;
56

67
import com.RNFetchBlob.Utils.RNFBCookieJar;
78
import com.facebook.react.bridge.Callback;
@@ -306,4 +307,26 @@ public void fetchBlobForm(ReadableMap options, String taskId, String method, Str
306307
new RNFetchBlobReq(options, taskId, method, url, headers, null, body, callback).run();
307308
}
308309

310+
@ReactMethod
311+
public void read(final String path, final String encoding, final int offset, final int length, final @Nullable String dest, final Promise promise) {
312+
fsThreadPool.execute(new Runnable() {
313+
@Override
314+
public void run() {
315+
RNFetchBlobFS fs = new RNFetchBlobFS(RCTContext);
316+
fs.read(path, encoding, offset, length, dest, promise);
317+
}
318+
});
319+
}
320+
321+
@ReactMethod
322+
public void write(final String path, final String data, final String encoding, final int offset, final int length, final Promise promise) {
323+
fsThreadPool.execute(new Runnable() {
324+
@Override
325+
public void run() {
326+
RNFetchBlobFS fs = new RNFetchBlobFS(RCTContext);
327+
fs.write(path, encoding, data, offset, length, promise);
328+
}
329+
});
330+
}
331+
309332
}

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobConst.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ public class RNFetchBlobConst {
88
public static final String EVENT_MESSAGE = "RNFetchBlobMessage";
99
public static final String FILE_PREFIX = "RNFetchBlob-file://";
1010
public static final String FILE_PREFIX_BUNDLE_ASSET = "bundle-assets://";
11-
public static final String FILE_PREFIX_CONTENT = "content://";
1211
public static final String DATA_ENCODE_URI = "uri";
12+
public static final String DATA_ENCODE_BASE64 = "base64";
13+
public static final String DATA_ENCODE_UTF8 = "utf8";
14+
public static final String DATA_ENCODE_ASCII = "ascii";
1315
public static final String RNFB_RESPONSE_BASE64 = "base64";
1416
public static final String RNFB_RESPONSE_UTF8 = "utf8";
1517
public static final String RNFB_RESPONSE_PATH = "path";

src/android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.os.Environment;
1111
import android.os.StatFs;
1212
import android.os.SystemClock;
13+
import android.support.annotation.Nullable;
1314
import android.util.Base64;
1415

1516
import com.RNFetchBlob.Utils.PathResolver;
@@ -22,6 +23,7 @@
2223
import com.facebook.react.bridge.WritableMap;
2324
import com.facebook.react.modules.core.DeviceEventManagerModule;
2425

26+
import java.io.ByteArrayOutputStream;
2527
import java.io.File;
2628
import java.io.FileInputStream;
2729
import java.io.FileOutputStream;
@@ -882,4 +884,134 @@ static String normalizePath(String path) {
882884
return PathResolver.getRealPathFromURI(RNFetchBlob.RCTContext, uri);
883885
}
884886

887+
/**
888+
* Read file with range
889+
* @param path The path of file to read
890+
* @param encoding Encoding of the result data should be one of `utf8` | `ascii` | `base64` | `uri`
891+
* @param offset The position the process will start to read
892+
* @param length Length of result data, in byte.
893+
* @param dest Optional, if this argument is given, the result data will be write to the `dest` as a file
894+
* @param promise JS context promise
895+
*/
896+
public void read(String path, String encoding, int offset, int length, @Nullable String dest, Promise promise) {
897+
try {
898+
File f = new File(normalizePath(path));
899+
File destFile = null;
900+
boolean destExists = true;
901+
// if `dest` is given, create destination file.
902+
if(dest != null) {
903+
destFile = new File(dest);
904+
if(!destFile.exists()) {
905+
destExists = destFile.createNewFile();
906+
}
907+
encoding = RNFetchBlobConst.DATA_ENCODE_URI;
908+
}
909+
if(!f.exists()) {
910+
promise.reject("RNFB read fail", "path : " + path + "does not exists.");
911+
return;
912+
}
913+
else if (!destExists) {
914+
promise.reject("RNFB read fail", "could not create destination file : " + dest);
915+
return;
916+
}
917+
FileInputStream in = new FileInputStream(f);
918+
OutputStream os;
919+
long bufferSize = 409500;
920+
long done = 0;
921+
int read = 0;
922+
length = length > 0 ? length : in.available() - offset;
923+
byte[] buffer = new byte[(int) bufferSize];
924+
925+
in.skip(offset);
926+
os = destFile == null ? new ByteArrayOutputStream() : new FileOutputStream(destFile);
927+
928+
929+
while((length = length - (read = in.read(buffer))) > 0) {
930+
os.write(buffer, (int) done, read);
931+
done += read;
932+
}
933+
934+
if(bufferSize + length > 0) {
935+
os.write(buffer, (int)done, (int)bufferSize + length);
936+
}
937+
938+
switch (encoding) {
939+
case RNFetchBlobConst.DATA_ENCODE_UTF8 :
940+
promise.resolve(((ByteArrayOutputStream) os).toString("UTF-8"));
941+
break;
942+
case RNFetchBlobConst.DATA_ENCODE_BASE64 :
943+
promise.resolve(Base64.encode(((ByteArrayOutputStream) os).toByteArray(), 0));
944+
break;
945+
case RNFetchBlobConst.DATA_ENCODE_ASCII :
946+
WritableArray byteArrary = Arguments.createArray();
947+
for(byte b : ((ByteArrayOutputStream) os).toByteArray()) {
948+
byteArrary.pushInt((int)b);
949+
}
950+
promise.resolve(byteArrary);
951+
break;
952+
case RNFetchBlobConst.DATA_ENCODE_URI :
953+
promise.resolve(dest);
954+
break;
955+
}
956+
in.close();
957+
os.close();
958+
959+
960+
}
961+
catch(Exception ex) {
962+
promise.reject("RNFB read error", ex.getMessage());
963+
}
964+
}
965+
966+
/**
967+
* Write file to specific offset
968+
* @param path Path of the file to write.
969+
* @param encoding Encoding of the input data.
970+
* @param data Data to write to `path`.
971+
* @param offset Offset
972+
* @param promise JS context promise
973+
*/
974+
public void write(String path, String encoding, String data, int offset, int length, Promise promise) {
975+
976+
try {
977+
File f = new File(path);
978+
979+
if(!f.exists()) {
980+
promise.reject("RNFB write failed", " path : " + path + "does not exists");
981+
return;
982+
}
983+
byte[] bytes;
984+
OutputStream os = new FileOutputStream(f);
985+
switch (encoding) {
986+
case RNFetchBlobConst.DATA_ENCODE_UTF8 :
987+
bytes = data.getBytes();
988+
os.write(bytes, offset, length);
989+
break;
990+
case RNFetchBlobConst.DATA_ENCODE_BASE64 :
991+
bytes = Base64.decode(data.getBytes(),0);
992+
os.write(bytes, offset, length);
993+
break;
994+
case RNFetchBlobConst.DATA_ENCODE_URI :
995+
FileInputStream in = new FileInputStream(new File(data));
996+
997+
int bufferSize = 102400;
998+
int read = 0;
999+
byte[] buffer = new byte[bufferSize];
1000+
int done = 0;
1001+
while((length = length - (read = in.read(buffer))) > 0) {
1002+
os.write(buffer, done, read);
1003+
done += read;
1004+
}
1005+
if(bufferSize + length > 0 ){
1006+
os.write(buffer, done, bufferSize + length );
1007+
}
1008+
os.close();
1009+
}
1010+
promise.resolve(length);
1011+
}
1012+
catch (Exception ex) {
1013+
promise.reject("RNFB write error", ex.getMessage());
1014+
}
1015+
1016+
}
8851017
}

0 commit comments

Comments
 (0)