1 /**
2 *
3 * Copyright 2003-2006 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.geronimo.mail.util;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.FilterInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.UnsupportedEncodingException;
25
26 /**
27 * An implementation of a FilterOutputStream that decodes the
28 * stream data in Q-P encoding format. This version does the
29 * decoding "on the fly" rather than decoding a single block of
30 * data. Since this version is intended for use by the MimeUtilty class,
31 * it also handles line breaks in the encoded data.
32 */
33 public class QuotedPrintableDecoderStream extends FilterInputStream {
34
35 protected QuotedPrintableEncoder decoder;
36
37
38 /**
39 * Stream constructor.
40 *
41 * @param in The InputStream this stream is filtering.
42 */
43 public QuotedPrintableDecoderStream(InputStream in) {
44 super(in);
45 decoder = new QuotedPrintableEncoder();
46 }
47
48
49
50
51
52 /**
53 * Read a single byte from the stream.
54 *
55 * @return The next byte of the stream. Returns -1 for an EOF condition.
56 * @exception IOException
57 */
58 public int read() throws IOException
59 {
60
61 return decoder.decode(in);
62 }
63
64
65 /**
66 * Read a buffer of data from the input stream.
67 *
68 * @param buffer The target byte array the data is placed into.
69 * @param offset The starting offset for the read data.
70 * @param length How much data is requested.
71 *
72 * @return The number of bytes of data read.
73 * @exception IOException
74 */
75 public int read(byte [] buffer, int offset, int length) throws IOException {
76
77 for (int i = 0; i < length; i++) {
78 int ch = decoder.decode(in);
79 if (ch == -1) {
80 return i;
81 }
82 buffer[offset + i] = (byte)ch;
83 }
84
85 return length;
86 }
87
88
89 /**
90 * Indicate whether this stream supports the mark() operation.
91 *
92 * @return Always returns false.
93 */
94 public boolean markSupported() {
95 return false;
96 }
97
98
99 /**
100 * Give an estimate of how much additional data is available
101 * from this stream.
102 *
103 * @return Always returns -1.
104 * @exception IOException
105 */
106 public int available() throws IOException {
107
108 return -1;
109 }
110 }
111
112