From e77409e57ae333f4e5e8e3d09d55ea6d63f3e90b Mon Sep 17 00:00:00 2001 From: str4d <str4d@mail.i2p> Date: Mon, 25 Nov 2013 23:14:09 +0000 Subject: [PATCH] Close resources --- .../src/org/klomp/snark/dht/PersistDHT.java | 8 +++--- apps/sam/java/src/net/i2p/sam/SAMBridge.java | 10 +++---- router/java/src/net/i2p/router/Blocklist.java | 26 +++++++++---------- .../java/src/net/i2p/router/MultiRouter.java | 3 +++ .../src/net/i2p/router/transport/GeoIP.java | 16 ++++++------ .../src/net/i2p/router/transport/GeoIPv6.java | 4 ++- .../src/net/i2p/router/util/EventLog.java | 9 +++---- 7 files changed, 40 insertions(+), 36 deletions(-) diff --git a/apps/i2psnark/java/src/org/klomp/snark/dht/PersistDHT.java b/apps/i2psnark/java/src/org/klomp/snark/dht/PersistDHT.java index c690bc340a..8ecd111ef7 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/dht/PersistDHT.java +++ b/apps/i2psnark/java/src/org/klomp/snark/dht/PersistDHT.java @@ -37,10 +37,10 @@ abstract class PersistDHT { public static synchronized void loadDHT(KRPC krpc, File file) { Log log = I2PAppContext.getGlobalContext().logManager().getLog(PersistDHT.class); int count = 0; - FileInputStream in = null; + BufferedReader br = null; try { - in = new FileInputStream(file); - BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1")); + br = new BufferedReader(new InputStreamReader( + new FileInputStream(file), "ISO-8859-1")); String line = null; while ( (line = br.readLine()) != null) { if (line.startsWith("#")) @@ -61,7 +61,7 @@ abstract class PersistDHT { if (log.shouldLog(Log.WARN) && file.exists()) log.warn("Error reading the DHT File", ioe); } finally { - if (in != null) try { in.close(); } catch (IOException ioe) {} + if (br != null) try { br.close(); } catch (IOException ioe) {} } if (log.shouldLog(Log.INFO)) log.info("Loaded " + count + " nodes from " + file); diff --git a/apps/sam/java/src/net/i2p/sam/SAMBridge.java b/apps/sam/java/src/net/i2p/sam/SAMBridge.java index 27f2af5a85..102c27fa09 100644 --- a/apps/sam/java/src/net/i2p/sam/SAMBridge.java +++ b/apps/sam/java/src/net/i2p/sam/SAMBridge.java @@ -214,12 +214,12 @@ public class SAMBridge implements Runnable, ClientApp { private void loadKeys() { synchronized (nameToPrivKeys) { nameToPrivKeys.clear(); - FileInputStream in = null; + BufferedReader br = null; try { - in = new FileInputStream(persistFilename); - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + br = new BufferedReader(new InputStreamReader( + new FileInputStream(persistFilename))); String line = null; - while ( (line = reader.readLine()) != null) { + while ( (line = br.readLine()) != null) { int eq = line.indexOf('='); String name = line.substring(0, eq); String privKeys = line.substring(eq+1); @@ -230,7 +230,7 @@ public class SAMBridge implements Runnable, ClientApp { } catch (IOException ioe) { _log.error("Unable to read the keys from " + persistFilename, ioe); } finally { - if (in != null) try { in.close(); } catch (IOException ioe) {} + if (br != null) try { br.close(); } catch (IOException ioe) {} } } } diff --git a/router/java/src/net/i2p/router/Blocklist.java b/router/java/src/net/i2p/router/Blocklist.java index 4c3b8407b4..66617507bb 100644 --- a/router/java/src/net/i2p/router/Blocklist.java +++ b/router/java/src/net/i2p/router/Blocklist.java @@ -215,10 +215,10 @@ public class Blocklist { int badcount = 0; int peercount = 0; long ipcount = 0; - FileInputStream in = null; + BufferedReader br = null; try { - in = new FileInputStream(BLFile); - BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); + br = new BufferedReader(new InputStreamReader( + new FileInputStream(BLFile), "UTF-8")); String buf = null; while ((buf = br.readLine()) != null && count < maxSize) { Entry e = parse(buf, true); @@ -247,7 +247,7 @@ public class Blocklist { _log.log(Log.CRIT, "OOM reading the blocklist"); return; } finally { - if (in != null) try { in.close(); } catch (IOException ioe) {} + if (br != null) try { br.close(); } catch (IOException ioe) {} } if (_wrapSave != null) { @@ -405,10 +405,10 @@ public class Blocklist { private int getSize(File BLFile) { if ( (!BLFile.exists()) || (BLFile.length() <= 0) ) return 0; int lines = 0; - FileInputStream in = null; + BufferedReader br = null; try { - in = new FileInputStream(BLFile); - BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1")); + br = new BufferedReader(new InputStreamReader( + new FileInputStream(BLFile), "ISO-8859-1")); while (br.readLine() != null) { lines++; } @@ -417,7 +417,7 @@ public class Blocklist { _log.warn("Error reading the BLFile", ioe); return 0; } finally { - if (in != null) try { in.close(); } catch (IOException ioe) {} + if (br != null) try { br.close(); } catch (IOException ioe) {} } return lines; } @@ -770,10 +770,10 @@ public class Blocklist { for (Iterator<byte[]> iter = ips.iterator(); iter.hasNext(); ) { byte ip[] = iter.next(); int ipint = toInt(ip); - FileInputStream in = null; + BufferedReader br = null; try { - in = new FileInputStream(BLFile); - BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); + br = new BufferedReader(new InputStreamReader( + new FileInputStream(BLFile), "UTF-8")); String buf = null; // assume the file is unsorted, so go through the whole thing while ((buf = br.readLine()) != null) { @@ -782,7 +782,7 @@ public class Blocklist { continue; } if (match(ipint, toEntry(e.ip1, e.ip2))) { - try { in.close(); } catch (IOException ioe) {} + try { br.close(); } catch (IOException ioe) {} String reason = _x("IP banned by blocklist.txt entry {0}"); // only one translate parameter for now //for (int i = 0; i < 4; i++) { @@ -801,7 +801,7 @@ public class Blocklist { if (_log.shouldLog(Log.WARN)) _log.warn("Error reading the BLFile", ioe); } finally { - if (in != null) try { in.close(); } catch (IOException ioe) {} + if (br != null) try { br.close(); } catch (IOException ioe) {} } } // We already banlisted in banlist(peer), that's good enough diff --git a/router/java/src/net/i2p/router/MultiRouter.java b/router/java/src/net/i2p/router/MultiRouter.java index 8d5f381eac..6e43468073 100644 --- a/router/java/src/net/i2p/router/MultiRouter.java +++ b/router/java/src/net/i2p/router/MultiRouter.java @@ -67,13 +67,16 @@ public class MultiRouter { Scanner scan = new Scanner(args[0]); if (!scan.hasNextInt()) { usage(); + scan.close(); return; } nbrRouters = scan.nextInt(); if (nbrRouters < 0) { usage(); + scan.close(); return; } + scan.close(); _out = System.out; diff --git a/router/java/src/net/i2p/router/transport/GeoIP.java b/router/java/src/net/i2p/router/transport/GeoIP.java index cad3654c02..3f0990dd3b 100644 --- a/router/java/src/net/i2p/router/transport/GeoIP.java +++ b/router/java/src/net/i2p/router/transport/GeoIP.java @@ -194,10 +194,10 @@ class GeoIP { _log.warn("Country file not found: " + geoFile.getAbsolutePath()); return; } - FileInputStream in = null; + BufferedReader br = null; try { - in = new FileInputStream(geoFile); - BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); + br = new BufferedReader(new InputStreamReader( + new FileInputStream(geoFile), "UTF-8")); String line = null; while ( (line = br.readLine()) != null) { try { @@ -215,7 +215,7 @@ class GeoIP { if (_log.shouldLog(Log.ERROR)) _log.error("Error reading the Country File", ioe); } finally { - if (in != null) try { in.close(); } catch (IOException ioe) {} + if (br != null) try { br.close(); } catch (IOException ioe) {} } } @@ -256,11 +256,11 @@ class GeoIP { String[] rv = new String[search.length]; int idx = 0; long start = _context.clock().now(); - FileInputStream in = null; + BufferedReader br = null; try { - in = new FileInputStream(geoFile); String buf = null; - BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1")); + br = new BufferedReader(new InputStreamReader( + new FileInputStream(geoFile), "ISO-8859-1")); while ((buf = br.readLine()) != null && idx < search.length) { try { if (buf.charAt(0) == '#') { @@ -288,7 +288,7 @@ class GeoIP { if (_log.shouldLog(Log.ERROR)) _log.error("Error reading the geoFile", ioe); } finally { - if (in != null) try { in.close(); } catch (IOException ioe) {} + if (br != null) try { br.close(); } catch (IOException ioe) {} } if (_log.shouldLog(Log.INFO)) { diff --git a/router/java/src/net/i2p/router/transport/GeoIPv6.java b/router/java/src/net/i2p/router/transport/GeoIPv6.java index f26e33a7eb..b93e3f004f 100644 --- a/router/java/src/net/i2p/router/transport/GeoIPv6.java +++ b/router/java/src/net/i2p/router/transport/GeoIPv6.java @@ -158,12 +158,13 @@ class GeoIPv6 { for (File geoFile : inFiles) { int count = 0; InputStream in = null; + BufferedReader br = null; try { in = new BufferedInputStream(new FileInputStream(geoFile)); if (geoFile.getName().endsWith(".gz")) in = new GZIPInputStream(in); String buf = null; - BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1")); + br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1")); while ((buf = br.readLine()) != null) { try { if (buf.charAt(0) == '#') { @@ -191,6 +192,7 @@ class GeoIPv6 { return false; } finally { if (in != null) try { in.close(); } catch (IOException ioe) {} + if (br != null) try { br.close(); } catch (IOException ioe) {} } } Collections.sort(entries); diff --git a/router/java/src/net/i2p/router/util/EventLog.java b/router/java/src/net/i2p/router/util/EventLog.java index 1f07f7f7d6..1e6f166f8b 100644 --- a/router/java/src/net/i2p/router/util/EventLog.java +++ b/router/java/src/net/i2p/router/util/EventLog.java @@ -3,7 +3,6 @@ package net.i2p.router.util; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; -import java.io.InputStream; import java.io.InputStreamReader; import java.io.IOException; import java.io.OutputStream; @@ -116,10 +115,10 @@ public class EventLog { } } rv = new TreeMap<Long, String>(); - InputStream in = null; + BufferedReader br = null; try { - in = new FileInputStream(_file); - BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); + br = new BufferedReader(new InputStreamReader( + new FileInputStream(_file), "UTF-8")); String line = null; while ( (line = br.readLine()) != null) { try { @@ -141,7 +140,7 @@ public class EventLog { _cacheTime.put(event, Long.valueOf(since)); } catch (IOException ioe) { } finally { - if (in != null) try { in.close(); } catch (IOException ioe) {} + if (br != null) try { br.close(); } catch (IOException ioe) {} } return rv; } -- GitLab