|
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.IOException; |
|
22 |
| import java.io.OutputStream; |
|
23 |
| import java.io.UnsupportedEncodingException; |
|
24 |
| |
|
25 |
| import javax.mail.internet.MimeUtility; |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| public class RFC2231Encoder implements Encoder |
|
48 |
| { |
|
49 |
| protected final byte[] encodingTable = |
|
50 |
| { |
|
51 |
| (byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7', |
|
52 |
| (byte)'8', (byte)'9', (byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F' |
|
53 |
| }; |
|
54 |
| |
|
55 |
| protected String DEFAULT_SPECIALS = " *'%"; |
|
56 |
| protected String specials = DEFAULT_SPECIALS; |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| protected final byte[] decodingTable = new byte[128]; |
|
62 |
| |
|
63 |
2
| protected void initialiseDecodingTable()
|
|
64 |
| { |
|
65 |
2
| for (int i = 0; i < encodingTable.length; i++)
|
|
66 |
| { |
|
67 |
32
| decodingTable[encodingTable[i]] = (byte)i;
|
|
68 |
| } |
|
69 |
| } |
|
70 |
| |
|
71 |
0
| public RFC2231Encoder()
|
|
72 |
| { |
|
73 |
0
| this(null);
|
|
74 |
| } |
|
75 |
| |
|
76 |
2
| public RFC2231Encoder(String specials)
|
|
77 |
| { |
|
78 |
2
| if (specials != null) {
|
|
79 |
2
| this.specials = DEFAULT_SPECIALS + specials;
|
|
80 |
| } |
|
81 |
2
| initialiseDecodingTable();
|
|
82 |
| } |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
1
| public int encode(byte[] data, int off, int length, OutputStream out) throws IOException {
|
|
91 |
| |
|
92 |
1
| int bytesWritten = 0;
|
|
93 |
1
| for (int i = off; i < (off + length); i++)
|
|
94 |
| { |
|
95 |
16
| int ch = data[i] & 0xff;
|
|
96 |
| |
|
97 |
16
| if (ch <= 32 || ch >= 127 || specials.indexOf(ch) != -1) {
|
|
98 |
13
| out.write((byte)'%');
|
|
99 |
13
| out.write(encodingTable[ch >> 4]);
|
|
100 |
13
| out.write(encodingTable[ch & 0xf]);
|
|
101 |
13
| bytesWritten += 3;
|
|
102 |
| } |
|
103 |
| else { |
|
104 |
| |
|
105 |
3
| out.write((byte)ch);
|
|
106 |
3
| bytesWritten++;
|
|
107 |
| } |
|
108 |
| } |
|
109 |
| |
|
110 |
1
| return bytesWritten;
|
|
111 |
| } |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
0
| public int decode(byte[] data, int off, int length, OutputStream out) throws IOException {
|
|
120 |
0
| int outLen = 0;
|
|
121 |
0
| int end = off + length;
|
|
122 |
| |
|
123 |
0
| int i = off;
|
|
124 |
0
| while (i < end)
|
|
125 |
| { |
|
126 |
0
| byte v = data[i++];
|
|
127 |
| |
|
128 |
0
| if (v == '%') {
|
|
129 |
0
| byte b1 = decodingTable[data[i++]];
|
|
130 |
0
| byte b2 = decodingTable[data[i++]];
|
|
131 |
0
| out.write((b1 << 4) | b2);
|
|
132 |
| } |
|
133 |
| else { |
|
134 |
| |
|
135 |
0
| out.write(v);
|
|
136 |
| } |
|
137 |
| |
|
138 |
0
| outLen++;
|
|
139 |
| } |
|
140 |
| |
|
141 |
0
| return outLen;
|
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
1
| public int decode(String data, OutputStream out) throws IOException
|
|
150 |
| { |
|
151 |
1
| int length = 0;
|
|
152 |
1
| int end = data.length();
|
|
153 |
| |
|
154 |
1
| int i = 0;
|
|
155 |
1
| while (i < end)
|
|
156 |
| { |
|
157 |
16
| char v = data.charAt(i++);
|
|
158 |
16
| if (v == '%') {
|
|
159 |
13
| byte b1 = decodingTable[data.charAt(i++)];
|
|
160 |
13
| byte b2 = decodingTable[data.charAt(i++)];
|
|
161 |
| |
|
162 |
13
| out.write((b1 << 4) | b2);
|
|
163 |
| } |
|
164 |
| else { |
|
165 |
3
| out.write((byte)v);
|
|
166 |
| } |
|
167 |
16
| length++;
|
|
168 |
| } |
|
169 |
| |
|
170 |
1
| return length;
|
|
171 |
| } |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
0
| public String encode(String charset, String language, String data) throws IOException {
|
|
185 |
| |
|
186 |
0
| byte[] bytes = null;
|
|
187 |
0
| try {
|
|
188 |
| |
|
189 |
| |
|
190 |
0
| bytes = data.getBytes(MimeUtility.javaCharset(charset));
|
|
191 |
| } catch (UnsupportedEncodingException e) { |
|
192 |
| |
|
193 |
0
| return null;
|
|
194 |
| } |
|
195 |
| |
|
196 |
0
| StringBuffer result = new StringBuffer();
|
|
197 |
| |
|
198 |
| |
|
199 |
0
| if (charset != null) {
|
|
200 |
0
| result.append(charset);
|
|
201 |
| } |
|
202 |
| |
|
203 |
0
| result.append("'");
|
|
204 |
| |
|
205 |
| |
|
206 |
0
| if (language != null) {
|
|
207 |
0
| result.append(language);
|
|
208 |
| } |
|
209 |
| |
|
210 |
0
| result.append("'");
|
|
211 |
| |
|
212 |
| |
|
213 |
0
| OutputStream out = new StringBufferOutputStream(result);
|
|
214 |
| |
|
215 |
| |
|
216 |
0
| encode(bytes, 0, bytes.length, out);
|
|
217 |
| |
|
218 |
| |
|
219 |
0
| return result.toString();
|
|
220 |
| } |
|
221 |
| |
|
222 |
| |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
1
| public String decode(String data) throws IOException, UnsupportedEncodingException {
|
|
233 |
| |
|
234 |
1
| int charsetEnd = data.indexOf('\'');
|
|
235 |
| |
|
236 |
1
| if (charsetEnd == -1) {
|
|
237 |
0
| throw new IOException("Missing charset in RFC2231 encoded value");
|
|
238 |
| } |
|
239 |
| |
|
240 |
1
| String charset = data.substring(0, charsetEnd);
|
|
241 |
| |
|
242 |
| |
|
243 |
1
| int languageEnd = data.indexOf('\'', charsetEnd + 1);
|
|
244 |
1
| if (languageEnd == -1) {
|
|
245 |
0
| throw new IOException("Missing language in RFC2231 encoded value");
|
|
246 |
| } |
|
247 |
| |
|
248 |
1
| String language = data.substring(charsetEnd + 1, languageEnd);
|
|
249 |
| |
|
250 |
1
| ByteArrayOutputStream out = new ByteArrayOutputStream(data.length());
|
|
251 |
| |
|
252 |
| |
|
253 |
1
| decode(data.substring(languageEnd + 1), out);
|
|
254 |
| |
|
255 |
1
| byte[] bytes = out.toByteArray();
|
|
256 |
| |
|
257 |
1
| return new String(bytes, 0, bytes.length, MimeUtility.javaCharset(charset));
|
|
258 |
| } |
|
259 |
| } |