|
10 | 10 | import android.os.Environment;
|
11 | 11 | import android.os.StatFs;
|
12 | 12 | import android.os.SystemClock;
|
| 13 | +import android.support.annotation.Nullable; |
13 | 14 | import android.util.Base64;
|
14 | 15 |
|
15 | 16 | import com.RNFetchBlob.Utils.PathResolver;
|
|
22 | 23 | import com.facebook.react.bridge.WritableMap;
|
23 | 24 | import com.facebook.react.modules.core.DeviceEventManagerModule;
|
24 | 25 |
|
| 26 | +import java.io.ByteArrayOutputStream; |
25 | 27 | import java.io.File;
|
26 | 28 | import java.io.FileInputStream;
|
27 | 29 | import java.io.FileOutputStream;
|
@@ -882,4 +884,134 @@ static String normalizePath(String path) {
|
882 | 884 | return PathResolver.getRealPathFromURI(RNFetchBlob.RCTContext, uri);
|
883 | 885 | }
|
884 | 886 |
|
| 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 | + } |
885 | 1017 | }
|
0 commit comments