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.IOException; |
21 |
| import java.io.OutputStream; |
22 |
| import java.io.UnsupportedEncodingException; |
23 |
| |
24 |
| public class UUEncoder implements Encoder { |
25 |
| |
26 |
| |
27 |
| |
28 |
| static private final int MAX_CHARS_PER_LINE = 45; |
29 |
| |
30 |
| |
31 |
10
| public UUEncoder()
|
32 |
| { |
33 |
| } |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
15
| public int encode(byte[] data, int off, int length, OutputStream out) throws IOException
|
46 |
| { |
47 |
15
| int byteCount = 0;
|
48 |
| |
49 |
15
| while (true) {
|
50 |
| |
51 |
30
| if (length > MAX_CHARS_PER_LINE) {
|
52 |
| |
53 |
15
| byteCount += encodeLine(data, off, MAX_CHARS_PER_LINE, out);
|
54 |
15
| length -= MAX_CHARS_PER_LINE;
|
55 |
15
| off += MAX_CHARS_PER_LINE;
|
56 |
| } |
57 |
| else { |
58 |
| |
59 |
15
| byteCount += encodeLine(data, off, MAX_CHARS_PER_LINE, out);
|
60 |
15
| break;
|
61 |
| } |
62 |
| } |
63 |
15
| return byteCount;
|
64 |
| } |
65 |
| |
66 |
| |
67 |
| |
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
30
| private int encodeLine(byte[] data, int offset, int length, OutputStream out) throws IOException {
|
79 |
| |
80 |
30
| out.write((byte)((length & 0x3F) + ' '));
|
81 |
30
| byte a;
|
82 |
30
| byte b;
|
83 |
30
| byte c;
|
84 |
| |
85 |
| |
86 |
30
| int bytesWritten = 2;
|
87 |
| |
88 |
30
| for (int i = 0; i < length;) {
|
89 |
| |
90 |
450
| b = 1;
|
91 |
450
| c = 1;
|
92 |
| |
93 |
450
| a = data[offset + i++];
|
94 |
450
| if (i < length) {
|
95 |
450
| b = data[offset + i++];
|
96 |
450
| if (i < length) {
|
97 |
450
| c = data[offset + i++];
|
98 |
| } |
99 |
| } |
100 |
| |
101 |
450
| byte d1 = (byte)(((a >>> 2) & 0x3F) + ' ');
|
102 |
450
| byte d2 = (byte)(((( a << 4) & 0x30) | ((b >>> 4) & 0x0F)) + ' ');
|
103 |
450
| byte d3 = (byte)((((b << 2) & 0x3C) | ((c >>> 6) & 0x3)) + ' ');
|
104 |
450
| byte d4 = (byte)((c & 0x3F) + ' ');
|
105 |
| |
106 |
450
| out.write(d1);
|
107 |
450
| out.write(d2);
|
108 |
450
| out.write(d3);
|
109 |
450
| out.write(d4);
|
110 |
| |
111 |
450
| bytesWritten += 4;
|
112 |
| } |
113 |
| |
114 |
| |
115 |
30
| out.write('\n');
|
116 |
| |
117 |
30
| return bytesWritten;
|
118 |
| } |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
30
| public int decode(byte[] data, int off, int length, OutputStream out) throws IOException
|
133 |
| { |
134 |
30
| int bytesWritten = 0;
|
135 |
| |
136 |
30
| while (length > 0) {
|
137 |
30
| int lineOffset = off;
|
138 |
| |
139 |
| |
140 |
30
| while (length > 0 && data[off] != '\n') {
|
141 |
1830
| off++;
|
142 |
1830
| length--;
|
143 |
| } |
144 |
| |
145 |
| |
146 |
30
| bytesWritten += decodeLine(data, lineOffset, off - lineOffset, out);
|
147 |
| |
148 |
| |
149 |
| |
150 |
30
| off++;
|
151 |
30
| length--;
|
152 |
| } |
153 |
30
| return bytesWritten;
|
154 |
| } |
155 |
| |
156 |
| |
157 |
| |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
| |
168 |
30
| private int decodeLine(byte[] data, int off, int length, OutputStream out) throws IOException {
|
169 |
30
| int count = data[off++];
|
170 |
| |
171 |
| |
172 |
30
| if (count < ' ') {
|
173 |
0
| throw new IOException("Invalid UUEncode line length");
|
174 |
| } |
175 |
| |
176 |
30
| count = (count - ' ') & 0x3F;
|
177 |
| |
178 |
| |
179 |
| |
180 |
30
| int requiredLength = (((count * 8) + 5) / 6) + 1;
|
181 |
30
| if (length < requiredLength) {
|
182 |
0
| throw new IOException("UUEncoded data and length do not match");
|
183 |
| } |
184 |
| |
185 |
30
| int bytesWritten = 0;
|
186 |
| |
187 |
30
| while (bytesWritten < count) {
|
188 |
| |
189 |
450
| byte a = (byte)((data[off++] - ' ') & 0x3F);
|
190 |
450
| byte b = (byte)((data[off++] - ' ') & 0x3F);
|
191 |
450
| byte c = 0;
|
192 |
450
| byte d = 0;
|
193 |
| |
194 |
| |
195 |
450
| byte first = (byte)(((a << 2) & 0xFC) | ((b >>> 4) & 3));
|
196 |
450
| out.write(first);
|
197 |
450
| bytesWritten++;
|
198 |
| |
199 |
| |
200 |
| |
201 |
450
| if (bytesWritten < count) {
|
202 |
450
| c = (byte)((data[off++] - ' ') & 0x3F);
|
203 |
450
| byte second = (byte)(((b << 4) & 0xF0) | ((c >>> 2) & 0x0F));
|
204 |
450
| out.write(second);
|
205 |
450
| bytesWritten++;
|
206 |
| |
207 |
| |
208 |
450
| if (bytesWritten < count) {
|
209 |
450
| d = (byte)((data[off++] - ' ') & 0x3F);
|
210 |
450
| byte third = (byte)(((c << 6) & 0xC0) | (d & 0x3F));
|
211 |
450
| out.write(third);
|
212 |
450
| bytesWritten++;
|
213 |
| } |
214 |
| } |
215 |
| } |
216 |
30
| return bytesWritten;
|
217 |
| } |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
| |
225 |
| |
226 |
| |
227 |
| |
228 |
| |
229 |
0
| public int decode(String data, OutputStream out) throws IOException
|
230 |
| { |
231 |
0
| try {
|
232 |
| |
233 |
0
| byte[] bytes = data.getBytes("US-ASCII");
|
234 |
0
| return decode(bytes, 0, bytes.length, out);
|
235 |
| } catch (UnsupportedEncodingException e) { |
236 |
0
| throw new IOException("Invalid UUEncoding");
|
237 |
| } |
238 |
| } |
239 |
| } |
240 |
| |
241 |
| |