Skip to content

Commit 7bf19f9

Browse files
committed
[CODEC-136] Use Charset objects when possible, create Charsets for required character encodings.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1306412 13f79535-47bb-0310-9956-ffa450edef68
1 parent 90caed9 commit 7bf19f9

5 files changed

Lines changed: 146 additions & 52 deletions

File tree

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

Lines changed: 55 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package org.apache.commons.codec.net;
1919

2020
import java.io.UnsupportedEncodingException;
21+
import java.nio.charset.Charset;
22+
import java.nio.charset.UnsupportedCharsetException;
2123

22-
import org.apache.commons.codec.CharEncoding;
24+
import org.apache.commons.codec.Charsets;
2325
import org.apache.commons.codec.DecoderException;
2426
import org.apache.commons.codec.EncoderException;
2527
import org.apache.commons.codec.StringDecoder;
@@ -49,13 +51,13 @@ public class BCodec extends RFC1522Codec implements StringEncoder, StringDecoder
4951
/**
5052
* The default charset used for string decoding and encoding.
5153
*/
52-
private final String charset;
54+
private final Charset charset;
5355

5456
/**
5557
* Default constructor.
5658
*/
5759
public BCodec() {
58-
this(CharEncoding.UTF_8);
60+
this(Charsets.UTF_8);
5961
}
6062

