chg: manage more resources explicitly
authorFelix Dörre <felix@dogcraft.de>
Tue, 7 Nov 2017 20:06:55 +0000 (21:06 +0100)
committerFelix Dörre <felix@dogcraft.de>
Tue, 7 Nov 2017 21:08:53 +0000 (22:08 +0100)
When a constructor that gets a resource fails, it might prevent the
resource from being closed. By giving the resource explicitly to the
try-with-resources, this is prevented.

Change-Id: I2f8a50773de71f699c00729b51048892a44033e6

src/club/wpia/gigi/database/DatabaseConnection.java
src/club/wpia/gigi/ocsp/OCSPIssuerManager.java
src/club/wpia/gigi/output/template/Template.java
src/club/wpia/gigi/util/PublicSuffixes.java

index 20125a8..51e5872 100644 (file)
@@ -53,7 +53,7 @@ public class DatabaseConnection {
             System.out.println("Upgrade 32: loading " + f);
             if (f.exists()) {
                 StringBuilder sb = new StringBuilder();
-                try (Reader r = new InputStreamReader(new FileInputStream(f), "UTF-8")) {
+                try (FileInputStream fis = new FileInputStream(f); Reader r = new InputStreamReader(fis, "UTF-8")) {
                     int len;
                     char[] buf = new char[4096];
                     while ((len = r.read(buf)) > 0) {
index c83c331..40e6d67 100644 (file)
@@ -194,7 +194,7 @@ public class OCSPIssuerManager implements Runnable {
         ocspCsr.delete();
         ocspCrt.delete();
         String csr = PEM.encode("CERTIFICATE REQUEST", p10.getEncoded());
-        try (Writer w = new OutputStreamWriter(new FileOutputStream(ocspCsr), "UTF-8")) {
+        try (FileOutputStream fos = new FileOutputStream(ocspCsr); Writer w = new OutputStreamWriter(fos, "UTF-8")) {
             w.write(csr);
         }
     }
index d3f5a01..ad53ac2 100644 (file)
@@ -237,7 +237,7 @@ public class Template implements Outputable {
     protected void tryReload() {
         if (source != null && lastLoaded < source.lastModified()) {
             System.out.println("Reloading template.... " + source);
-            try (InputStreamReader r = new InputStreamReader(new FileInputStream(source), "UTF-8")) {
+            try (FileInputStream fis = new FileInputStream(source); InputStreamReader r = new InputStreamReader(fis, "UTF-8")) {
                 data = parse(r).getBlock(null);
                 r.close();
                 lastLoaded = source.lastModified() + 1000;
index 401502f..a86cbbb 100644 (file)
@@ -26,7 +26,7 @@ public class PublicSuffixes {
             throw new Error("Public Suffix List could not be loaded.");
         }
 
-        try (BufferedReader br = new BufferedReader(new InputStreamReader(res, "UTF-8"))) {
+        try (InputStreamReader isr = new InputStreamReader(res, "UTF-8"); BufferedReader br = new BufferedReader(isr)) {
             return new PublicSuffixes(br);
         }
     }