1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.codec.net;
19
20 import java.io.UnsupportedEncodingException;
21 import java.nio.charset.Charset;
22 import java.nio.charset.StandardCharsets;
23 import java.nio.charset.UnsupportedCharsetException;
24 import java.util.BitSet;
25
26 import org.apache.commons.codec.DecoderException;
27 import org.apache.commons.codec.EncoderException;
28 import org.apache.commons.codec.StringDecoder;
29 import org.apache.commons.codec.StringEncoder;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54 public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder {
55
56
57
58 private static final BitSet PRINTABLE_CHARS = new BitSet(256);
59
60
61 static {
62
63 PRINTABLE_CHARS.set(' ');
64 PRINTABLE_CHARS.set('!');
65 PRINTABLE_CHARS.set('"');
66 PRINTABLE_CHARS.set('#');
67 PRINTABLE_CHARS.set('$');
68 PRINTABLE_CHARS.set('%');
69 PRINTABLE_CHARS.set('&');
70 PRINTABLE_CHARS.set('\'');
71 PRINTABLE_CHARS.set('(');
72 PRINTABLE_CHARS.set(')');
73 PRINTABLE_CHARS.set('*');
74 PRINTABLE_CHARS.set('+');
75 PRINTABLE_CHARS.set(',');
76 PRINTABLE_CHARS.set('-');
77 PRINTABLE_CHARS.set('.');
78 PRINTABLE_CHARS.set('/');
79 for (int i = '0'; i <= '9'; i++) {
80 PRINTABLE_CHARS.set(i);
81 }
82 PRINTABLE_CHARS.set(':');
83 PRINTABLE_CHARS.set(';');
84 PRINTABLE_CHARS.set('<');
85 PRINTABLE_CHARS.set('>');
86 PRINTABLE_CHARS.set('@');
87 for (int i = 'A'; i <= 'Z'; i++) {
88 PRINTABLE_CHARS.set(i);
89 }
90 PRINTABLE_CHARS.set('[');
91 PRINTABLE_CHARS.set('\\');
92 PRINTABLE_CHARS.set(']');
93 PRINTABLE_CHARS.set('^');
94 PRINTABLE_CHARS.set('`');
95 for (int i = 'a'; i <= 'z'; i++) {
96 PRINTABLE_CHARS.set(i);
97 }
98 PRINTABLE_CHARS.set('{');
99 PRINTABLE_CHARS.set('|');
100 PRINTABLE_CHARS.set('}');
101 PRINTABLE_CHARS.set('~');
102 }
103 private static final byte SPACE = 32;
104
105 private static final byte UNDERSCORE = 95;
106
107 private boolean encodeBlanks;
108
109
110
111
112 public QCodec() {
113 this(StandardCharsets.UTF_8);
114 }
115
116
117
118
119
120
121
122
123
124
125 public QCodec(final Charset charset) {
126 super(charset);
127 }
128
129
130
131
132
133
134
135
136
137
138
139 public QCodec(final String charsetName) {
140 this(Charset.forName(charsetName));
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154 @Override
155 public Object decode(final Object obj) throws DecoderException {
156 if (obj == null) {
157 return null;
158 }
159 if (obj instanceof String) {
160 return decode((String) obj);
161 }
162 throw new DecoderException("Objects of type " + obj.getClass().getName() + " cannot be decoded using Q codec");
163 }
164
165
166
167
168
169
170
171
172
173
174
175 @Override
176 public String decode(final String str) throws DecoderException {
177 try {
178 return decodeText(str);
179 } catch (final UnsupportedEncodingException e) {
180 throw new DecoderException(e.getMessage(), e);
181 }
182 }
183
184 @Override
185 protected byte[] doDecoding(final byte[] bytes) throws DecoderException {
186 if (bytes == null) {
187 return null;
188 }
189 boolean hasUnderscores = false;
190 for (final byte b : bytes) {
191 if (b == UNDERSCORE) {
192 hasUnderscores = true;
193 break;
194 }
195 }
196 if (hasUnderscores) {
197 final byte[] tmp = new byte[bytes.length];
198 for (int i = 0; i < bytes.length; i++) {
199 final byte b = bytes[i];
200 if (b != UNDERSCORE) {
201 tmp[i] = b;
202 } else {
203 tmp[i] = SPACE;
204 }
205 }
206 return QuotedPrintableCodec.decodeQuotedPrintable(tmp);
207 }
208 return QuotedPrintableCodec.decodeQuotedPrintable(bytes);
209 }
210
211 @Override
212 protected byte[] doEncoding(final byte[] bytes) {
213 if (bytes == null) {
214 return null;
215 }
216 final byte[] data = QuotedPrintableCodec.encodeQuotedPrintable(PRINTABLE_CHARS, bytes);
217 if (this.encodeBlanks) {
218 for (int i = 0; i < data.length; i++) {
219 if (data[i] == SPACE) {
220 data[i] = UNDERSCORE;
221 }
222 }
223 }
224 return data;
225 }
226
227
228
229
230
231
232
233
234
235
236 @Override
237 public Object encode(final Object obj) throws EncoderException {
238 if (obj == null) {
239 return null;
240 }
241 if (obj instanceof String) {
242 return encode((String) obj);
243 }
244 throw new EncoderException("Objects of type " + obj.getClass().getName() + " cannot be encoded using Q codec");
245 }
246
247
248
249
250
251
252
253
254
255
256 @Override
257 public String encode(final String sourceStr) throws EncoderException {
258 return encode(sourceStr, getCharset());
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272
273 public String encode(final String sourceStr, final Charset sourceCharset) throws EncoderException {
274 return encodeText(sourceStr, sourceCharset);
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288 public String encode(final String sourceStr, final String sourceCharset) throws EncoderException {
289 try {
290 return encodeText(sourceStr, sourceCharset);
291 } catch (final UnsupportedCharsetException e) {
292 throw new EncoderException(e.getMessage(), e);
293 }
294 }
295
296 @Override
297 protected String getEncoding() {
298 return "Q";
299 }
300
301
302
303
304
305
306 public boolean isEncodeBlanks() {
307 return this.encodeBlanks;
308 }
309
310
311
312
313
314
315
316 public void setEncodeBlanks(final boolean b) {
317 this.encodeBlanks = b;
318 }
319 }