Cleanups: Close resources via try-finally

We can't use try-with-resources until we bump the minimum-supported Android
version for the client library to API 19.
This commit is contained in:
str4d
2017-12-09 01:02:17 +00:00
parent fe5e4a2c7a
commit a67ea4b2f2
14 changed files with 81 additions and 46 deletions

View File

@@ -494,11 +494,12 @@ public class DataHelper {
* or a value contains '#' or '\n'
*/
public static void storeProps(Properties props, File file) throws IOException {
FileOutputStream fos = null;
PrintWriter out = null;
IllegalArgumentException iae = null;
File tmpFile = new File(file.getPath() + ".tmp");
try {
FileOutputStream fos = new SecureFileOutputStream(tmpFile);
fos = new SecureFileOutputStream(tmpFile);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")));
out.println("# NOTE: This I2P config file must use UTF-8 encoding");
for (Map.Entry<Object, Object> entry : props.entrySet()) {
@@ -533,6 +534,7 @@ public class DataHelper {
throw new IOException("Failed rename from " + tmpFile + " to " + file);
} finally {
if (out != null) out.close();
if (fos != null) try { fos.close(); } catch (IOException ioe) {}
}
if (iae != null)
throw iae;