@@ -79,22 +79,14 @@ static public void writeFile(String path, String encoding, String data, final bo
79
79
FileOutputStream fout = new FileOutputStream (f , append );
80
80
// write data from a file
81
81
if (encoding .equalsIgnoreCase (RNFetchBlobConst .DATA_ENCODE_URI )) {
82
- data = normalizePath (data );
83
- File src = new File (data );
84
- if (!src .exists ()) {
85
- promise .reject ("RNfetchBlob writeFileError" , "source file : " + data + "not exists" );
86
- fout .close ();
87
- return ;
82
+ try {
83
+ written = writeFileToFileWithOffset (data , path , 0 , append );
84
+ promise .resolve (written );
88
85
}
89
- FileInputStream fin = new FileInputStream (src );
90
- byte [] buffer = new byte [10240 ];
91
- int read ;
92
- written = 0 ;
93
- while ((read = fin .read (buffer )) > 0 ) {
94
- fout .write (buffer , 0 , read );
95
- written += read ;
86
+ catch (Exception ex ) {
87
+ ex .printStackTrace ();
88
+ promise .reject ("RNfetchBlob writeFileError" , ex .getMessage ());
96
89
}
97
- fin .close ();
98
90
}
99
91
else {
100
92
byte [] bytes = stringToBytes (data , encoding );
@@ -108,6 +100,22 @@ static public void writeFile(String path, String encoding, String data, final bo
108
100
}
109
101
}
110
102
103
+ private static int writeFileToFileWithOffset (String source , String dest , int offset , boolean append ) throws IOException {
104
+
105
+ source = normalizePath (source );
106
+ FileOutputStream fout = new FileOutputStream (dest , append );
107
+ FileInputStream fin = new FileInputStream (source );
108
+ byte [] buffer = new byte [10240 ];
109
+ int read ;
110
+ int written = 0 ;
111
+ while ((read = fin .read (buffer )) > 0 ) {
112
+ fout .write (buffer , offset + written , read );
113
+ written += read ;
114
+ }
115
+ fin .close ();
116
+ return written ;
117
+ }
118
+
111
119
/**
112
120
* Write array of bytes into file
113
121
* @param path Destination file path.
@@ -123,11 +131,7 @@ static public void writeFile(String path, ReadableArray data, final boolean appe
123
131
if (!dir .exists ())
124
132
dir .mkdirs ();
125
133
FileOutputStream os = new FileOutputStream (f , append );
126
- byte [] bytes = new byte [data .size ()];
127
- for (int i =0 ;i <data .size ();i ++) {
128
- bytes [i ] = (byte ) data .getInt (i );
129
- }
130
- os .write (bytes );
134
+ os .write (DataConverter .RCTArrayToBytes (data ));
131
135
os .close ();
132
136
promise .resolve (data .size ());
133
137
} catch (Exception e ) {
@@ -337,7 +341,7 @@ public void writeStream(String path, String encoding, boolean append, Callback c
337
341
* @param data Data chunk in string format
338
342
* @param callback JS context callback
339
343
*/
340
- static void writeChunk (String streamId , String data , Callback callback ) {
344
+ static void writeStreamChunk (String streamId , String data , Callback callback ) {
341
345
342
346
RNFetchBlobFS fs = fileStreams .get (streamId );
343
347
OutputStream stream = fs .writeStreamInstance ;
@@ -900,24 +904,65 @@ static String normalizePath(String path) {
900
904
return PathResolver .getRealPathFromURI (RNFetchBlob .RCTContext , uri );
901
905
}
902
906
907
+ /**
908
+ * Read {length} bytes from {path} from {offset} bytes.
909
+ * @param path Source file URI
910
+ * @param encoding The encoding of output data
911
+ * @param offset Offset of the file.
912
+ * @param length Length of data to read.
913
+ * @return Result of the operation.
914
+ * @throws Exception
915
+ */
903
916
static Object readChunk (String path , String encoding , int offset , int length ) throws Exception {
904
917
path = normalizePath (path );
905
918
if (path == null )
906
919
return null ;
907
920
byte [] buffer = new byte [length ];
908
- InputStream in = new FileInputStream (path );
921
+ FileInputStream in = new FileInputStream (path );
909
922
int read = in .read (buffer , offset , length );
910
-
923
+ Object result = null ;
911
924
if (encoding .equalsIgnoreCase (RNFetchBlobConst .RNFB_RESPONSE_BASE64 )) {
912
- return DataConverter .byteToBase64 (buffer , read );
925
+ result = DataConverter .byteToBase64 (buffer , read );
913
926
}
914
927
else if (encoding .equalsIgnoreCase (RNFetchBlobConst .RNFB_RESPONSE_UTF8 )) {
915
- return DataConverter .byteToUTF8 (buffer , read );
928
+ result = DataConverter .byteToUTF8 (buffer , read );
916
929
}
917
930
else if (encoding .equalsIgnoreCase (RNFetchBlobConst .RNFB_RESPONSE_ASCII )) {
918
- return DataConverter .byteToRCTArray (buffer , read );
931
+ result = DataConverter .byteToRCTArray (buffer , read );
919
932
}
920
- return null ;
933
+ in .close ();
934
+ return result ;
935
+
936
+ }
921
937
938
+ /**
939
+ * Write specified data to destination start from {offset} bytes.
940
+ * @param path Destination file path.
941
+ * @param encoding Encoding of input data.
942
+ * @param data Data to be written to file.
943
+ * @param offset Offset of the operation.
944
+ */
945
+ static void writeChunk (String path , String encoding , Object data , int offset ) throws Exception {
946
+ if (path == null )
947
+ return ;
948
+ FileOutputStream out = new FileOutputStream (path );
949
+ byte [] bytes = null ;
950
+ switch (encoding ) {
951
+ case RNFetchBlobConst .DATA_ENCODE_BASE64 :
952
+ bytes = Base64 .decode ((String )data , 0 );
953
+ break ;
954
+ case RNFetchBlobConst .DATA_ENCODE_UTF8 :
955
+ bytes = ((String )data ).getBytes ();
956
+ break ;
957
+ case RNFetchBlobConst .DATA_ENCODE_ASCII :
958
+ bytes = DataConverter .RCTArrayToBytes ((ReadableArray ) data );
959
+ break ;
960
+ case RNFetchBlobConst .DATA_ENCODE_URI :
961
+ writeFileToFileWithOffset ((String )data , path , offset , false );
962
+ return ;
963
+ }
964
+ if (bytes != null )
965
+ out .write (bytes , offset , bytes .length );
922
966
}
967
+
923
968
}
0 commit comments