From fe41dc047926805d37fe53576c8dea4120c2f78c Mon Sep 17 00:00:00 2001 From: adam Date: Thu, 8 Jul 2004 09:09:42 +0000 Subject: [PATCH] be more careful about closing files and connections darcs-hash:20040708090942-5007d-4e4ca6c52c01bd9f14f1224c2e638fbbf727bfa7.gz --- src/org/ibex/jinetd/Loader.java | 8 +++++++- src/org/ibex/jinetd/Port.java | 2 ++ src/org/ibex/mail/protocol/SMTP.java | 5 ++++- src/org/ibex/mail/target/FileBasedMailbox.java | 6 +++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/org/ibex/jinetd/Loader.java b/src/org/ibex/jinetd/Loader.java index a94365e..5c933c7 100644 --- a/src/org/ibex/jinetd/Loader.java +++ b/src/org/ibex/jinetd/Loader.java @@ -108,7 +108,9 @@ public class Loader extends Watcher { File classFile = slash("BIN").slash(name.replace('.', File.separatorChar) + ".class"); if (classFile.exists()) { try { - byte[] b = InputStreamToByteArray.convert(new FileInputStream(classFile)); + FileInputStream fis = new FileInputStream(classFile); + byte[] b = InputStreamToByteArray.convert(fis); + fis.close(); Log.debug(this, " loading " + name + " from " + classFile.getAbsolutePath()); return defineClass(b, 0, b.length); } catch (Exception e) { @@ -130,8 +132,10 @@ public class Loader extends Watcher { if (ze != null) { byte[] b = InputStreamToByteArray.convert(zf.getInputStream(ze)); Log.debug(this, " loading " + name + " from " + f.getAbsolutePath()); + zf.close(); return defineClass(b, 0, b.length); } + zf.close(); } } } catch (Exception e) { @@ -150,8 +154,10 @@ public class Loader extends Watcher { if (ze != null) { byte[] b = InputStreamToByteArray.convert(zf.getInputStream(ze)); Log.debug(this, " loading " + name + " from " + f.getAbsolutePath()); + zf.close(); return defineClass(b, 0, b.length); } + zf.close(); } } } catch (Exception e) { diff --git a/src/org/ibex/jinetd/Port.java b/src/org/ibex/jinetd/Port.java index 840cec2..2bcd839 100644 --- a/src/org/ibex/jinetd/Port.java +++ b/src/org/ibex/jinetd/Port.java @@ -71,6 +71,8 @@ public class Port extends Loader { } catch (Exception e) { Log.error(l.getClass(), "Listener threw exception"); Log.error(l.getClass(), e); + } finally { + conn.close(); } } }.start(); return; diff --git a/src/org/ibex/mail/protocol/SMTP.java b/src/org/ibex/mail/protocol/SMTP.java index 636bc5d..f457986 100644 --- a/src/org/ibex/mail/protocol/SMTP.java +++ b/src/org/ibex/mail/protocol/SMTP.java @@ -122,9 +122,10 @@ public class SMTP { private static void check(String s) { if (s.startsWith("4")||s.startsWith("5")) throw new MailException(s); } private static boolean attempt(final Message m, final InetAddress mx) { boolean accepted = false; + Connection conn = null; try { Log.info(SMTP.Outgoing.class, "connecting to " + mx + "..."); - Connection conn = new Connection(new Socket(mx, 25), InetAddress.getLocalHost().getHostName()); + conn = new Connection(new Socket(mx, 25), InetAddress.getLocalHost().getHostName()); conn.setTimeout(60 * 1000); Log.info(SMTP.Outgoing.class, "connected"); check(conn.readln()); // banner @@ -143,6 +144,8 @@ public class SMTP { Log.warn(SMTP.Outgoing.class, "unable to send; error=" + e); Log.warn(SMTP.Outgoing.class, e); return false; + } finally { + if (conn != null) conn.close(); } return accepted; } diff --git a/src/org/ibex/mail/target/FileBasedMailbox.java b/src/org/ibex/mail/target/FileBasedMailbox.java index f348f4c..cba347e 100644 --- a/src/org/ibex/mail/target/FileBasedMailbox.java +++ b/src/org/ibex/mail/target/FileBasedMailbox.java @@ -75,6 +75,7 @@ public class FileBasedMailbox extends Mailbox.Default { } BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(uidNext))); int ret = Integer.parseInt(br.readLine()); + br.close(); if (inc) { File tmp = new File(uidNext + "-"); PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(tmp))); @@ -118,7 +119,10 @@ public class FileBasedMailbox extends Mailbox.Default { if (cur >= names.length) return null; try { File file = new File(path + File.separatorChar + names[cur]); - return new Message(null, null, new Stream(new FileInputStream(file))); + FileInputStream fis = new FileInputStream(file); + Message ret = new Message(null, null, new Stream(fis)); + fis.close(); + return ret; } catch (IOException e) { throw new MailException.IOException(e); } catch (Message.Malformed e) { throw new MailException(e.getMessage()); } } -- 1.7.10.4