KickJava   Java API By Example, From Geeks To Geeks.

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


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

53 public class QInputStream
54   extends QPInputStream
55 {
56
57   private static final int SPACE = 32;
58   private static final int EQ = 61;
59   private static final int UNDERSCORE = 95;
60
61   /**
62    * Constructor.
63    * @param in the underlying input stream.
64    */

65   public QInputStream(InputStream in)
66   {
67     super(in);
68   }
69
70   /**
71    * Read a character.
72    */

73   public int read()
74     throws IOException
75   {
76     int c = in.read();
77     if (c==UNDERSCORE)
78       return SPACE;
79     if (c==EQ)
80     {
81       buf[0] = (byte)in.read();
82       buf[1] = (byte)in.read();
83       try
84       {
85         return Integer.parseInt(new String JavaDoc(buf, 0, 2), 16);
86       }
87       catch (NumberFormatException JavaDoc e)
88       {
89         throw new IOException("Quoted-Printable encoding error: "+
90             e.getMessage());
91       }
92     }
93     return c;
94   }
95
96 }
97
Popular Tags