major simplifications to Iterator api
[org.ibex.mail.git] / src / org / ibex / mail / protocol / Mbox.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail.protocol;
6 import org.ibex.io.*;
7 import org.ibex.crypto.*;
8 import org.ibex.jinetd.Listener;
9 import org.ibex.mail.*;
10 import org.ibex.util.*;
11 import org.ibex.mail.target.*;
12 import java.util.*;
13 import java.net.*;
14 import java.text.*;
15 import java.io.*;
16
17 public class Mbox {
18     public static Message[] parse(Stream stream) {
19         StringBuffer buf = null;
20         Vec vec = new Vec();
21         for(String s = stream.readln(); ; s = stream.readln()) {
22             if (s == null || s.startsWith("From ")) {
23                 try {
24                     if (buf != null) vec.addElement(Message.newMessage(new Fountain.StringFountain(buf.toString())));
25                 } catch (Exception e) { Log.warn(Mbox.class, e); }
26                 if (s == null) break;
27                 buf = new StringBuffer();
28             } else if (buf != null) {
29                 if (buf.length() == 0 && s.length() == 0) continue;   // skip blank line after From
30                 buf.append(s);
31                 buf.append("\r\n");
32             }
33         }
34         return (Message[])vec.copyInto(new Message[vec.size()]);
35     }
36 }