KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > mail > util > QEncoderStream


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21
22 /*
23  * @(#)QEncoderStream.java 1.5 05/08/29
24  *
25  * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
26  */

27
28 package com.sun.mail.util;
29
30 import java.io.*;
31
32 /**
33  * This class implements a Q Encoder as defined by RFC 2047 for
34  * encoding MIME headers. It subclasses the QPEncoderStream class.
35  *
36  * @author John Mani
37  */

38
39 public class QEncoderStream extends QPEncoderStream {
40
41     private String JavaDoc specials;
42     private static String JavaDoc WORD_SPECIALS = "=_?\"#$%&'(),.:;<>@[\\]^`{|}~";
43     private static String JavaDoc TEXT_SPECIALS = "=_?";
44
45     /**
46      * Create a Q encoder that encodes the specified input stream
47      * @param out the output stream
48      * @param encodingWord true if we are Q-encoding a word within a
49      * phrase.
50      */

51     public QEncoderStream(OutputStream out, boolean encodingWord) {
52     super(out, Integer.MAX_VALUE); // MAX_VALUE is 2^31, should
53
// suffice (!) to indicate that
54
// CRLFs should not be inserted
55
// when encoding rfc822 headers
56

57     // a RFC822 "word" token has more restrictions than a
58
// RFC822 "text" token.
59
specials = encodingWord ? WORD_SPECIALS : TEXT_SPECIALS;
60     }
61
62     /**
63      * Encodes the specified <code>byte</code> to this output stream.
64      * @param c the <code>byte</code>.
65      * @exception IOException if an I/O error occurs.
66      */

67     public void write(int c) throws IOException {
68     c = c & 0xff; // Turn off the MSB.
69
if (c == ' ')
70         output('_', false);
71     else if (c < 040 || c >= 0177 || specials.indexOf(c) >= 0)
72         // Encoding required.
73
output(c, true);
74     else // No encoding required
75
output(c, false);
76     }
77
78     /**
79      * Returns the length of the encoded version of this byte array.
80      */

81     public static int encodedLength(byte[] b, boolean encodingWord) {
82     int len = 0;
83     String JavaDoc specials = encodingWord ? WORD_SPECIALS: TEXT_SPECIALS;
84     for (int i = 0; i < b.length; i++) {
85         int c = b[i] & 0xff; // Mask off MSB
86
if (c < 040 || c >= 0177 || specials.indexOf(c) >= 0)
87         // needs encoding
88
len += 3; // Q-encoding is 1 -> 3 conversion
89
else
90         len++;
91     }
92     return len;
93     }
94
95     /**** begin TEST program ***
96     public static void main(String argv[]) throws Exception {
97         FileInputStream infile = new FileInputStream(argv[0]);
98         QEncoderStream encoder = new QEncoderStream(System.out);
99         int c;
100  
101         while ((c = infile.read()) != -1)
102             encoder.write(c);
103         encoder.close();
104     }
105     *** end TEST program ***/

106 }
107
Popular Tags