@@ -79,22 +79,14 @@ static public void writeFile(String path, String encoding, String data, final bo
7979 FileOutputStream fout = new FileOutputStream (f , append );
8080 // write data from a file
8181 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 );
8885 }
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 ());
9689 }
97- fin .close ();
9890 }
9991 else {
10092 byte [] bytes = stringToBytes (data , encoding );
@@ -108,6 +100,22 @@ static public void writeFile(String path, String encoding, String data, final bo
108100 }
109101 }
110102
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+
111119 /**
112120 * Write array of bytes into file
113121 * @param path Destination file path.
@@ -123,11 +131,7 @@ static public void writeFile(String path, ReadableArray data, final boolean appe
123131 if (!dir .exists ())
124132 dir .mkdirs ();
125133 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 ));
131135 os .close ();
132136 promise .resolve (data .size ());
133137 } catch (Exception e ) {
@@ -337,7 +341,7 @@ public void writeStream(String path, String encoding, boolean append, Callback c
337341 * @param data Data chunk in string format
338342 * @param callback JS context callback
339343 */
340- static void writeChunk (String streamId , String data , Callback callback ) {
344+ static void writeStreamChunk (String streamId , String data , Callback callback ) {
341345
342346 RNFetchBlobFS fs = fileStreams .get (streamId );
343347 OutputStream stream = fs .writeStreamInstance ;
@@ -900,24 +904,65 @@ static String normalizePath(String path) {
900904 return PathResolver .getRealPathFromURI (RNFetchBlob .RCTContext , uri );
901905 }
902906
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+ */
903916 static Object readChunk (String path , String encoding , int offset , int length ) throws Exception {
904917 path = normalizePath (path );
905918 if (path == null )
906919 return null ;
907920 byte [] buffer = new byte [length ];
908- InputStream in = new FileInputStream (path );
921+ FileInputStream in = new FileInputStream (path );
909922 int read = in .read (buffer , offset , length );
910-
923+ Object result = null ;
911924 if (encoding .equalsIgnoreCase (RNFetchBlobConst .RNFB_RESPONSE_BASE64 )) {
912- return DataConverter .byteToBase64 (buffer , read );
925+ result = DataConverter .byteToBase64 (buffer , read );
913926 }
914927 else if (encoding .equalsIgnoreCase (RNFetchBlobConst .RNFB_RESPONSE_UTF8 )) {
915- return DataConverter .byteToUTF8 (buffer , read );
928+ result = DataConverter .byteToUTF8 (buffer , read );
916929 }
917930 else if (encoding .equalsIgnoreCase (RNFetchBlobConst .RNFB_RESPONSE_ASCII )) {
918- return DataConverter .byteToRCTArray (buffer , read );
931+ result = DataConverter .byteToRCTArray (buffer , read );
919932 }
920- return null ;
933+ in .close ();
934+ return result ;
935+
936+ }
921937
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 );
922966 }
967+
923968}
0 commit comments