KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * RFC2822OutputStream.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 package gnu.mail.util;
20
21 import java.io.*;
22
23 /**
24  * An output stream that ensures that lines of characters in the body are
25  * limited to 998 octets (excluding CRLF).
26  *
27  * This is required by RFC 2822, section 2.3.
28  *
29  * In order to conform to further requirements of RFC 2822 the underlying
30  * stream must be a CRLFOutputStream.
31  *
32  * @author <a HREF="mailto:dog@gnu.org">Chris Burdess</a>
33  */

34 public class RFC2822OutputStream
35      extends FilterOutputStream
36 {
37
38     /** The CR octet. */
39     public final static int CR = 13;
40
41     /** The LF octet. */
42     public final static int LF = 10;
43
44     /** The maximum allowed size of a line */
45     public final static int MaxLineLength = 998;
46
47     /** The number of bytes in the line. */
48     protected int count;
49
50
51     /**
52      * Constructs an RFC2822 output stream
53      * connected to the specified CRLF output stream.
54      *
55      * @param out the underlying CRLFOutputStream
56      */

57     public RFC2822OutputStream(CRLFOutputStream out)
58     {
59         super(out);
60         count = 0;
61     }
62
63
64     /**
65      * Writes a character to the underlying stream.
66      *
67      * @param ch Description of Parameter
68      * @exception IOException if an I/O error occurred
69      */

70     public void write(int ch)
71         throws IOException
72     {
73         if (ch == CR || ch == LF) {
74             out.write(ch);
75             count = 0;
76         }
77         else {
78             if (count > MaxLineLength) {
79                 out.write(CR);
80                 out.write(LF);
81                 count = 0;
82             }
83             out.write(ch);
84             count++;
85         }
86     }
87
88 }
89
90
Popular Tags