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