home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-14 | 1.4 KB | 74 lines |
- // EmailMessage.java
- // Encapsulates the text and headers of an email message
- import java.io.*;
-
- class EmailMessage extends Message
- {
- String text;
- String from;
-
- EmailMessage(LineInputStream in) throws IOException
- {
- if (!in.markSupported())
- throw new IOException("Input does not support mark/reset");
- String line;
-
- // Read From line
- from = in.gets();
-
- // Read headers
- while((line = in.gets()).length() > 0) {
- int cln = line.indexOf(':');
- if (cln < 0 || cln == line.length()-1)
- continue;
- add(line.substring(0, cln), line.substring(cln+1).trim());
- }
-
- // Read message until a line starting with "From ", or EOF
- String t = "";
- try {
- in.mark(128);
- while(!(line = in.gets()).startsWith("From ")) {
- t += line+"\n";
- in.mark(128);
- }
- in.reset();
- }
- catch(EOFException e);
- settext(t);
- }
-
- // Create an email message copied from a Message
- EmailMessage(Message m)
- {
- for(int i=0; i<m.size(); i++)
- add(m.name(i), m.value(i));
- setdata(m.getdata());
- }
-
- // write
- // Output this message to the given stream, including the From header
- void write(LineOutputStream out)
- {
- if (from != null) out.puts(from);
- send(out);
- }
-
- // settext
- // Set the text of this email
- void settext(String s)
- {
- byte d[] = new byte[s.length()];
- s.getBytes(0, s.length(), d, 0);
- setdata(d);
- }
-
- // gettext
- // Returns the text of this email
- String gettext()
- {
- if (getdata() != null) return new String(getdata(), 0);
- return null;
- }
- }
-