KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * CRLFOutputStream.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 filters LFs into CR/LF pairs.
25  *
26  * @author <a HREF="mailto:dog@gnu.org">Chris Burdess</a>
27  */

28 public class CRLFOutputStream
29      extends FilterOutputStream
30 {
31
32     /** The CR octet. */
33     public final static int CR = 13;
34
35     /** The LF octet. */
36     public final static int LF = 10;
37
38     /** The CR/LF pair. */
39     public final static byte[] CRLF = {CR, LF};
40
41     /** The last byte written. */
42     protected int last;
43
44
45     /**
46      * Constructs a CR/LF output stream connected to the specified output stream.
47      *
48      * @param out output stream
49      */

50     public CRLFOutputStream(OutputStream out)
51     {
52         super(out);
53         last = -1;
54     }
55
56
57     /**
58      * Writes a character to the underlying stream.
59      *
60      * @param ch Description of Parameter
61      * @exception IOException if an I/O error occurred
62      */

63     public void write(int ch)
64         throws IOException
65     {
66         if (ch == CR) {
67             out.write(CRLF);
68         }
69         else if (ch == LF) {
70             if (last != CR) {
71                 out.write(CRLF);
72             }
73         }
74         else {
75             out.write(ch);
76         }
77         last = ch;
78     }
79
80
81     /**
82      * Writes a byte array to the underlying stream.
83      *
84      * @param b Description of Parameter
85      * @exception IOException if an I/O error occurred
86      */

87     public void write(byte b[])
88         throws IOException
89     {
90         write(b, 0, b.length);
91     }
92
93
94     /**
95      * Writes a portion of a byte array to the underlying stream.
96      *
97      * @param b byte array
98      * @param off offset in array to begin write
99      * @param len length to write
100      * @exception IOException if an I/O error occurred
101      */

102     public void write(byte b[], int off, int len)
103         throws IOException
104     {
105         int d = off;
106         len += off;
107         for (int i = off; i < len; i++) {
108             switch (b[i]) {
109                 case CR:
110                     out.write(b, d, i - d);
111                     out.write(CRLF, 0, 2);
112                     d = i + 1;
113                     break;
114                 case LF:
115                     if (last != CR) {
116                         out.write(b, d, i - d);
117                         out.write(CRLF, 0, 2);
118                     }
119                     d = i + 1;
120                     break;
121             }
122             last = b[i];
123         }
124         if (len - d > 0) {
125             out.write(b, d, len - d);
126         }
127     }
128
129
130     /**
131      * Writes the specified string to the underlying stream.
132      *
133      * @param text text string
134      * @exception IOException if an I/O error occurred
135      */

136     public void write(String JavaDoc text)
137         throws IOException
138     {
139         byte[] bytes = text.getBytes();
140         write(bytes, 0, bytes.length);
141     }
142
143
144     /**
145      * Writes a newline to the underlying stream.
146      *
147      * @exception IOException if an I/O error occurred
148      */

149     public void writeln()
150         throws IOException
151     {
152         out.write(CRLF);
153     }
154
155 }
156
157
Popular Tags