make sure to close files
authoradam <adam@megacz.com>
Thu, 5 Aug 2004 23:24:14 +0000 (23:24 +0000)
committeradam <adam@megacz.com>
Thu, 5 Aug 2004 23:24:14 +0000 (23:24 +0000)
darcs-hash:20040805232414-5007d-176917d034753d5246d58a9b029b5449f66fb84e.gz

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

index 2602990..54347cf 100644 (file)
@@ -77,24 +77,31 @@ public class FileBasedMailbox extends Mailbox.Default implements Serializable {
                 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) + "." +
@@ -104,7 +111,7 @@ public class FileBasedMailbox extends Mailbox.Default implements Serializable {
                 ((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);
@@ -114,7 +121,7 @@ public class FileBasedMailbox extends Mailbox.Default implements Serializable {
             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); }
     }
 
@@ -129,25 +136,11 @@ public class FileBasedMailbox extends Mailbox.Default implements Serializable {
            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()); }
         }