fix too many open files problem
authoradam <adam@megacz.com>
Mon, 2 Aug 2004 16:49:56 +0000 (16:49 +0000)
committeradam <adam@megacz.com>
Mon, 2 Aug 2004 16:49:56 +0000 (16:49 +0000)
darcs-hash:20040802164956-5007d-40c6ad018289529dfbacd7235fe1fd894798399e.gz

src/org/ibex/mail/target/FileBasedMailbox.java

index a934fac..60e5246 100644 (file)
@@ -72,24 +72,31 @@ public class FileBasedMailbox extends Mailbox.Default {
                 tmp.renameTo(uidNext);
                 return 1;
             }
-            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)));
-                pw.println(ret+1);
-                pw.flush();
-                pw.close();
-                tmp.renameTo(uidNext);
-            }
+            FileInputStream fis = null;
+            int ret = -1;
+            try {
+                fis = new FileInputStream(uidNext);
+                BufferedReader br = new BufferedReader(new InputStreamReader(fis));
+                ret = Integer.parseInt(br.readLine());
+                if (inc) {
+                    File tmp = new File(uidNext + "-");
+                    FileOutputStream fos = null;
+                    try {
+                        fos = new FileOutputStream(tmp);
+                        PrintWriter pw = new PrintWriter(new OutputStreamWriter(fos));
+                        pw.println(ret+1);
+                        pw.flush();
+                    } finally { if (fos != null) fos.close(); }
+                    tmp.renameTo(uidNext);
+                }
+            } finally { if (fis != null) fis.close(); }
             return ret;
         } catch (IOException e) { throw new MailException.IOException(e); }
     }        
 
     public synchronized void add(Message message) { add(message, Mailbox.Flag.RECENT); }
     public synchronized void add(Message message, int flags) {
-        Log.info(this, "adding message to ["+toString()+"]:\n" + message.summary());
+        Log.info(path, message.summary());
         try {
             int num = new File(path).list(filter).length;
            String name = path + slash + uidNext(true) + "." +
@@ -99,7 +106,7 @@ public class FileBasedMailbox extends Mailbox.Default {
                 ((flags & Mailbox.Flag.ANSWERED) == Mailbox.Flag.ANSWERED ? "a" : "") +
                 ((flags & Mailbox.Flag.FLAGGED) == Mailbox.Flag.FLAGGED ? "f" : "") +
                 ((flags & Mailbox.Flag.SEEN) == Mailbox.Flag.SEEN ? "s" : "");
-            Log.info(this, "    with chosen filename " + name);
+            //Log.info(this, "    with chosen filename " + name);
             File target = new File(name);
             File f = new File(target.getCanonicalPath() + "-");
             FileOutputStream fo = new FileOutputStream(f);
@@ -109,7 +116,7 @@ public class FileBasedMailbox extends Mailbox.Default {
             message.dump(stream);
             fo.close();
             f.renameTo(target);
-            Log.info(this, "    done writing.");
+            //Log.info(this, "    done writing.");
         } catch (IOException e) { throw new MailException.IOException(e); }
     }
 
@@ -124,25 +131,11 @@ public class FileBasedMailbox extends Mailbox.Default {
            if (cur >= names.length) return null;
             try {
                 File file = new File(path + File.separatorChar + names[cur]);
-                FileInputStream fis = new FileInputStream(file);
-                Stream stream = new Stream(fis);
-                Address envelopeFrom = null;
-                Address envelopeTo = null;
-                /*
-                for(String s = stream.readln(); s != null; s = stream.readln()) {
-                    if (s.startsWith("X-org.ibex.mail.headers.envelope.From: "))
-                        envelopeFrom = Address.parse(s.substring(38).trim());
-                    else if (s.startsWith("X-org.ibex.mail.headers.envelope.To: "))
-                        envelopeTo = Address.parse(s.substring(36).trim());
-                    else {
-                        stream.unread(s + "\r\n");
-                        break;
-                    }
-                }
-                */
-                Message ret = new Message(stream, new Message.Envelope(null, null, new Date(file.lastModified())));
-                fis.close();
-                return ret;
+                FileInputStream fis = null;
+                try {
+                    fis = new FileInputStream(file);
+                    return new Message(new Stream(fis), new Message.Envelope(null, null, new Date(file.lastModified())));
+                } finally { if (fis != null) fis.close(); }
             } catch (IOException e) { throw new MailException.IOException(e);
             } catch (Message.Malformed e) { throw new MailException(e.getMessage()); }
         }