Bläddra i källkod

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

RNFetchBlobFS.writeFile(): improve IO error handling
Travis Nuttall 5 år sedan
förälder
incheckning
3361baa44b
No account linked to committer's email address
1 ändrade filer med 31 tillägg och 15 borttagningar
  1. 31
    15
      android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java

+ 31
- 15
android/src/main/java/com/RNFetchBlob/RNFetchBlobFS.java Visa fil

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