KickJava   Java API By Example, From Geeks To Geeks.

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


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  * @(#)QDecoderStream.java 1.6 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 Decoder as defined in RFC 2047
34  * for decoding MIME headers. It subclasses the QPDecoderStream class.
35  *
36  * @author John Mani
37  */

38
39 public class QDecoderStream extends QPDecoderStream {
40
41     /**
42      * Create a Q-decoder that decodes the specified input stream.
43      * @param in the input stream
44      */

45     public QDecoderStream(InputStream in) {
46     super(in);
47     }
48
49     /**
50      * Read the next decoded byte from this input stream. The byte
51      * is returned as an <code>int</code> in the range <code>0</code>
52      * to <code>255</code>. If no byte is available because the end of
53      * the stream has been reached, the value <code>-1</code> is returned.
54      * This method blocks until input data is available, the end of the
55      * stream is detected, or an exception is thrown.
56      *
57      * @return the next byte of data, or <code>-1</code> if the end of the
58      * stream is reached.
59      * @exception IOException if an I/O error occurs.
60      */

61     public int read() throws IOException {
62     int c = in.read();
63
64     if (c == '_') // Return '_' as ' '
65
return ' ';
66     else if (c == '=') {
67         // QP Encoded atom. Get the next two bytes ..
68
ba[0] = (byte)in.read();
69         ba[1] = (byte)in.read();
70         // .. and decode them
71
try {
72         return ASCIIUtility.parseInt(ba, 0, 2, 16);
73         } catch (NumberFormatException JavaDoc nex) {
74         throw new IOException("Error in QP stream " + nex.getMessage());
75         }
76     } else
77         return c;
78     }
79 }
80
Popular Tags