KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > providers > smtp > SMTPOutputStream


1 /*
2   GNU-Classpath Extensions: javamail
3   Copyright (C) 2002 Chris Burdess
4
5   For more information on the classpathx please mail:
6   nferrier@tapsellferrier.co.uk
7
8   This program is free software; you can redistribute it and/or
9   modify it under the terms of the GNU Lesser General Public License
10   as published by the Free Software Foundation; either version 2
11   of the License, or (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */

22 package gnu.mail.providers.smtp;
23
24 import java.io.*;
25 import gnu.mail.util.CRLFOutputStream;
26
27 /**
28  * An output stream implementing the SMTP specification for dot escaping.
29  * Objects of this class are intended to be chained with CRLFOutputStream
30  * instances as all SMTP output is intended to be CRLF-escaped.
31  *
32  * @author dog@gnu.org
33  * @version 0.1
34  */

35 public class SMTPOutputStream
36   extends FilterOutputStream
37 {
38
39   /**
40    * The LF octet.
41    */

42   public static final int LF = 0x0a;
43
44   /**
45    * The dot octet.
46    */

47   public static final int DOT = 0x2e;
48
49   /**
50    * The last octet read.
51    */

52   protected int last;
53
54   /**
55    * Constructs an SMTP output stream connected to the specified output stream.
56    * The underlying output stream should coordinate proper CRLF pairs at
57    * line ends.
58    * @param out a CRLFOutputStream
59    */

60   public SMTPOutputStream(CRLFOutputStream out)
61   {
62     super(out);
63   }
64
65   /**
66    * Writes a character to the underlying stream.
67    * @exception IOException if an I/O error occurred
68    */

69   public void write(int ch)
70     throws IOException
71   {
72     if (ch==DOT)
73     {
74       if (last==LF)
75         out.write(DOT);
76     }
77     out.write(ch);
78     last = ch;
79   }
80
81   /**
82    * Writes a byte array to the underlying stream.
83    * @exception IOException if an I/O error occurred
84    */

85   public void write(byte b[])
86     throws IOException
87   {
88     write(b, 0, b.length);
89   }
90
91   /**
92    * Writes a portion of a byte array to the underlying stream.
93    * @exception IOException if an I/O error occurred
94    */

95   public void write(byte b[], int off, int len)
96     throws IOException
97   {
98     int d = off;
99     len += off;
100     for (int i=off; i<len; i++)
101     {
102       switch (b[i])
103       {
104         case DOT:
105           int l = (i-d);
106           if (l>0)
107             out.write(b, d, l);
108           d = i;
109           if (last==LF)
110             out.write(DOT);
111           break;
112       }
113       last = b[i];
114     }
115     if (len-d>0)
116       out.write(b, d, len-d);
117   }
118
119 }
120
Popular Tags