diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java b/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java index 628a8c438d..c78ade3e86 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java +++ b/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java @@ -135,7 +135,7 @@ class PeerConnectionOut implements Runnable nm = null; } - if (m == null && nm != null) + if (nm != null) { m = nm; //SimpleTimer.getInstance().removeEvent(nm.expireEvent); diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java index 42bad53f0c..d38be61ec4 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/I2PTunnel.java @@ -1415,6 +1415,8 @@ public class I2PTunnel extends EventDispatcherImpl implements Logging { //_log.error(getPrefix() + "Error generating keys to out", ioe); //notifyEvent("genkeysResult", "error"); return; + } finally { + if(pubdest != null) try { pubdest.close(); } catch(IOException ioe) {} } } else if (args.length != 1) { l.log("genkeys []\n" + diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/irc/IrcInboundFilter.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/irc/IrcInboundFilter.java index 5270cbb6b9..5f0dd83338 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/irc/IrcInboundFilter.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/irc/IrcInboundFilter.java @@ -94,7 +94,8 @@ public class IrcInboundFilter implements Runnable { } catch (RuntimeException re) { _log.error("Error filtering inbound data", re); } finally { - try { local.close(); } catch (IOException e) {} + try { in.close(); } catch (IOException e) {} + try { local.close(); } catch(IOException e) {} } if(_log.shouldLog(Log.DEBUG)) _log.debug("IrcInboundFilter: Done."); diff --git a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/SOCKS5Server.java b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/SOCKS5Server.java index 1bff169075..ff9d0a802d 100644 --- a/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/SOCKS5Server.java +++ b/apps/i2ptunnel/java/src/net/i2p/i2ptunnel/socks/SOCKS5Server.java @@ -468,8 +468,10 @@ public class SOCKS5Server extends SOCKSServer { if (dest == null) throw new SOCKSException("Outproxy not found"); I2PSocket destSock = tun.createI2PSocket(I2PAppContext.getGlobalContext().namingService().lookup(proxy), proxyOpts); + DataOutputStream out = null; + DataInputStream in = null; try { - DataOutputStream out = new DataOutputStream(destSock.getOutputStream()); + out = new DataOutputStream(destSock.getOutputStream()); boolean authAvail = Boolean.parseBoolean(props.getProperty(I2PTunnelHTTPClientBase.PROP_OUTPROXY_AUTH)); String configUser = null; String configPW = null; @@ -497,7 +499,7 @@ public class SOCKS5Server extends SOCKSServer { out.flush(); // read init reply - DataInputStream in = new DataInputStream(destSock.getInputStream()); + in = new DataInputStream(destSock.getInputStream()); // is this right or should we not try to do 5-to-4 conversion? int hisVersion = in.readByte(); if (hisVersion != SOCKS_VERSION_5 /* && addrtype == AddressType.DOMAINNAME */ ) @@ -573,6 +575,8 @@ public class SOCKS5Server extends SOCKSServer { throw new SOCKSException("Outproxy rejected request, response = " + reply); // throw away the address in the response // todo pass the response through? + out.close(); + in.close(); } catch (IOException e) { try { destSock.close(); } catch (IOException ioe) {} throw e; diff --git a/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java b/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java index 01108f2467..062ab73d5e 100644 --- a/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java +++ b/apps/routerconsole/java/src/net/i2p/router/web/LogsHelper.java @@ -168,9 +168,10 @@ 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); - BufferedReader in = new BufferedReader(new InputStreamReader(fis)); + in = new BufferedReader(new InputStreamReader(fis)); List lines = new ArrayList(maxNumLines); String line = null; while ( (line = in.readLine()) != null) { @@ -186,7 +187,7 @@ public class LogsHelper extends HelperBase { } catch (IOException ioe) { return null; } finally { - if (fis != null) try { fis.close(); } catch (IOException ioe) {} + if (in != null) try { in.close(); } catch (IOException ioe) {} } } } diff --git a/apps/sam/java/src/net/i2p/sam/client/SAMStreamSend.java b/apps/sam/java/src/net/i2p/sam/client/SAMStreamSend.java index 7787c9390d..cd70161eb6 100644 --- a/apps/sam/java/src/net/i2p/sam/client/SAMStreamSend.java +++ b/apps/sam/java/src/net/i2p/sam/client/SAMStreamSend.java @@ -81,7 +81,7 @@ public class SAMStreamSend { public void streamClosedReceived(String result, int id, String message) { Sender sender = null; synchronized (_remotePeers) { - sender = _remotePeers.remove(new Integer(id)); + sender = _remotePeers.remove(Integer.valueOf(id)); } if (sender != null) { sender.closed(); @@ -171,7 +171,7 @@ public class SAMStreamSend { _remoteDestination = new String(dest, 0, read); synchronized (_remotePeers) { _connectionId = _remotePeers.size() + 1; - _remotePeers.put(new Integer(_connectionId), Sender.this); + _remotePeers.put(Integer.valueOf(_connectionId), Sender.this); } _context.statManager().createRateStat("send." + _connectionId + ".totalSent", "Data size sent", "swarm", new long[] { 30*1000, 60*1000, 5*60*1000 }); 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 83dcf510fa..31d983ee15 100644 --- a/apps/sam/java/src/net/i2p/sam/client/SAMStreamSink.java +++ b/apps/sam/java/src/net/i2p/sam/client/SAMStreamSink.java @@ -83,7 +83,7 @@ public class SAMStreamSink { public void streamClosedReceived(String result, int id, String message) { Sink sink = null; synchronized (_remotePeers) { - sink = _remotePeers.remove(new Integer(id)); + sink = _remotePeers.remove(Integer.valueOf(id)); } if (sink != null) { sink.closed(); @@ -96,7 +96,7 @@ public class SAMStreamSink { public void streamDataReceived(int id, byte data[], int offset, int length) { Sink sink = null; synchronized (_remotePeers) { - sink = _remotePeers.get(new Integer(id)); + sink = _remotePeers.get(Integer.valueOf(id)); } if (sink != null) { sink.received(data, offset, length); @@ -111,7 +111,7 @@ public class SAMStreamSink { try { Sink sink = new Sink(id, dest); synchronized (_remotePeers) { - _remotePeers.put(new Integer(id), sink); + _remotePeers.put(Integer.valueOf(id), sink); } } catch (IOException ioe) { _log.error("Error creating a new sink", ioe); @@ -169,15 +169,17 @@ public class SAMStreamSink { } private boolean writeDest(String dest) { + FileOutputStream fos = null; try { - FileOutputStream fos = new FileOutputStream(_destFile); + fos = new FileOutputStream(_destFile); fos.write(dest.getBytes()); - fos.close(); - return true; } catch (Exception e) { _log.error("Error writing to " + _destFile, e); return false; + } finally { + if(fos != null) try { fos.close(); } catch(IOException ioe) {} } + return true; } private class Sink { diff --git a/apps/susimail/src/src/i2p/susi/util/Config.java b/apps/susimail/src/src/i2p/susi/util/Config.java index 9752f05a41..4e068669df 100644 --- a/apps/susimail/src/src/i2p/susi/util/Config.java +++ b/apps/susimail/src/src/i2p/susi/util/Config.java @@ -27,6 +27,7 @@ import i2p.susi.debug.Debug; import java.io.File; import java.io.FileInputStream; +import java.io.InputStream; import java.io.IOException; import java.util.Properties; @@ -91,10 +92,14 @@ public class Config { { // DEBUG level logging won't work here since we haven't loaded the config yet... properties = new Properties(); + InputStream iv = null; try { - properties.load( Config.class.getResourceAsStream( "/susimail.properties" ) ); + iv = Config.class.getResourceAsStream("/susimail.properties"); + properties.load(iv); } catch (Exception e) { Debug.debug(Debug.ERROR, "Could not open WEB-INF/classes/susimail.properties (possibly in jar), reason: " + e); + } finally { + if(iv != null) try { iv.close(); } catch(IOException ioe) {} } try { File cfg = new File(I2PAppContext.getGlobalContext().getConfigDir(), "susimail.config"); diff --git a/core/java/src/net/i2p/crypto/CertUtil.java b/core/java/src/net/i2p/crypto/CertUtil.java index 55d04a0864..359ce7aacf 100644 --- a/core/java/src/net/i2p/crypto/CertUtil.java +++ b/core/java/src/net/i2p/crypto/CertUtil.java @@ -39,11 +39,12 @@ public class CertUtil { */ public static boolean saveCert(Certificate cert, File file) { OutputStream os = null; + PrintWriter wr = null; try { // Get the encoded form which is suitable for exporting byte[] buf = cert.getEncoded(); os = new SecureFileOutputStream(file); - PrintWriter wr = new PrintWriter(os); + wr = new PrintWriter(os); wr.println("-----BEGIN CERTIFICATE-----"); String b64 = Base64.encode(buf, true); // true = use standard alphabet for (int i = 0; i < b64.length(); i += LINE_LENGTH) { diff --git a/core/java/src/net/i2p/util/FileUtil.java b/core/java/src/net/i2p/util/FileUtil.java index fd43d8f863..02024d02e0 100644 --- a/core/java/src/net/i2p/util/FileUtil.java +++ b/core/java/src/net/i2p/util/FileUtil.java @@ -355,9 +355,10 @@ public class FileUtil { File f = new File(filename); if (!f.exists()) return null; FileInputStream fis = null; + BufferedReader in = null; try { fis = new FileInputStream(f); - BufferedReader in = new BufferedReader(new InputStreamReader(fis, "UTF-8")); + in = new BufferedReader(new InputStreamReader(fis, "UTF-8")); List lines = new ArrayList(maxNumLines > 0 ? maxNumLines : 64); String line = null; while ( (line = in.readLine()) != null) { @@ -377,7 +378,7 @@ public class FileUtil { } catch (IOException ioe) { return null; } finally { - if (fis != null) try { fis.close(); } catch (IOException ioe) {} + if (in != null) try { in.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 dd1e910b5a..bc26bbe59a 100644 --- a/core/java/src/net/i2p/util/TranslateReader.java +++ b/core/java/src/net/i2p/util/TranslateReader.java @@ -391,14 +391,16 @@ 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", - new FileInputStream(file)); + fio); int c; while ((c = r.read()) >= 0) { System.out.print((char)c); } System.out.flush(); + r.close(); } /** @param files ignore 0 */ diff --git a/history.txt b/history.txt index b337281a22..d61da9dca0 100644 --- a/history.txt +++ b/history.txt @@ -1,3 +1,6 @@ +2014-04-21 dg + * findbugs: mostly stream closure fixes in router, apps, core + 2014-04-20 zzz * SusiMail: - Implement extensive pipelining in POP3 for a big speedup diff --git a/router/java/src/net/i2p/router/Router.java b/router/java/src/net/i2p/router/Router.java index 600c35f718..4498e27982 100644 --- a/router/java/src/net/i2p/router/Router.java +++ b/router/java/src/net/i2p/router/Router.java @@ -1314,9 +1314,10 @@ public class Router implements RouterClock.ClockShiftListener { return; // this is similar to FileUtil.readTextFile() but we can't use any I2P classes here FileInputStream fis = null; + BufferedReader in = null; try { fis = new FileInputStream(deleteFile); - BufferedReader in = new BufferedReader(new InputStreamReader(fis, "UTF-8")); + in = new BufferedReader(new InputStreamReader(fis, "UTF-8")); String line; while ( (line = in.readLine()) != null) { String fl = line.trim(); @@ -1331,9 +1332,9 @@ public class Router implements RouterClock.ClockShiftListener { System.out.println("INFO: File [" + fl + "] deleted"); } } - } catch (IOException ioe) { - } finally { - if (fis != null) try { fis.close(); } catch (IOException ioe) {} + } catch (IOException ioe) {} + finally { + if (in != null) try { in.close(); } catch(IOException ioe) {} if (deleteFile.delete()) System.out.println("INFO: File [" + DELETE_FILE + "] deleted"); }