@@ -69,32 +69,45 @@ static void writeFile(String path, String encoding, String data, final boolean a
6969 }
7070 }
7171
72- FileOutputStream fout = new FileOutputStream (f , append );
7372 // write data from a file
7473 if (encoding .equalsIgnoreCase (RNFetchBlobConst .DATA_ENCODE_URI )) {
7574 String normalizedData = normalizePath (data );
7675 File src = new File (normalizedData );
7776 if (!src .exists ()) {
7877 promise .reject ("ENOENT" , "No such file '" + path + "' " + "('" + normalizedData + "')" );
79- fout .close ();
8078 return ;
8179 }
82- FileInputStream fin = new FileInputStream (src );
8380 byte [] buffer = new byte [10240 ];
8481 int read ;
8582 written = 0 ;
86- while ((read = fin .read (buffer )) > 0 ) {
87- fout .write (buffer , 0 , read );
88- written += read ;
83+ FileInputStream fin = null ;
84+ FileOutputStream fout = null ;
85+ try {
86+ fin = new FileInputStream (src );
87+ fout = new FileOutputStream (f , append );
88+ while ((read = fin .read (buffer )) > 0 ) {
89+ fout .write (buffer , 0 , read );
90+ written += read ;
91+ }
92+ } finally {
93+ if (fin != null ) {
94+ fin .close ();
95+ }
96+ if (fout != null ) {
97+ fout .close ();
98+ }
8999 }
90- fin .close ();
91100 }
92101 else {
93102 byte [] bytes = stringToBytes (data , encoding );
94- fout .write (bytes );
95- written = bytes .length ;
103+ FileOutputStream fout = new FileOutputStream (f , append );
104+ try {
105+ fout .write (bytes );
106+ written = bytes .length ;
107+ } finally {
108+ fout .close ();
109+ }
96110 }
97- fout .close ();
98111 promise .resolve (written );
99112 } catch (FileNotFoundException e ) {
100113 // According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
@@ -129,12 +142,15 @@ static void writeFile(String path, ReadableArray data, final boolean append, fin
129142 }
130143
131144 FileOutputStream os = new FileOutputStream (f , append );
132- byte [] bytes = new byte [data .size ()];
133- for (int i =0 ;i <data .size ();i ++) {
134- bytes [i ] = (byte ) data .getInt (i );
145+ try {
146+ byte [] bytes = new byte [data .size ()];
147+ for (int i = 0 ; i < data .size (); i ++) {
148+ bytes [i ] = (byte ) data .getInt (i );
149+ }
150+ os .write (bytes );
151+ } finally {
152+ os .close ();
135153 }
136- os .write (bytes );
137- os .close ();
138154 promise .resolve (data .size ());
139155 } catch (FileNotFoundException e ) {
140156 // According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
0 commit comments