KickJava   Java API By Example, From Geeks To Geeks.

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


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

21 package gnu.mail.util;
22
23 // Imports
24
import java.io.OutputStream JavaDoc;
25 import java.io.FilterOutputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 /**
29  * Quoted Printable Encoding stream.
30  *
31  * @author Andrew Selkirk
32  * @author <a HREF="mailto:dog@gnu.org">Chris Burdess</a>
33  * @version 1.0
34  * @see java.io.FilterOutputStream
35  **/

36 public class QPOutputStream
37 extends FilterOutputStream JavaDoc
38 {
39
40   /**
41    * Char array used in decimal to hexidecimal conversion.
42    */

43   private static final char[] hex = {'0','1','2','3','4','5','6',
44                      '7','8','9','A','B','C','D',
45                      'E','F'};
46
47   /**
48    * Current byte position in output.
49    */

50   private int count;
51
52   /**
53    * Number of bytes per line.
54    */

55   private int bytesPerLine;
56
57   /**
58    * Flag when a space is seen.
59    */

60   private boolean gotSpace;
61
62   /**
63    * Flag when a CR is seen.
64    */

65   private boolean gotCR;
66
67
68   //-------------------------------------------------------------
69
// Initialization ---------------------------------------------
70
//-------------------------------------------------------------
71

72   /**
73    * Create a new Quoted Printable Encoding stream.
74    * @param stream Output stream
75    * @param length Number of bytes per line
76    */

77   public QPOutputStream(OutputStream JavaDoc stream, int length)
78   {
79     super(stream);
80     this.bytesPerLine = length;
81     this.count = 0;
82     this.gotSpace = false;
83     this.gotCR = false;
84   } // QPEncoderStream()
85

86   /**
87    * Create a new Quoted Printable Encoding stream with
88    * the default 76 bytes per line.
89    * @param stream Output stream
90    */

91   public QPOutputStream(OutputStream JavaDoc stream)
92   {
93     this(stream, 76);
94   } // QPEWncoderStream()
95

96
97   //-------------------------------------------------------------
98
// Methods ----------------------------------------------------
99
//-------------------------------------------------------------
100

101   /**
102    * Flush encoding buffer.
103    * @exception IOException IO Exception occurred
104    */

105   public void flush() throws IOException JavaDoc
106   {
107     out.flush();
108   } // flush()
109

110   /**
111    * Write bytes to encoding stream.
112    * @param bytes Byte array to read values from
113    * @param offset Offset to start reading bytes from
114    * @param length Number of bytes to read
115    * @exception IOException IO Exception occurred
116    */

117   public void write(byte[] bytes, int offset, int length)
118   throws IOException JavaDoc
119   {
120
121     // Variables
122
int index;
123
124     // Process Each Byte
125
for (index = offset; index < length; index++)
126     {
127       write(bytes[index]);
128     } // for
129

130   } // write()
131

132   /**
133    * Write bytes to stream.
134    * @param bytes Byte array to write to stream
135    * @exception IOException IO Exception occurred
136    */

137   public void write(byte[] bytes) throws IOException JavaDoc
138   {
139     write(bytes, 0, bytes.length);
140   } // write()
141

142   /**
143    * Write a byte to the stream.
144    * @param b Byte to write to the stream
145    * @exception IOException IO Exception occurred
146    */

147   public void write(int b) throws IOException JavaDoc
148   {
149     b &= 0xff;
150     if (gotSpace)
151     {
152       if (b=='\n' || b=='\r')
153         output(' ', true);
154       else
155         output(' ', false);
156       gotSpace = false;
157     }
158     if (b==' ')
159       gotSpace = true;
160     else if (b=='\r')
161     {
162       gotCR = true;
163       outputCRLF();
164     }
165     else if (b=='\n')
166     {
167       if (gotCR)
168         gotCR = false;
169       else
170         outputCRLF();
171     }
172     if (b<' ' || b>=127 || b=='=')
173       output(b, true);
174     else
175       output(b, false);
176   } // write()
177

178   /**
179    * Close stream.
180    * @exception IOException IO Exception occurred
181    */

182   public void close() throws IOException JavaDoc
183   {
184     out.close();
185   } // close()
186

187   /**
188    * ????
189    * @param b ??
190    * @param value ??
191    * @exception IOException IO Exception occurred
192    */

193   protected void output(int b, boolean value)
194     throws IOException JavaDoc
195   {
196     if (value)
197     {
198       if ((count += 3) > bytesPerLine)
199       {
200         out.write('=');
201         out.write('\r');
202         out.write('\n');
203         count = 3;
204       }
205       out.write('=');
206       out.write(hex[b >> 4]);
207       out.write(hex[b & 0xf]);
208     }
209     else
210     {
211       if (++count > bytesPerLine)
212       {
213         out.write('=');
214         out.write('\r');
215         out.write('\n');
216         count = 1;
217       }
218       out.write(b);
219     }
220   } // output()
221

222   /**
223    * Write CRLF byte series to stream.
224    * @exception IOException IO Exception occurred
225    */

226   private void outputCRLF() throws IOException JavaDoc
227   {
228     out.write('\r');
229     out.write('\n');
230     count = 0;
231   } // outputCRLF()
232

233
234 } // QPEncoderStream
235
Popular Tags