Browse Source

Merge pull request #432 from mcuelenaere/fix/improve-io-error-handling

RNFetchBlobFS.writeFile(): improve IO error handling
Travis Nuttall 5 years ago
parent
commit
3361baa44b
No account linked to committer's email address
1 changed files with 31 additions and 15 deletions
  1. 31
    15
      android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 31
- 15
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java View File

69
                 }
69
                 }
70
             }
70
             }
71
 
71
 
72
-            FileOutputStream fout = new FileOutputStream(f, append);
73
             // write data from a file
72
             // write data from a file
74
             if(encoding.equalsIgnoreCase(RNFetchBlobConst.DATA_ENCODE_URI)) {
73
             if(encoding.equalsIgnoreCase(RNFetchBlobConst.DATA_ENCODE_URI)) {
75
                 String normalizedData = normalizePath(data);
74
                 String normalizedData = normalizePath(data);
76
                 File src = new File(normalizedData);
75
                 File src = new File(normalizedData);
77
                 if (!src.exists()) {
76
                 if (!src.exists()) {
78
                     promise.reject("ENOENT", "No such file '" + path + "' " + "('" + normalizedData + "')");
77
                     promise.reject("ENOENT", "No such file '" + path + "' " + "('" + normalizedData + "')");
79
-                    fout.close();
80
                     return;
78
                     return;
81
                 }
79
                 }
82
-                FileInputStream fin = new FileInputStream(src);
83
                 byte[] buffer = new byte [10240];
80
                 byte[] buffer = new byte [10240];
84
                 int read;
81
                 int read;
85
                 written = 0;
82
                 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
+                    }
89
                 }
99
                 }
90
-                fin.close();
91
             }
100
             }
92
             else {
101
             else {
93
                 byte[] bytes = stringToBytes(data, encoding);
102
                 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
+                }
96
             }
110
             }
97
-            fout.close();
98
             promise.resolve(written);
111
             promise.resolve(written);
99
         } catch (FileNotFoundException e) {
112
         } catch (FileNotFoundException e) {
100
             // According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
113
             // According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
129
             }
142
             }
130
 
143
 
131
             FileOutputStream os = new FileOutputStream(f, append);
144
             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();
135
             }
153
             }
136
-            os.write(bytes);
137
-            os.close();
138
             promise.resolve(data.size());
154
             promise.resolve(data.size());
139
         } catch (FileNotFoundException e) {
155
         } catch (FileNotFoundException e) {
140
             // According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
156
             // According to https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html