6163
/**
@@ -66,11 +68,24 @@ public BCodec() {
6668
*
6769
* @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
6870
*/
69-
public BCodec(final String charset) {
70-
super();
71+
public BCodec(final Charset charset) {
7172
this.charset = charset;
7273
}
7374

75+
/**
76+
* Constructor which allows for the selection of a default charset
77+
*
78+
* @param charsetName
79+
* the default charset to use.
80+
* @throws UnsupportedCharsetException
81+
* If the named charset is unavailable
82+
* @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable
83+
* @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
84+
*/
85+
public BCodec(final String charsetName) {
86+
this(Charset.forName(charsetName));
87+
}
88+
7489
@Override
7590
protected String getEncoding() {
7691
return "B";
@@ -92,6 +107,25 @@ protected byte[] doDecoding(byte[] bytes) {
92107
return Base64.decodeBase64(bytes);
93108
}
94109

110+
/**
111+
* Encodes a string into its Base64 form using the specified charset. Unsafe characters are escaped.
112+
*
113+
* @param value
114+
* string to convert to Base64 form
115+
* @param charset
116+
* the charset for <code>value</code>
117+
* @return Base64 string
118+
*
119+
* @throws EncoderException
120+
* thrown if a failure condition is encountered during the encoding process.
121+
*/
122+
public String encode(final String value, final Charset charset) throws EncoderException {
123+
if (value == null) {
124+
return null;
125+
}
126+
return encodeText(value, charset);
127+
}
128+
95129
/**
96130
* Encodes a string into its Base64 form using the specified charset. Unsafe characters are escaped.
97131
*
@@ -109,7 +143,7 @@ public String encode(final String value, final String charset) throws EncoderExc
109143
return null;
110144
}
111145
try {
112-
return encodeText(value, charset);
146+
return this.encodeText(value, charset);
113147
} catch (UnsupportedEncodingException e) {
114148
throw new EncoderException(e.getMessage(), e);
115149
}
@@ -129,7 +163,7 @@ public String encode(String value) throws EncoderException {
129163
if (value == null) {
130164
return null;
131165
}
132-
return encode(value, getDefaultCharset());
166+
return encode(value, this.getCharset());
133167
}
134168

135169
/**
@@ -147,7 +181,7 @@ public String decode(String value) throws DecoderException {
147181
return null;
148182
}
149183
try {
150-
return decodeText(value);
184+
return this.decodeText(value);
151185
} catch (UnsupportedEncodingException e) {
152186
throw new DecoderException(e.getMessage(), e);
153187
}
@@ -201,11 +235,21 @@ public Object decode(Object value) throws DecoderException {
201235
}
202236

203237
/**
204-
* The default charset used for string decoding and encoding.
238+
* Gets the default charset name used for string decoding and encoding.
205239
*
206-
* @return the default string charset.
240+
* @return the default charset name
241+
* @since 1.7
207242
*/
208-
public String getDefaultCharset() {
243+
public Charset getCharset() {
209244
return this.charset;
210245
}
246+
247+
/**
248+
* Gets the default charset name used for string decoding and encoding.
249+
*
250+
* @return the default charset name
251+
*/
252+
public String getDefaultCharset() {
253+
return this.charset.name();
254+
}
211255
}

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

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package org.apache.commons.codec.net;
1919

2020
import java.io.UnsupportedEncodingException;
21+
import java.nio.charset.Charset;
22+
import java.nio.charset.UnsupportedCharsetException;
2123
import java.util.BitSet;
2224

2325
import org.apache.commons.codec.CharEncoding;
@@ -50,7 +52,7 @@ public class QCodec extends RFC1522Codec implements StringEncoder, StringDecoder
5052
/**
5153
* The default charset used for string decoding and encoding.
5254
*/
53-
private final String charset;
55+
private final Charset charset;
5456

5557
/**
5658
* BitSet of printable characters as defined in RFC 1522.
@@ -121,11 +123,25 @@ public QCodec() {
121123
*
122124
* @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
123125
*/
124-
public QCodec(final String charset) {
126+
public QCodec(final Charset charset) {
125127
super();
126128
this.charset = charset;
127129
}
128130

131+
/**
132+
* Constructor which allows for the selection of a default charset
133+
*
134+
* @param charsetName
135+
* the charset to use.
136+
* @throws UnsupportedCharsetException
137+
* If the named charset is unavailable
138+
* @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable
139+
* @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
140+
*/
141+
public QCodec(final String charsetName) {
142+
this(Charset.forName(charsetName));
143+
}
144+
129145
@Override
130146
protected String getEncoding() {
131147
return "Q";
@@ -174,6 +190,25 @@ protected byte[] doDecoding(byte[] bytes) throws DecoderException {
174190
return QuotedPrintableCodec.decodeQuotedPrintable(bytes);
175191
}
176192

193+
/**
194+
* Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped.
195+
*
196+
* @param pString
197+
* string to convert to quoted-printable form
198+
* @param charset
199+
* the charset for pString
200+
* @return quoted-printable string
201+
*
202+
* @throws EncoderException
203+
* thrown if a failure condition is encountered during the encoding process.
204+
*/
205+
public String encode(final String pString, final Charset charset) throws EncoderException {
206+
if (pString == null) {
207+
return null;
208+
}
209+
return encodeText(pString, charset);
210+
}
211+
177212
/**
178213
* Encodes a string into its quoted-printable form using the specified charset. Unsafe characters are escaped.
179214
*
@@ -211,7 +246,7 @@ public String encode(String pString) throws EncoderException {
211246
if (pString == null) {
212247
return null;
213248
}
214-
return encode(pString, getDefaultCharset());
249+
return encode(pString, getCharset());
215250
}
216251

217252
/**
@@ -285,14 +320,24 @@ public Object decode(Object pObject) throws DecoderException {
285320
}
286321

287322
/**
288-
* The default charset used for string decoding and encoding.
323+
* Gets the default charset name used for string decoding and encoding.
289324
*
290-
* @return the default string charset.
325+
* @return the default charset name
326+
* @since 1.7
291327
*/
292-
public String getDefaultCharset() {
328+
public Charset getCharset() {
293329
return this.charset;
294330
}
295331

332+
/**
333+
* Gets the default charset name used for string decoding and encoding.
334+
*
335+
* @return the default charset name
336+
*/
337+
public String getDefaultCharset() {
338+
return this.charset.name();
339+
}
340+
296341
/**
297342
* Tests if optional transformation of SPACE characters is to be used
298343
*

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

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.apache.commons.codec.net;
1919

2020
import java.io.UnsupportedEncodingException;
21+
import java.nio.charset.Charset;
2122

2223
import org.apache.commons.codec.DecoderException;
2324
import org.apache.commons.codec.EncoderException;
@@ -74,12 +75,10 @@ abstract class RFC1522Codec {
7475
*
7576
* @throws EncoderException thrown if there is an error condition during the Encoding
7677
* process.
77-
* @throws UnsupportedEncodingException thrown if charset is not supported
78-
*
7978
* @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
8079
*/
81-
protected String encodeText(final String text, final String charset)
82-
throws EncoderException, UnsupportedEncodingException
80+
protected String encodeText(final String text, final Charset charset)
81+
throws EncoderException
8382
{
8483
if (text == null) {
8584
return null;
@@ -88,14 +87,40 @@ protected String encodeText(final String text, final String charset)
8887
buffer.append(PREFIX);
8988
buffer.append(charset);
9089
buffer.append(SEP);
91-
buffer.append(getEncoding());
90+
buffer.append(this.getEncoding());
9291
buffer.append(SEP);
93-
byte [] rawdata = doEncoding(text.getBytes(charset));
94-
buffer.append(StringUtils.newStringUsAscii(rawdata));
92+
byte [] rawData = this.doEncoding(text.getBytes(charset));
93+
buffer.append(StringUtils.newStringUsAscii(rawData));
9594
buffer.append(POSTFIX);
9695
return buffer.toString();
9796
}
9897

98+
/**
99+
* Applies an RFC 1522 compliant encoding scheme to the given string of text with the
100+
* given charset. This method constructs the "encoded-word" header common to all the
101+
* RFC 1522 codecs and then invokes {@link #doEncoding(byte [])} method of a concrete
102+
* class to perform the specific encoding.
103+
*
104+
* @param text a string to encode
105+
* @param charsetName the charset to use
106+
*
107+
* @return RFC 1522 compliant "encoded-word"
108+
*
109+
* @throws EncoderException thrown if there is an error condition during the Encoding
110+
* process.
111+
* @throws UnsupportedEncodingException if charset is not available
112+
*
113+
* @see <a href="http://download.oracle.com/javase/1.5.0/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
114+
*/
115+
protected String encodeText(final String text, final String charsetName)
116+
throws EncoderException, UnsupportedEncodingException
117+
{
118+
if (text == null) {
119+
return null;
120+
}
121+
return this.encodeText(text, Charset.forName(charsetName));
122+
}
123+
99124
/**
100125
* Applies an RFC 1522 compliant decoding scheme to the given string of text. This method
101126
* processes the "encoded-word" header common to all the RFC 1522 codecs and then invokes

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

Lines changed: 4 additions & 14 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;
@@ -123,21 +125,9 @@ public void testEncodeObjects() throws Exception {
123125
}
124126
}
125127

126-
@Test
128+
@Test(expected=UnsupportedCharsetException.class)
127129
public void testInvalidEncoding() {
128-
BCodec bcodec = new BCodec("NONSENSE");
129-
try {
130-
bcodec.encode("Hello there!");
131-
fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked.");
132-
} catch (EncoderException ee) {
133-
// Exception expected, test segment passes.
134-
}
135-
try {
136-
bcodec.decode("=?NONSENSE?B?Hello there!?=");
137-
fail("We set the encoding to a bogus NONSENSE value, this shouldn't have worked.");
138-
} catch (DecoderException ee) {
139-
// Exception expected, test segment passes.
140-
}
130+
new BCodec("NONSENSE");
141131
}
142132

143133
@Test

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import static org.junit.Assert.assertTrue;
2525
import static org.junit.Assert.fail;
2626

27+
import java.nio.charset.UnsupportedCharsetException;
28+
2729
import org.apache.commons.codec.CharEncoding;
2830
import org.apache.commons.codec.DecoderException;
2931
import org.apache.commons.codec.EncoderException;
@@ -150,21 +152,9 @@ public void testEncodeObjects() throws Exception {
150152
}
151153

152154

153-
@Test
155+
@Test(expected=UnsupportedCharsetException.class)
154156
public void testInvalidEncoding() {
155-
QCodec qcodec = new QCodec("NONSENSE");
156-
try {
157-
qcodec.encode("Hello there!");
158-
fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
159-
} catch (EncoderException ee) {
160-
// Exception expected, test segment passes.
161-
}
162-
try {
163-
qcodec.decode("=?NONSENSE?Q?Hello there!?=");
164-
fail( "We set the encoding to a bogus NONSENSE vlaue, this shouldn't have worked.");
165-
} catch (DecoderException ee) {
166-
// Exception expected, test segment passes.
167-
}
157+
new QCodec("NONSENSE");
168158
}
169159

170160
@Test

0 commit comments

Comments
 (0)