be more careful about closing files and connections
authoradam <adam@megacz.com>
Thu, 8 Jul 2004 09:09:42 +0000 (09:09 +0000)
committeradam <adam@megacz.com>
Thu, 8 Jul 2004 09:09:42 +0000 (09:09 +0000)
darcs-hash:20040708090942-5007d-4e4ca6c52c01bd9f14f1224c2e638fbbf727bfa7.gz

src/org/ibex/jinetd/Loader.java
src/org/ibex/jinetd/Port.java
src/org/ibex/mail/protocol/SMTP.java
src/org/ibex/mail/target/FileBasedMailbox.java

index a94365e..5c933c7 100644 (file)
@@ -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) {
index 840cec2..2bcd839 100644 (file)
@@ -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;
index 636bc5d..f457986 100644 (file)
@@ -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;
         }
index f348f4c..cba347e 100644 (file)
@@ -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()); }
         }