bogus
[org.ibex.mail.git] / src / org / ibex / mail / target / FileBasedMailbox.java
index cf47fe3..d1e0a5e 100644 (file)
@@ -2,7 +2,6 @@ package org.ibex.mail.target;
 import org.ibex.mail.*;
 import org.ibex.util.*;
 import org.ibex.io.*;
-import org.ibex.net.*;
 import java.io.*;
 import java.net.*;
 import java.util.*;
@@ -14,6 +13,8 @@ import java.text.*;
 /** An exceptionally crude implementation of Mailbox relying on POSIXy filesystem semantics */
 public class FileBasedMailbox extends Mailbox.Default {
 
+    public String toString() { return "[FileBasedMailbox " + path + "]"; }
+
     private static final char slash = File.separatorChar;
     private static final Hashtable instances = new Hashtable();
     public static FileBasedMailbox getFileBasedMailbox(String path, boolean create) {
@@ -73,6 +74,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)));
@@ -87,6 +89,7 @@ public class FileBasedMailbox extends Mailbox.Default {
 
     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());
         try {
             int num = new File(path).list(filter).length;
            String name = path + slash + uidNext(true) + "." +
@@ -96,12 +99,14 @@ 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);
             File target = new File(name);
             File f = new File(target.getCanonicalPath() + "-");
             FileOutputStream fo = new FileOutputStream(f);
             message.dump(new Stream(fo));
             fo.close();
             f.renameTo(target);
+            Log.info(this, "    done writing.");
         } catch (IOException e) { throw new MailException.IOException(e); }
     }
 
@@ -111,12 +116,17 @@ public class FileBasedMailbox extends Mailbox.Default {
         private boolean seen = false, deleted = false, draft = false, flagged = false, answered = false, recent = false;
         public Iterator() { names = new File(path).list(filter); }
 
+        public Message head() { return cur(); }
         public Message cur() {
            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)));
-            } catch (IOException e) { throw new MailException.IOException(e); }
+                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()); }
         }
         public boolean next() {
             cur++;