home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / pr8adpl7 / emailmessage.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  1.4 KB  |  74 lines

  1. // EmailMessage.java
  2. // Encapsulates the text and headers of an email message
  3. import java.io.*;
  4.  
  5. class EmailMessage extends Message
  6. {
  7.     String text;
  8.     String from;
  9.  
  10.     EmailMessage(LineInputStream in) throws IOException
  11.     {
  12.     if (!in.markSupported())
  13.         throw new IOException("Input does not support mark/reset");
  14.     String line;
  15.  
  16.     // Read From line
  17.     from = in.gets();
  18.  
  19.     // Read headers
  20.     while((line = in.gets()).length() > 0) {
  21.         int cln = line.indexOf(':');
  22.         if (cln < 0 || cln == line.length()-1)
  23.             continue;
  24.         add(line.substring(0, cln), line.substring(cln+1).trim());
  25.         }
  26.  
  27.     // Read message until a line starting with "From ", or EOF
  28.     String t = "";
  29.     try {
  30.         in.mark(128);
  31.         while(!(line = in.gets()).startsWith("From ")) {
  32.             t += line+"\n";
  33.             in.mark(128);
  34.             }
  35.         in.reset();
  36.         }
  37.     catch(EOFException e);
  38.     settext(t);
  39.     }
  40.  
  41.     // Create an email message copied from a Message
  42.     EmailMessage(Message m)
  43.     {
  44.     for(int i=0; i<m.size(); i++)
  45.         add(m.name(i), m.value(i));
  46.     setdata(m.getdata());
  47.     }
  48.  
  49.     // write
  50.     // Output this message to the given stream, including the From header
  51.     void write(LineOutputStream out)
  52.     {
  53.     if (from != null) out.puts(from);
  54.     send(out);
  55.     }
  56.  
  57.     // settext
  58.     // Set the text of this email
  59.     void settext(String s)
  60.     {
  61.     byte d[] = new byte[s.length()];
  62.     s.getBytes(0, s.length(), d, 0);
  63.     setdata(d);
  64.     }
  65.  
  66.     // gettext
  67.     // Returns the text of this email
  68.     String gettext()
  69.     {
  70.     if (getdata() != null) return new String(getdata(), 0);
  71.     return null;
  72.     }
  73. }
  74.