Skip to content

Commit 5dae4d9

Browse files
committed
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1306437 13f79535-47bb-0310-9956-ffa450edef68
1 parent 4f7912d commit 5dae4d9

2 files changed

Lines changed: 80 additions & 37 deletions

File tree

src/main/java/org/apache/commons/codec/net/QuotedPrintableCodec.java

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
import java.io.ByteArrayOutputStream;
2121
import java.io.UnsupportedEncodingException;
22+
import java.nio.charset.Charset;
23+
import java.nio.charset.UnsupportedCharsetException;
2224
import java.util.BitSet;
2325

2426
import org.apache.commons.codec.BinaryDecoder;
2527
import org.apache.commons.codec.BinaryEncoder;
2628
import org.apache.commons.codec.CharEncoding;
29+
import org.apache.commons.codec.Charsets;
2730
import org.apache.commons.codec.DecoderException;
2831
import org.apache.commons.codec.EncoderException;
2932
import org.apache.commons.codec.StringDecoder;
@@ -64,7 +67,7 @@ public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, Strin
6467
/**
6568
* The default charset used for string decoding and encoding.
6669
*/
67-
private final String charset;
70+
private final Charset charset;
6871

6972
/**
7073
* BitSet of printable characters as defined in RFC 1521.
@@ -93,20 +96,35 @@ public class QuotedPrintableCodec implements BinaryEncoder, BinaryDecoder, Strin
9396
* Default constructor.
9497
*/
9598
public QuotedPrintableCodec() {
96-
this(CharEncoding.UTF_8);
99+
this(Charsets.UTF_8);
97100
}
98101

99102
/**
100103
* Constructor which allows for the selection of a default charset
101104
*
102105
* @param charset
103106
* 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
104110
*/
105-
public QuotedPrintableCodec(String charset) {
106-
super();
111+
public QuotedPrintableCodec(Charset charset) {
107112
this.charset = charset;
108113
}
109114

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+
110128
/**
111129
* Encodes byte into its quoted-printable representation.
112130
*
@@ -246,17 +264,29 @@ public byte[] decode(byte[] bytes) throws DecoderException {
246264
* @throws EncoderException
247265
* Thrown if quoted-printable encoding is unsuccessful
248266
*
249-
* @see #getDefaultCharset()
267+
* @see #getCharset()
250268
*/
251269
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 {
252286
if (pString == null) {
253287
return null;
254288
}
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);
260290
}
261291

262292
/**
@@ -290,17 +320,10 @@ public String decode(String pString, String charset) throws DecoderException, Un
290320
* @throws DecoderException
291321
* Thrown if quoted-printable decoding is unsuccessful.
292322
* Thrown if charset is not supported.
293-
* @see #getDefaultCharset()
323+
* @see #getCharset()
294324
*/
295325
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());
304327
}
305328

306329
/**
@@ -353,14 +376,45 @@ public Object decode(Object pObject) throws DecoderException {
353376
}
354377

355378
/**
356-
* Returns the default charset used for string decoding and encoding.
379+
* Gets the default charset name used for string decoding and encoding.
357380
*
358-
* @return the default string charset.
381+
* @return the default charset name
382+
* @since 1.7
359383
*/
360-
public String getDefaultCharset() {
384+
public Charset getCharset() {
361385
return this.charset;
362386
}
363387

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+
364418
/**
365419
* Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped.
366420
*

src/test/java/org/apache/commons/codec/net/QuotedPrintableCodecTest.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import static org.junit.Assert.assertNull;
2222
import static org.junit.Assert.fail;
2323

24+
import java.nio.charset.UnsupportedCharsetException;
25+
2426
import org.apache.commons.codec.CharEncoding;
2527
import org.apache.commons.codec.DecoderException;
2628
import org.apache.commons.codec.EncoderException;
@@ -209,22 +211,9 @@ public void testEncodeObjects() throws Exception {
209211
}
210212
}
211213

212-
@Test
214+
@Test(expected=UnsupportedCharsetException.class)
213215
public void testInvalidEncoding() {
214-
QuotedPrintableCodec qpcodec = new QuotedPrintableCodec("NONSENSE");
215-
String plain = "Hello there!";
216-
try {
217-
qpcodec.encode(plain);
218-
fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
219-
} catch (EncoderException ee) {
220-
// Exception expected, test segment passes.
221-
}
222-
try {
223-
qpcodec.decode(plain);
224-
fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
225-
} catch (DecoderException ee) {
226-
// Exception expected, test segment passes.
227-
}
216+
new QuotedPrintableCodec("NONSENSE");
228217
}
229218

230219
@Test

0 commit comments

Comments
 (0)