KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > mail > util > MessageOutputStream


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

19
20 package gnu.mail.util;
21
22 import java.io.*;
23
24 /**
25  * An output stream that escapes any dots on a line by themself with
26  * another dot, for the purposes of sending messages to SMTP and NNTP
27  * servers.
28  *
29  * @author <a HREF="mailto:dog@gnu.org">Chris Burdess</a>
30  */

31 public class MessageOutputStream
32     extends FilterOutputStream
33 {
34     
35     /**
36      * The stream termination octet.
37      */

38     public static final int END = 46;
39     
40     /**
41      * The line termination octet.
42      */

43     public static final int LF = 10;
44     
45     int[] last = { LF, LF }; // the last character written to the stream
46

47     /**
48      * Constructs a message output stream connected to the specified output stream.
49      * @param out the target output stream
50      */

51     public MessageOutputStream(OutputStream out)
52     {
53         super(out);
54     }
55     
56     /**
57      * Writes a character to the underlying stream.
58      * @exception IOException if an I/O error occurred
59      */

60     public void write(int ch)
61         throws IOException
62     {
63         if (last[0]==LF && last[1]==END && ch==LF)
64             out.write(END);
65         out.write(ch);
66         last[0] = last[1];
67         last[1] = ch;
68     }
69     
70     /**
71      * Writes a portion of a byte array to the underlying stream.
72      * @exception IOException if an I/O error occurred
73      */

74     public void write(byte b[], int off, int len)
75         throws IOException
76     {
77         for (int i = 0; i < len; i++)
78         {
79             int ch = (int)b[off+i];
80             if (last[0]==LF && last[1]==END && ch==LF)
81             {
82                 byte[] b2 = new byte[b.length+1];
83                 System.arraycopy(b, off, b2, off, i);
84                 b2[off+i] = END;
85                 System.arraycopy(b, off+i, b2, off+i+1, len-i);
86                 b = b2;
87                 i++; len++;
88             }
89             last[0] = last[1];
90             last[1] = ch;
91         }
92         out.write(b, off, len);
93     }
94     
95 }
96
97
Popular Tags