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) {
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);

View File

@ -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) {}
}
}
}

View File

@ -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

View File

@ -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;

View File

@ -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)) {

View File

@ -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);

View File

@ -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;
}