|
19 | 19 |
|
20 | 20 | import java.io.ByteArrayOutputStream; |
21 | 21 | import java.io.UnsupportedEncodingException; |
| 22 | +import java.nio.charset.Charset; |
| 23 | +import java.nio.charset.UnsupportedCharsetException; |
22 | 24 | import java.util.BitSet; |
23 | 25 |
|
24 | 26 | import org.apache.commons.codec.BinaryDecoder; |
25 | 27 | import org.apache.commons.codec.BinaryEncoder; |
26 | 28 | import org.apache.commons.codec.CharEncoding; |
| 29 | +import org.apache.commons.codec.Charsets; |
27 | 30 | import org.apache.commons.codec.DecoderException; |
28 | 31 | import org.apache.commons.codec.EncoderException; |
29 | 32 | import org.apache.commons.codec.StringDecoder; |
@@ -64,7 +67,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, Strin |
64 | 67 | /** |
65 | 68 | * The default charset used for string decoding and encoding. |
66 | 69 | */ |
67 | | - private final String charset; |
| 70 | + private final Charset charset; |
68 | 71 |
|
69 | 72 | /** |
70 | 73 | * BitSet of printable characters as defined in RFC 1521. |
@@ -93,20 +96,35 @@ public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, Strin |
93 | 96 | * Default constructor. |
94 | 97 | */ |
95 | 98 | public QuotedPrintableCodec() { |
96 | | - this(CharEncoding.UTF_8); |
| 99 | + this(Charsets.UTF_8); |
97 | 100 | } |
98 | 101 |
|
99 | 102 | /** |
100 | 103 | * Constructor which allows for the selection of a default charset |
101 | 104 | * |
102 | 105 | * @param charset |
103 | 106 | * the default string charset to use. |
| 107 | + * @throws UnsupportedCharsetException |
| 108 | + * If the named charset is unavailable |
| 109 | + * @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable |
104 | 110 | */ |
105 | | - public QuotedPrintableCodec(String charset) { |
106 | | - super(); |
| 111 | + public QuotedPrintableCodec(Charset charset) { |
107 | 112 | this.charset = charset; |
108 | 113 | } |
109 | 114 |
|
| 115 | + /** |
| 116 | + * Constructor which allows for the selection of a default charset |
| 117 | + * |
| 118 | + * @param charsetName |
| 119 | + * the default string charset to use. |
| 120 | + * @throws UnsupportedCharsetException |
| 121 | + * If the named charset is unavailable |
| 122 | + * @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable |
| 123 | + */ |
| 124 | + public QuotedPrintableCodec(String charsetName) { |
| 125 | + this(Charset.forName(charsetName)); |
| 126 | + } |
| 127 | + |
110 | 128 | /** |
111 | 129 | * Encodes byte into its quoted-printable representation. |
112 | 130 | * |
@@ -246,17 +264,29 @@ public byte[] decode(byte[] bytes) throws DecoderException { |
246 | 264 | * @throws EncoderException |
247 | 265 | * Thrown if quoted-printable encoding is unsuccessful |
248 | 266 | * |
249 | | - * @see #getDefaultCharset() |
| 267 | + * @see #getCharset() |
250 | 268 | */ |
251 | 269 | public String encode(String pString) throws EncoderException { |
| 270 | + return this.encode(pString, getCharset()); |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * Decodes a quoted-printable string into its original form using the specified string charset. Escaped characters |
| 275 | + * are converted back to their original representation. |
| 276 | + * |
| 277 | + * @param pString |
| 278 | + * quoted-printable string to convert into its original form |
| 279 | + * @param charset |
| 280 | + * the original string charset |
| 281 | + * @return original string |
| 282 | + * @throws DecoderException |
| 283 | + * Thrown if quoted-printable decoding is unsuccessful |
| 284 | + */ |
| 285 | + public String decode(String pString, Charset charset) throws DecoderException { |
252 | 286 | if (pString == null) { |
253 | 287 | return null; |
254 | 288 | } |
255 | | - try { |
256 | | - return encode(pString, getDefaultCharset()); |
257 | | - } catch (UnsupportedEncodingException e) { |
258 | | - throw new EncoderException(e.getMessage(), e); |
259 | | - } |
| 289 | + return new String(this.decode(StringUtils.getBytesUsAscii(pString)), charset); |
260 | 290 | } |
261 | 291 |
|
262 | 292 | /** |
@@ -290,17 +320,10 @@ public String decode(String pString, String charset) throws DecoderException, Un |
290 | 320 | * @throws DecoderException |
291 | 321 | * Thrown if quoted-printable decoding is unsuccessful. |
292 | 322 | * Thrown if charset is not supported. |
293 | | - * @see #getDefaultCharset() |
| 323 | + * @see #getCharset() |
294 | 324 | */ |
295 | 325 | public String decode(String pString) throws DecoderException { |
296 | | - if (pString == null) { |
297 | | - return null; |
298 | | - } |
299 | | - try { |
300 | | - return decode(pString, getDefaultCharset()); |
301 | | - } catch (UnsupportedEncodingException e) { |
302 | | - throw new DecoderException(e.getMessage(), e); |
303 | | - } |
| 326 | + return this.decode(pString, this.getCharset()); |
304 | 327 | } |
305 | 328 |
|
306 | 329 | /** |
@@ -353,14 +376,45 @@ public Object decode(Object pObject) throws DecoderException { |
353 | 376 | } |
354 | 377 |
|
355 | 378 | /** |
356 | | - * Returns the default charset used for string decoding and encoding. |
| 379 | + * Gets the default charset name used for string decoding and encoding. |
357 | 380 | * |
358 | | - * @return the default string charset. |
| 381 | + * @return the default charset name |
| 382 | + * @since 1.7 |
359 | 383 | */ |
360 | | - public String getDefaultCharset() { |
| 384 | + public Charset getCharset() { |
361 | 385 | return this.charset; |
362 | 386 | } |
363 | 387 |
|
| 388 | + /** |
| 389 | + * Gets the default charset name used for string decoding and encoding. |
| 390 | + * |
| 391 | + * @return the default charset name |
| 392 | + */ |
| 393 | + public String getDefaultCharset() { |
| 394 | + return this.charset.name(); |
| 395 | + } |
| 396 | + |
| 397 | + /** |
| 398 | + * Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped. |
| 399 | + * |
| 400 | + * <p> |
| 401 | + * This function implements a subset of quoted-printable encoding specification (rule #1 and rule #2) as defined in |
| 402 | + * RFC 1521 and is suitable for encoding binary data and unformatted text. |
| 403 | + * </p> |
| 404 | + * |
| 405 | + * @param pString |
| 406 | + * string to convert to quoted-printable form |
| 407 | + * @param charset |
| 408 | + * the charset for pString |
| 409 | + * @return quoted-printable string |
| 410 | + */ |
| 411 | + public String encode(String pString, Charset charset) { |
| 412 | + if (pString == null) { |
| 413 | + return null; |
| 414 | + } |
| 415 | + return StringUtils.newStringUsAscii(this.encode(pString.getBytes(charset))); |
| 416 | + } |
| 417 | + |
364 | 418 | /** |
365 | 419 | * Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped. |
366 | 420 | * |
|
0 commit comments