1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.geronimo.mail.util;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.FilterInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.UnsupportedEncodingException;
27
28
29
30
31
32
33
34
35 public class UUDecoderStream extends FilterInputStream {
36
37 protected static final int MAX_CHARS_PER_LINE = 45;
38
39
40 protected UUEncoder decoder = new UUEncoder();
41
42
43 protected byte[] decodedChars;
44
45 protected int decodedCount = 0;
46
47 protected int decodedIndex = 0;
48
49
50 protected boolean beginRead = false;
51
52
53 public UUDecoderStream(InputStream in) {
54 super(in);
55 }
56
57
58
59
60
61
62
63
64 private boolean dataAvailable() {
65 return decodedCount != 0;
66 }
67
68
69
70
71
72
73 private byte getBufferedChar() {
74 decodedCount--;
75 return decodedChars[decodedIndex++];
76 }
77
78
79
80
81
82
83 private boolean decodeStreamData() throws IOException {
84 decodedIndex = 0;
85
86
87 return fillEncodedBuffer() != -1;
88 }
89
90
91
92
93
94
95
96 private int getByte() throws IOException {
97 if (!dataAvailable()) {
98 if (!decodeStreamData()) {
99 return -1;
100 }
101 }
102 decodedCount--;
103 return decodedChars[decodedIndex++];
104 }
105
106 private int getBytes(byte[] data, int offset, int length) throws IOException {
107
108 int readCharacters = 0;
109 while (length > 0) {
110
111 if (!dataAvailable()) {
112
113 if (!decodeStreamData()) {
114 return readCharacters > 0 ? readCharacters : -1;
115 }
116 }
117
118
119 int copyCount = Math.min(decodedCount, length);
120 System.arraycopy(decodedChars, decodedIndex, data, offset, copyCount);
121 decodedIndex += copyCount;
122 decodedCount -= copyCount;
123 offset += copyCount;
124 length -= copyCount;
125 readCharacters += copyCount;
126 }
127 return readCharacters;
128 }
129
130
131
132
133
134
135
136 private void checkBegin() throws IOException {
137
138 if (beginRead) {
139 return;
140 }
141
142
143
144 while (true) {
145 String line = readLine();
146 if (line == null) {
147 throw new IOException("Missing UUEncode begin command");
148 }
149
150
151 if (line.regionMatches(true, 0, "begin ", 0, 6)) {
152
153 beginRead = true;
154 return;
155 }
156 }
157 }
158
159
160
161
162
163
164
165
166
167 protected String readLine() throws IOException {
168 decodedIndex = 0;
169
170 StringBuffer buffer = new StringBuffer();
171
172
173 int ch = in.read();
174 while (ch != -1) {
175
176 if (ch == '\n') {
177 break;
178 }
179
180
181 else if (ch == '\r') {
182 ;
183 }
184 else {
185
186 buffer.append((char)ch);
187 }
188 ch = in.read();
189 }
190
191
192 if (ch == -1 && buffer.length() == 0) {
193 return null;
194 }
195
196 return buffer.toString();
197 }
198
199
200
201
202
203
204
205
206
207
208
209 private int fillEncodedBuffer() throws IOException
210 {
211 checkBegin();
212
213 decodedIndex = 0;
214
215 while (true) {
216
217
218
219 String line = readLine();
220
221
222 if (line == null) {
223 throw new IOException("Missing end in UUEncoded data");
224 }
225
226
227 if (line.equalsIgnoreCase("end")) {
228
229 return -1;
230 }
231
232 ByteArrayOutputStream out = new ByteArrayOutputStream(MAX_CHARS_PER_LINE);
233
234 byte [] lineBytes;
235 try {
236 lineBytes = line.getBytes("US-ASCII");
237 } catch (UnsupportedEncodingException e) {
238 throw new IOException("Invalid UUEncoding");
239 }
240
241
242 decodedCount = decoder.decode(lineBytes, 0, lineBytes.length, out);
243
244
245 if (decodedCount != 0) {
246
247 decodedChars = out.toByteArray();
248 return decodedCount;
249 }
250 }
251 }
252
253
254
255
256
257 public int read() throws IOException
258 {
259 return getByte();
260 }
261
262
263 public int read(byte [] buffer, int offset, int length) throws IOException {
264 return getBytes(buffer, offset, length);
265 }
266
267
268 public boolean markSupported() {
269 return false;
270 }
271
272
273 public int available() throws IOException {
274 return ((in.available() / 4) * 3) + decodedCount;
275 }
276 }
277