KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * QOutputStream.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.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24
25 /**
26  * Provides RFC 2047 "B" transfer decoding.
27  * See section 4.2:
28  * <p>
29  * The "Q" encoding is similar to the "Quoted-Printable" content-
30  * transfer-encoding defined in RFC 2045. It is designed to allow text
31  * containing mostly ASCII characters to be decipherable on an ASCII
32  * terminal without decoding.
33  * <ol>
34  * <li>Any 8-bit value may be represented by a "=" followed by two
35  * hexadecimal digits. For example, if the character set in use
36  * were ISO-8859-1, the "=" character would thus be encoded as
37  * "=3D", and a SPACE by "=20". (Upper case should be used for
38  * hexadecimal digits "A" through "F".)
39  * <li>The 8-bit hexadecimal value 20 (e.g., ISO-8859-1 SPACE) may be
40  * represented as "_" (underscore, ASCII 95.). (This character may
41  * not pass through some internetwork mail gateways, but its use
42  * will greatly enhance readability of "Q" encoded data with mail
43  * readers that do not support this encoding.) Note that the "_"
44  * always represents hexadecimal 20, even if the SPACE character
45  * occupies a different code position in the character set in use.
46  * <li>8-bit values which correspond to printable ASCII characters other
47  * than "=", "?", and "_" (underscore), MAY be represented as those
48  * characters. (But see section 5 for restrictions.) In
49  * particular, SPACE and TAB MUST NOT be represented as themselves
50  * within encoded words.
51  *
52  * @author <a HREF="mailto:dog@gnu.org">Chris Burdess</a>
53  */

54 public class QOutputStream
55   extends QPOutputStream
56 {
57
58   private static final int SPACE = 32;
59   private static final int UNDERSCORE = 95;
60
61   private static String JavaDoc TEXT_SPECIALS = "=_?";
62   private static String JavaDoc WORD_SPECIALS = "=_?\"#$%&'(),.:;<>@[\\]^`{|}~";
63
64   private String JavaDoc specials;
65   
66   /**
67    * Constructor.
68    * The <code>word</code> parameter is used to indicate whether the bytes
69    * passed to this stream are considered to be RFC 822 "word" tokens or
70    * "text" tokens.
71    * @param out the underlying output stream
72    * @param word word mode if true, text mode otherwise
73    */

74   public QOutputStream(OutputStream JavaDoc out, boolean word)
75   {
76     super(out, 0x7fffffff);
77     specials = word ? WORD_SPECIALS : TEXT_SPECIALS;
78   }
79
80   /**
81    * Write a character to the stream.
82    */

83   public void write(int c)
84     throws IOException JavaDoc
85   {
86     c &= 0xff;
87     if (c==SPACE)
88       output(UNDERSCORE, false);
89     else
90     {
91       if (c<32 || c>=127 || specials.indexOf(c)>=0)
92         output(c, true);
93       else
94         output(c, false);
95     }
96   }
97
98   public static int encodedLength(byte[] bytes, boolean word)
99   {
100     int len = 0;
101     String JavaDoc specials = word ? WORD_SPECIALS : TEXT_SPECIALS;
102     for(int i = 0; i<bytes.length; i++)
103     {
104       int c = bytes[i]&0xff;
105       if (c<32 || c>=127 || specials.indexOf(c)>=0)
106         len += 3;
107       else
108         len++;
109     }
110     return len;
111   }
112
113 }
114
Popular Tags