Close resources

This commit is contained in:
str4d
2013-11-25 23:14:09 +00:00
parent 615ba94559
commit e77409e57a
7 changed files with 40 additions and 36 deletions

View File

@ -37,10 +37,10 @@ abstract class PersistDHT {
public static synchronized void loadDHT(KRPC krpc, File file) { public static synchronized void loadDHT(KRPC krpc, File file) {
Log log = I2PAppContext.getGlobalContext().logManager().getLog(PersistDHT.class); Log log = I2PAppContext.getGlobalContext().logManager().getLog(PersistDHT.class);
int count = 0; int count = 0;
FileInputStream in = null; BufferedReader br = null;
try { try {
in = new FileInputStream(file); br = new BufferedReader(new InputStreamReader(
BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1")); new FileInputStream(file), "ISO-8859-1"));
String line = null; String line = null;
while ( (line = br.readLine()) != null) { while ( (line = br.readLine()) != null) {
if (line.startsWith("#")) if (line.startsWith("#"))
@ -61,7 +61,7 @@ abstract class PersistDHT {
if (log.shouldLog(Log.WARN) && file.exists()) if (log.shouldLog(Log.WARN) && file.exists())
log.warn("Error reading the DHT File", ioe); log.warn("Error reading the DHT File", ioe);
} finally { } finally {
if (in != null) try { in.close(); } catch (IOException ioe) {} if (br != null) try { br.close(); } catch (IOException ioe) {}
} }
if (log.shouldLog(Log.INFO)) if (log.shouldLog(Log.INFO))
log.info("Loaded " + count + " nodes from " + file); log.info("Loaded " + count + " nodes from " + file);

View File

@ -214,12 +214,12 @@ public class SAMBridge implements Runnable, ClientApp {
private void loadKeys() { private void loadKeys() {
synchronized (nameToPrivKeys) { synchronized (nameToPrivKeys) {
nameToPrivKeys.clear(); nameToPrivKeys.clear();
FileInputStream in = null; BufferedReader br = null;
try { try {
in = new FileInputStream(persistFilename); br = new BufferedReader(new InputStreamReader(
BufferedReader reader = new BufferedReader(new InputStreamReader(in)); new FileInputStream(persistFilename)));
String line = null; String line = null;
while ( (line = reader.readLine()) != null) { while ( (line = br.readLine()) != null) {
int eq = line.indexOf('='); int eq = line.indexOf('=');
String name = line.substring(0, eq); String name = line.substring(0, eq);
String privKeys = line.substring(eq+1); String privKeys = line.substring(eq+1);
@ -230,7 +230,7 @@ public class SAMBridge implements Runnable, ClientApp {
} catch (IOException ioe) { } catch (IOException ioe) {
_log.error("Unable to read the keys from " + persistFilename, ioe); _log.error("Unable to read the keys from " + persistFilename, ioe);
} finally { } finally {
if (in != null) try { in.close(); } catch (IOException ioe) {} if (br != null) try { br.close(); } catch (IOException ioe) {}
} }
} }
} }

View File

@ -215,10 +215,10 @@ public class Blocklist {
int badcount = 0; int badcount = 0;
int peercount = 0; int peercount = 0;
long ipcount = 0; long ipcount = 0;
FileInputStream in = null; BufferedReader br = null;
try { try {
in = new FileInputStream(BLFile); br = new BufferedReader(new InputStreamReader(
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); new FileInputStream(BLFile), "UTF-8"));
String buf = null; String buf = null;
while ((buf = br.readLine()) != null && count < maxSize) { while ((buf = br.readLine()) != null && count < maxSize) {
Entry e = parse(buf, true); Entry e = parse(buf, true);
@ -247,7 +247,7 @@ public class Blocklist {
_log.log(Log.CRIT, "OOM reading the blocklist"); _log.log(Log.CRIT, "OOM reading the blocklist");
return; return;
} finally { } finally {
if (in != null) try { in.close(); } catch (IOException ioe) {} if (br != null) try { br.close(); } catch (IOException ioe) {}
} }
if (_wrapSave != null) { if (_wrapSave != null) {
@ -405,10 +405,10 @@ public class Blocklist {
private int getSize(File BLFile) { private int getSize(File BLFile) {
if ( (!BLFile.exists()) || (BLFile.length() <= 0) ) return 0; if ( (!BLFile.exists()) || (BLFile.length() <= 0) ) return 0;
int lines = 0; int lines = 0;
FileInputStream in = null; BufferedReader br = null;
try { try {
in = new FileInputStream(BLFile); br = new BufferedReader(new InputStreamReader(
BufferedReader br = new BufferedReader(new InputStreamReader(in, "ISO-8859-1")); new FileInputStream(BLFile), "ISO-8859-1"));
while (br.readLine() != null) { while (br.readLine() != null) {
lines++; lines++;
} }
@ -417,7 +417,7 @@ public class Blocklist {
_log.warn("Error reading the BLFile", ioe); _log.warn("Error reading the BLFile", ioe);
return 0; return 0;
} finally { } finally {
if (in != null) try { in.close(); } catch (IOException ioe) {} if (br != null) try { br.close(); } catch (IOException ioe) {}
} }
return lines; return lines;
} }
@ -770,10 +770,10 @@ public class Blocklist {
for (Iterator<byte[]> iter = ips.iterator(); iter.hasNext(); ) { for (Iterator<byte[]> iter = ips.iterator(); iter.hasNext(); ) {
byte ip[] = iter.next(); byte ip[] = iter.next();
int ipint = toInt(ip); int ipint = toInt(ip);
FileInputStream in = null; BufferedReader br = null;
try { try {
in = new FileInputStream(BLFile); br = new BufferedReader(new InputStreamReader(
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); new FileInputStream(BLFile), "UTF-8"));
String buf = null; String buf = null;
// assume the file is unsorted, so go through the whole thing // assume the file is unsorted, so go through the whole thing
while ((buf = br.readLine()) != null) { while ((buf = br.readLine()) != null) {
@ -782,7 +782,7 @@ public class Blocklist {
continue; continue;
} }
if (match(ipint, toEntry(e.ip1, e.ip2))) { 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}"); String reason = _x("IP banned by blocklist.txt entry {0}");
// only one translate parameter for now // only one translate parameter for now
//for (int i = 0; i < 4; i++) { //for (int i = 0; i < 4; i++) {
@ -801,7 +801,7 @@ public class Blocklist {
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn("Error reading the BLFile", ioe); _log.warn("Error reading the BLFile", ioe);
} finally { } 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 // We already banlisted in banlist(peer), that's good enough

View File

@ -67,13 +67,16 @@ public class MultiRouter {
Scanner scan = new Scanner(args[0]); Scanner scan = new Scanner(args[0]);
if (!scan.hasNextInt()) { if (!scan.hasNextInt()) {
usage(); usage();
scan.close();
return; return;
} }
nbrRouters = scan.nextInt(); nbrRouters = scan.nextInt();
if (nbrRouters < 0) { if (nbrRouters < 0) {
usage(); usage();
scan.close();
return; return;
} }
scan.close();
_out = System.out; _out = System.out;

View File

@ -194,10 +194,10 @@ class GeoIP {
_log.warn("Country file not found: " + geoFile.getAbsolutePath()); _log.warn("Country file not found: " + geoFile.getAbsolutePath());
return; return;
} }
FileInputStream in = null; BufferedReader br = null;
try { try {
in = new FileInputStream(geoFile); br = new BufferedReader(new InputStreamReader(
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); new FileInputStream(geoFile), "UTF-8"));
String line = null; String line = null;
while ( (line = br.readLine()) != null) { while ( (line = br.readLine()) != null) {
try { try {
@ -215,7 +215,7 @@ class GeoIP {
if (_log.shouldLog(Log.ERROR)) if (_log.shouldLog(Log.ERROR))
_log.error("Error reading the Country File", ioe); _log.error("Error reading the Country File", ioe);
} finally { } 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]; String[] rv = new String[search.length];
int idx = 0; int idx = 0;
long start = _context.clock().now(); long start = _context.clock().now();
FileInputStream in = null; BufferedReader br = null;
try { try {
in = new FileInputStream(geoFile);
String buf = null; 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) { while ((buf = br.readLine()) != null && idx < search.length) {
try { try {
if (buf.charAt(0) == '#') { if (buf.charAt(0) == '#') {
@ -288,7 +288,7 @@ class GeoIP {
if (_log.shouldLog(Log.ERROR)) if (_log.shouldLog(Log.ERROR))
_log.error("Error reading the geoFile", ioe); _log.error("Error reading the geoFile", ioe);
} finally { } finally {
if (in != null) try { in.close(); } catch (IOException ioe) {} if (br != null) try { br.close(); } catch (IOException ioe) {}
} }
if (_log.shouldLog(Log.INFO)) { if (_log.shouldLog(Log.INFO)) {

View File

@ -158,12 +158,13 @@ class GeoIPv6 {
for (File geoFile : inFiles) { for (File geoFile : inFiles) {
int count = 0; int count = 0;
InputStream in = null; InputStream in = null;
BufferedReader br = null;
try { try {
in = new BufferedInputStream(new FileInputStream(geoFile)); in = new BufferedInputStream(new FileInputStream(geoFile));
if (geoFile.getName().endsWith(".gz")) if (geoFile.getName().endsWith(".gz"))
in = new GZIPInputStream(in); in = new GZIPInputStream(in);
String buf = null; 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) { while ((buf = br.readLine()) != null) {
try { try {
if (buf.charAt(0) == '#') { if (buf.charAt(0) == '#') {
@ -191,6 +192,7 @@ class GeoIPv6 {
return false; return false;
} finally { } finally {
if (in != null) try { in.close(); } catch (IOException ioe) {} if (in != null) try { in.close(); } catch (IOException ioe) {}
if (br != null) try { br.close(); } catch (IOException ioe) {}
} }
} }
Collections.sort(entries); Collections.sort(entries);

View File

@ -3,7 +3,6 @@ package net.i2p.router.util;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
@ -116,10 +115,10 @@ public class EventLog {
} }
} }
rv = new TreeMap<Long, String>(); rv = new TreeMap<Long, String>();
InputStream in = null; BufferedReader br = null;
try { try {
in = new FileInputStream(_file); br = new BufferedReader(new InputStreamReader(
BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8")); new FileInputStream(_file), "UTF-8"));
String line = null; String line = null;
while ( (line = br.readLine()) != null) { while ( (line = br.readLine()) != null) {
try { try {
@ -141,7 +140,7 @@ public class EventLog {
_cacheTime.put(event, Long.valueOf(since)); _cacheTime.put(event, Long.valueOf(since));
} catch (IOException ioe) { } catch (IOException ioe) {
} finally { } finally {
if (in != null) try { in.close(); } catch (IOException ioe) {} if (br != null) try { br.close(); } catch (IOException ioe) {}
} }
return rv; return rv;
} }