|
13 | 13 | import android.support.annotation.Nullable;
|
14 | 14 | import android.util.Base64;
|
15 | 15 |
|
| 16 | +import com.RNFetchBlob.Utils.DataConverter; |
16 | 17 | import com.RNFetchBlob.Utils.PathResolver;
|
17 | 18 | import com.facebook.react.bridge.Arguments;
|
18 | 19 | import com.facebook.react.bridge.Callback;
|
|
23 | 24 | import com.facebook.react.bridge.WritableMap;
|
24 | 25 | import com.facebook.react.modules.core.DeviceEventManagerModule;
|
25 | 26 |
|
26 |
| -import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.ByteArrayInputStream; |
27 | 28 | import java.io.File;
|
28 | 29 | import java.io.FileInputStream;
|
| 30 | +import java.io.FileNotFoundException; |
29 | 31 | import java.io.FileOutputStream;
|
30 | 32 | import java.io.IOException;
|
31 | 33 | import java.io.InputStream;
|
@@ -897,134 +899,24 @@ static String normalizePath(String path) {
|
897 | 899 | return PathResolver.getRealPathFromURI(RNFetchBlob.RCTContext, uri);
|
898 | 900 | }
|
899 | 901 |
|
900 |
| - /** |
901 |
| - * Read file with range |
902 |
| - * @param path The path of file to read |
903 |
| - * @param encoding Encoding of the result data should be one of `utf8` | `ascii` | `base64` | `uri` |
904 |
| - * @param offset The position the process will start to read |
905 |
| - * @param length Length of result data, in byte. |
906 |
| - * @param dest Optional, if this argument is given, the result data will be write to the `dest` as a file |
907 |
| - * @param promise JS context promise |
908 |
| - */ |
909 |
| - public void read(String path, String encoding, int offset, int length, @Nullable String dest, Promise promise) { |
910 |
| - try { |
911 |
| - File f = new File(normalizePath(path)); |
912 |
| - File destFile = null; |
913 |
| - boolean destExists = true; |
914 |
| - // if `dest` is given, create destination file. |
915 |
| - if(dest != null) { |
916 |
| - destFile = new File(dest); |
917 |
| - if(!destFile.exists()) { |
918 |
| - destExists = destFile.createNewFile(); |
919 |
| - } |
920 |
| - encoding = RNFetchBlobConst.DATA_ENCODE_URI; |
921 |
| - } |
922 |
| - if(!f.exists()) { |
923 |
| - promise.reject("RNFB read fail", "path : " + path + "does not exists."); |
924 |
| - return; |
925 |
| - } |
926 |
| - else if (!destExists) { |
927 |
| - promise.reject("RNFB read fail", "could not create destination file : " + dest); |
928 |
| - return; |
929 |
| - } |
930 |
| - FileInputStream in = new FileInputStream(f); |
931 |
| - OutputStream os; |
932 |
| - long bufferSize = 409500; |
933 |
| - long done = 0; |
934 |
| - int read = 0; |
935 |
| - length = length > 0 ? length : in.available() - offset; |
936 |
| - byte[] buffer = new byte[(int) bufferSize]; |
937 |
| - |
938 |
| - in.skip(offset); |
939 |
| - os = destFile == null ? new ByteArrayOutputStream() : new FileOutputStream(destFile); |
940 |
| - |
941 |
| - |
942 |
| - while((length = length - (read = in.read(buffer))) > 0) { |
943 |
| - os.write(buffer, (int) done, read); |
944 |
| - done += read; |
945 |
| - } |
946 |
| - |
947 |
| - if(bufferSize + length > 0) { |
948 |
| - os.write(buffer, (int)done, (int)bufferSize + length); |
949 |
| - } |
950 |
| - |
951 |
| - switch (encoding) { |
952 |
| - case RNFetchBlobConst.DATA_ENCODE_UTF8 : |
953 |
| - promise.resolve(((ByteArrayOutputStream) os).toString("UTF-8")); |
954 |
| - break; |
955 |
| - case RNFetchBlobConst.DATA_ENCODE_BASE64 : |
956 |
| - promise.resolve(Base64.encode(((ByteArrayOutputStream) os).toByteArray(), 0)); |
957 |
| - break; |
958 |
| - case RNFetchBlobConst.DATA_ENCODE_ASCII : |
959 |
| - WritableArray byteArrary = Arguments.createArray(); |
960 |
| - for(byte b : ((ByteArrayOutputStream) os).toByteArray()) { |
961 |
| - byteArrary.pushInt((int)b); |
962 |
| - } |
963 |
| - promise.resolve(byteArrary); |
964 |
| - break; |
965 |
| - case RNFetchBlobConst.DATA_ENCODE_URI : |
966 |
| - promise.resolve(dest); |
967 |
| - break; |
968 |
| - } |
969 |
| - in.close(); |
970 |
| - os.close(); |
971 |
| - |
| 902 | + static Object readChunk(String path, String encoding, int offset, int length) throws Exception { |
| 903 | + path = normalizePath(path); |
| 904 | + if(path == null) |
| 905 | + return null; |
| 906 | + byte [] buffer = new byte[length]; |
| 907 | + InputStream in = new FileInputStream(path); |
| 908 | + int read = in.read(buffer, offset, length); |
972 | 909 |
|
| 910 | + if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_BASE64)) { |
| 911 | + return DataConverter.byteToBase64(buffer, read); |
973 | 912 | }
|
974 |
| - catch(Exception ex) { |
975 |
| - promise.reject("RNFB read error", ex.getMessage()); |
976 |
| - } |
977 |
| - } |
978 |
| - |
979 |
| - /** |
980 |
| - * Write file to specific offset |
981 |
| - * @param path Path of the file to write. |
982 |
| - * @param encoding Encoding of the input data. |
983 |
| - * @param data Data to write to `path`. |
984 |
| - * @param offset Offset |
985 |
| - * @param promise JS context promise |
986 |
| - */ |
987 |
| - public void write(String path, String encoding, String data, int offset, int length, Promise promise) { |
988 |
| - |
989 |
| - try { |
990 |
| - File f = new File(path); |
991 |
| - |
992 |
| - if(!f.exists()) { |
993 |
| - promise.reject("RNFB write failed", " path : " + path + "does not exists"); |
994 |
| - return; |
995 |
| - } |
996 |
| - byte[] bytes; |
997 |
| - OutputStream os = new FileOutputStream(f); |
998 |
| - switch (encoding) { |
999 |
| - case RNFetchBlobConst.DATA_ENCODE_UTF8 : |
1000 |
| - bytes = data.getBytes(); |
1001 |
| - os.write(bytes, offset, length); |
1002 |
| - break; |
1003 |
| - case RNFetchBlobConst.DATA_ENCODE_BASE64 : |
1004 |
| - bytes = Base64.decode(data.getBytes(),0); |
1005 |
| - os.write(bytes, offset, length); |
1006 |
| - break; |
1007 |
| - case RNFetchBlobConst.DATA_ENCODE_URI : |
1008 |
| - FileInputStream in = new FileInputStream(new File(data)); |
1009 |
| - |
1010 |
| - int bufferSize = 102400; |
1011 |
| - int read = 0; |
1012 |
| - byte[] buffer = new byte[bufferSize]; |
1013 |
| - int done = 0; |
1014 |
| - while((length = length - (read = in.read(buffer))) > 0) { |
1015 |
| - os.write(buffer, done, read); |
1016 |
| - done += read; |
1017 |
| - } |
1018 |
| - if(bufferSize + length > 0 ){ |
1019 |
| - os.write(buffer, done, bufferSize + length ); |
1020 |
| - } |
1021 |
| - os.close(); |
1022 |
| - } |
1023 |
| - promise.resolve(length); |
| 913 | + else if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_UTF8)) { |
| 914 | + return DataConverter.byteToUTF8(buffer, read); |
1024 | 915 | }
|
1025 |
| - catch (Exception ex) { |
1026 |
| - promise.reject("RNFB write error", ex.getMessage()); |
| 916 | + else if(encoding.equalsIgnoreCase(RNFetchBlobConst.RNFB_RESPONSE_ASCII)) { |
| 917 | + return DataConverter.byteToRCTArray(buffer, read); |
1027 | 918 | }
|
| 919 | + return null; |
1028 | 920 |
|
1029 | 921 | }
|
1030 | 922 | }
|
0 commit comments