From a67ea4b2f2ac73a55f0684217904ba3c87d93d6e Mon Sep 17 00:00:00 2001 From: str4d <str4d@mail.i2p> Date: Sat, 9 Dec 2017 01:02:17 +0000 Subject: [PATCH] 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. --- .../net/metanotion/io/block/BlockFile.java | 11 +++++---- .../java/src/net/i2p/i2ptunnel/I2PTunnel.java | 5 +++- .../java/src/net/i2p/i2ptunnel/I2Ping.java | 7 +++++- .../java/src/net/i2p/jetty/JettyStart.java | 23 +++++++++++++++---- .../i2p/router/web/helpers/LogsHelper.java | 4 +--- .../src/net/i2p/sam/client/SAMStreamSink.java | 5 +++- .../src/net/i2p/apps/systray/UrlLauncher.java | 14 ++++++----- .../freenet/support/CPUInformation/CPUID.java | 4 +++- .../naming/SingleFileNamingService.java | 12 ++++++---- .../src/net/i2p/crypto/TrustedUpdate.java | 9 ++------ core/java/src/net/i2p/data/DataHelper.java | 4 +++- .../src/net/i2p/util/NativeBigInteger.java | 4 +++- .../src/net/i2p/util/TranslateReader.java | 20 +++++++++------- .../cybergarage/upnp/ssdp/HTTPMUSocket.java | 5 ++-- 14 files changed, 81 insertions(+), 46 deletions(-) diff --git a/apps/addressbook/java/src/net/metanotion/io/block/BlockFile.java b/apps/addressbook/java/src/net/metanotion/io/block/BlockFile.java index 4db5e1085f..2cfec6f62b 100644 --- a/apps/addressbook/java/src/net/metanotion/io/block/BlockFile.java +++ b/apps/addressbook/java/src/net/metanotion/io/block/BlockFile.java @@ -144,14 +144,17 @@ public class BlockFile implements Closeable { return; } boolean init = !(new File(args[0])).exists(); + RAIFile raif = null; + BlockFile bf = null; try { - RAIFile raif = new RAIFile(new File(args[0]), true, true); - BlockFile bf = new BlockFile(raif, init); + raif = new RAIFile(new File(args[0]), true, true); + bf = new BlockFile(raif, init); bf.bfck(true); - bf.close(); - raif.close(); } catch (IOException e) { e.printStackTrace(); + } finally { + if (bf != null) try { bf.close(); } catch (IOException ioe) {} + if (raif != null) try { raif.close(); } catch (IOException ioe) {} } } diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java index 03027b4861..95c5285d51 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java @@ -1607,8 +1607,9 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging { */ private void runRun(String args[], Logging l) { if (args.length == 1) { + BufferedReader br = null; try { - BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8")); + br = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]), "UTF-8")); String line; while ((line = br.readLine()) != null) { runCommand(line, l); @@ -1619,6 +1620,8 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging { l.log("IO error running the file"); _log.error(getPrefix() + "Error running the file", ioe); notifyEvent("runResult", "error"); + } finally { + if (br != null) try { br.close(); } catch (IOException ioe) {} } } else { l.log("run <commandfile>\n" + diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2Ping.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2Ping.java index ff7698fe85..9f858b3c5d 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2Ping.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2Ping.java @@ -159,7 +159,9 @@ public class I2Ping extends I2PTunnelClientBase { } if (hostListFile != null) { - BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(hostListFile), "UTF-8")); + BufferedReader br = null; + try { + br = new BufferedReader(new InputStreamReader(new FileInputStream(hostListFile), "UTF-8")); String line; List<PingHandler> pingHandlers = new ArrayList<PingHandler>(); int i = 0; @@ -181,6 +183,9 @@ public class I2Ping extends I2PTunnelClientBase { for (Thread t : pingHandlers) t.join(); return; + } finally { + if (br != null) try { br.close(); } catch (IOException ioe) {} + } } String host = argv[g.getOptind()]; diff --git a/apps/jetty/java/src/net/i2p/jetty/JettyStart.java b/apps/jetty/java/src/net/i2p/jetty/JettyStart.java index f068a20c08..cbf54fb5df 100644 --- a/apps/jetty/java/src/net/i2p/jetty/JettyStart.java +++ b/apps/jetty/java/src/net/i2p/jetty/JettyStart.java @@ -1,5 +1,7 @@ package net.i2p.jetty; +import java.io.IOException; + // Contains code from org.mortbay.xml.XmlConfiguation: // ======================================================================== @@ -17,6 +19,7 @@ package net.i2p.jetty; // ======================================================================== import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -79,14 +82,24 @@ public class JettyStart implements ClientApp { public void parseArgs(String[] args) throws Exception { Properties properties=new Properties(); XmlConfiguration last=null; - InputStream in = null; + Resource r = null; for (int i = 0; i < args.length; i++) { if (args[i].toLowerCase().endsWith(".properties")) { - in = Resource.newResource(args[i]).getInputStream(); - properties.load(in); - in.close(); + try { + r = Resource.newResource(args[i]); + properties.load(r.getInputStream()); + } finally { + if (r != null) r.close(); + } } else { - XmlConfiguration configuration = new XmlConfiguration(Resource.newResource(args[i]).getURL()); + URL configUrl; + try { + r = Resource.newResource(args[i]); + configUrl = r.getURL(); + } finally { + if (r != null) r.close(); + } + XmlConfiguration configuration = new XmlConfiguration(configUrl); if (last!=null) configuration.getIdMap().putAll(last.getIdMap()); if (properties.size()>0) { diff --git a/apps/routerconsole/java/src/net/i2p/router/web/helpers/LogsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/helpers/LogsHelper.java index 48a0f51834..f14324420c 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/helpers/LogsHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/helpers/LogsHelper.java @@ -173,11 +173,9 @@ public class LogsHelper extends HelperBase { */ private static String readTextFile(File f, int maxNumLines) { if (!f.exists()) return null; - FileInputStream fis = null; BufferedReader in = null; try { - fis = new FileInputStream(f); - in = new BufferedReader(new InputStreamReader(fis)); + in = new BufferedReader(new InputStreamReader(new FileInputStream(f))); List<String> lines = new ArrayList<String>(maxNumLines); String line = null; while ( (line = in.readLine()) != null) { diff --git a/apps/sam/java/src/net/i2p/sam/client/SAMStreamSink.java b/apps/sam/java/src/net/i2p/sam/client/SAMStreamSink.java index 0b67b7abdf..4e1fe06396 100644 --- a/apps/sam/java/src/net/i2p/sam/client/SAMStreamSink.java +++ b/apps/sam/java/src/net/i2p/sam/client/SAMStreamSink.java @@ -243,10 +243,11 @@ public class SAMStreamSink { } public void run() { + DatagramSocket dg = null; byte[] buf = new byte[32768]; try { Sink sink = new Sink("FAKE", "FAKEFROM"); - DatagramSocket dg = new DatagramSocket(V3DGPORT); + dg = new DatagramSocket(V3DGPORT); while (true) { DatagramPacket p = new DatagramPacket(buf, 32768); dg.receive(p); @@ -283,6 +284,8 @@ public class SAMStreamSink { } } catch (IOException ioe) { _log.error("DGRcvr", ioe); + } finally { + if (dg != null) dg.close(); } } } diff --git a/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java b/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java index aa8f5ef125..dabd1ed8e0 100644 --- a/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java +++ b/apps/systray/java/src/net/i2p/apps/systray/UrlLauncher.java @@ -136,13 +136,15 @@ public class UrlLauncher implements ClientApp { long done = System.currentTimeMillis() + MAX_WAIT_TIME; for (int i = 0; i < MAX_TRIES; i++) { try { - Socket test = new Socket(); - // this will usually fail right away if it's going to fail since it's local - test.connect(sa, WAIT_TIME); - // it worked + Socket test = null; try { - test.close(); - } catch (IOException ioe) {} + test = new Socket(); + // this will usually fail right away if it's going to fail since it's local + test.connect(sa, WAIT_TIME); + // it worked + } finally { + if (test != null) try { test.close(); } catch (IOException ioe) {} + } // Jetty 6 seems to start the Connector before the // webapp is completely ready try { diff --git a/core/java/src/freenet/support/CPUInformation/CPUID.java b/core/java/src/freenet/support/CPUInformation/CPUID.java index 161cdb64dc..ca8c092cd8 100644 --- a/core/java/src/freenet/support/CPUInformation/CPUID.java +++ b/core/java/src/freenet/support/CPUInformation/CPUID.java @@ -551,11 +551,12 @@ public class CPUID { URL resource = CPUID.class.getClassLoader().getResource(resourceName); if (resource == null) return false; + InputStream libStream = null; File outFile = null; FileOutputStream fos = null; String filename = getLibraryPrefix() + "jcpuid" + getLibrarySuffix(); try { - InputStream libStream = resource.openStream(); + libStream = resource.openStream(); outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename); fos = new FileOutputStream(outFile); DataHelper.copy(libStream, fos); @@ -580,6 +581,7 @@ public class CPUID { outFile.delete(); return false; } finally { + if (libStream != null) try { libStream.close(); } catch (IOException ioe) {} if (fos != null) { try { fos.close(); } catch (IOException ioe) {} } diff --git a/core/java/src/net/i2p/client/naming/SingleFileNamingService.java b/core/java/src/net/i2p/client/naming/SingleFileNamingService.java index 835c3dbdd5..acde6e6ed2 100644 --- a/core/java/src/net/i2p/client/naming/SingleFileNamingService.java +++ b/core/java/src/net/i2p/client/naming/SingleFileNamingService.java @@ -210,11 +210,13 @@ public class SingleFileNamingService extends NamingService { } return success; } catch (IOException ioe) { - if (in != null) try { in.close(); } catch (IOException e) {} - if (out != null) try { out.close(); } catch (IOException e) {} _log.error("Error adding " + hostname, ioe); return false; - } finally { releaseWriteLock(); } + } finally { + if (in != null) try { in.close(); } catch (IOException e) {} + if (out != null) try { out.close(); } catch (IOException e) {} + releaseWriteLock(); + } } /** @@ -333,11 +335,11 @@ public class SingleFileNamingService extends NamingService { } return success; } catch (IOException ioe) { - if (in != null) try { in.close(); } catch (IOException e) {} - if (out != null) try { out.close(); } catch (IOException e) {} _log.error("Error removing " + hostname, ioe); return false; } finally { + if (in != null) try { in.close(); } catch (IOException e) {} + if (out != null) try { out.close(); } catch (IOException e) {} releaseWriteLock(); } } diff --git a/core/java/src/net/i2p/crypto/TrustedUpdate.java b/core/java/src/net/i2p/crypto/TrustedUpdate.java index 23520942be..653d427e84 100644 --- a/core/java/src/net/i2p/crypto/TrustedUpdate.java +++ b/core/java/src/net/i2p/crypto/TrustedUpdate.java @@ -764,13 +764,8 @@ riCe6OlAEiNpcc6mMyIYYWFICbrDFTrDR3wXqwc/Jkcx6L5VVWoagpSzbo3yGhc= return null; } finally { - if (bytesToSignInputStream != null) - try { - bytesToSignInputStream.close(); - fileInputStream.close(); - } catch (IOException ioe) { - } - + if (bytesToSignInputStream != null) try { bytesToSignInputStream.close(); } catch (IOException ioe) {} + if (fileInputStream != null) try { fileInputStream.close(); } catch (IOException ioe) {} } FileOutputStream fileOutputStream = null; diff --git a/core/java/src/net/i2p/data/DataHelper.java b/core/java/src/net/i2p/data/DataHelper.java index 0e7dfab112..2f08a0566b 100644 --- a/core/java/src/net/i2p/data/DataHelper.java +++ b/core/java/src/net/i2p/data/DataHelper.java @@ -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; diff --git a/core/java/src/net/i2p/util/NativeBigInteger.java b/core/java/src/net/i2p/util/NativeBigInteger.java index bbd40912ec..9e043729de 100644 --- a/core/java/src/net/i2p/util/NativeBigInteger.java +++ b/core/java/src/net/i2p/util/NativeBigInteger.java @@ -1119,11 +1119,12 @@ public class NativeBigInteger extends BigInteger { return false; } + InputStream libStream = null; File outFile = null; FileOutputStream fos = null; String filename = _libPrefix + "jbigi" + _libSuffix; try { - InputStream libStream = resource.openStream(); + libStream = resource.openStream(); outFile = new File(I2PAppContext.getGlobalContext().getTempDir(), filename); fos = new FileOutputStream(outFile); DataHelper.copy(libStream, fos); @@ -1143,6 +1144,7 @@ public class NativeBigInteger extends BigInteger { outFile.delete(); return false; } finally { + if (libStream != null) try { libStream.close(); } catch (IOException ioe) {} if (fos != null) { try { fos.close(); } catch (IOException ioe) {} } diff --git a/core/java/src/net/i2p/util/TranslateReader.java b/core/java/src/net/i2p/util/TranslateReader.java index 171edd4a87..02eb0a0d68 100644 --- a/core/java/src/net/i2p/util/TranslateReader.java +++ b/core/java/src/net/i2p/util/TranslateReader.java @@ -395,15 +395,19 @@ public class TranslateReader extends FilterReader { private static void test(String file) throws IOException { FileInputStream fio = new FileInputStream(file); - TranslateReader r = new TranslateReader(I2PAppContext.getGlobalContext(), - "net.i2p.router.web.messages", - fio); - int c; - while ((c = r.read()) >= 0) { - System.out.print((char)c); + TranslateReader r = null; + try { + r = new TranslateReader(I2PAppContext.getGlobalContext(), + "net.i2p.router.web.messages", + fio); + int c; + while ((c = r.read()) >= 0) { + System.out.print((char)c); + } + System.out.flush(); + } finally { + if (r != null) try { r.close(); } catch (IOException ioe) {} } - System.out.flush(); - r.close(); } /** @param files ignore 0 */ diff --git a/router/java/src/org/cybergarage/upnp/ssdp/HTTPMUSocket.java b/router/java/src/org/cybergarage/upnp/ssdp/HTTPMUSocket.java index 657a1e4c94..34cb29aa7a 100644 --- a/router/java/src/org/cybergarage/upnp/ssdp/HTTPMUSocket.java +++ b/router/java/src/org/cybergarage/upnp/ssdp/HTTPMUSocket.java @@ -194,8 +194,8 @@ public class HTTPMUSocket public boolean send(String msg, String bindAddr, int bindPort) { + MulticastSocket msock = null; try { - MulticastSocket msock; if ((bindAddr) != null && (0 < bindPort)) { msock = new MulticastSocket(null); msock.bind(new InetSocketAddress(bindAddr, bindPort)); @@ -206,11 +206,12 @@ public class HTTPMUSocket // Thnaks for Theo Beisch (11/09/04) msock.setTimeToLive(UPnP.getTimeToLive()); msock.send(dgmPacket); - msock.close(); } catch (Exception e) { Debug.warning(e); return false; + } finally { + if (msock != null) msock.close(); } return true; } -- GitLab