Skip to content

Commit 90caed9

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@1306398 13f79535-47bb-0310-9956-ffa450edef68
1 parent 453c56c commit 90caed9

2 files changed

Lines changed: 53 additions & 52 deletions

File tree

  • src
    • main/java/org/apache/commons/codec/binary
    • test/java/org/apache/commons/codec/binary

src/main/java/org/apache/commons/codec/binary/Hex.java

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717

1818
package org.apache.commons.codec.binary;
1919

20-
import java.io.UnsupportedEncodingException;
20+
import java.nio.charset.Charset;
21+
import java.nio.charset.UnsupportedCharsetException;
2122

2223
import org.apache.commons.codec.BinaryDecoder;
2324
import org.apache.commons.codec.BinaryEncoder;
2425
import org.apache.commons.codec.CharEncoding;
26+
import org.apache.commons.codec.Charsets;
2527
import org.apache.commons.codec.DecoderException;
2628
import org.apache.commons.codec.EncoderException;
2729

@@ -35,6 +37,13 @@
3537
*/
3638
public class Hex implements BinaryEncoder, BinaryDecoder {
3739

40+
/**
41+
* Default charset name is {@link Charsets#UTF_8}
42+
*
43+
* @since 1.7
44+
*/
45+
public static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
46+
3847
/**
3948
* Default charset name is {@link CharEncoding#UTF_8}
4049
*
@@ -169,25 +178,39 @@ protected static int toDigit(char ch, int index) throws DecoderException {
169178
return digit;
170179
}
171180

172-
private final String charsetName;
181+
private final Charset charset;
173182

174183
/**
175-
* Creates a new codec with the default charset name {@link #DEFAULT_CHARSET_NAME}
184+
* Creates a new codec with the default charset name {@link #DEFAULT_CHARSET}
176185
*/
177186
public Hex() {
178187
// use default encoding
179-
this.charsetName = DEFAULT_CHARSET_NAME;
188+
this.charset = DEFAULT_CHARSET;
189+
}
190+
191+
/**
192+
* Creates a new codec with the given Charset.
193+
*
194+
* @param charset
195+
* the charset.
196+
* @since 1.7
197+
*/
198+
public Hex(Charset charset) {
199+
this.charset = charset;
180200
}
181201

182202
/**
183203
* Creates a new codec with the given charset name.
184204
*
185-
* @param csName
205+
* @param charsetName
186206
* the charset name.
207+
* @throws UnsupportedCharsetException
208+
* If the named charset is unavailable
187209
* @since 1.4
210+
* @since 1.7 throws UnsupportedCharsetException if the named charset is unavailable
188211
*/
189-
public Hex(String csName) {
190-
this.charsetName = csName;
212+
public Hex(String charsetName) {
213+
this(Charset.forName(charsetName));
191214
}
192215

193216
/**
@@ -203,11 +226,7 @@ public Hex(String csName) {
203226
* @see #decodeHex(char[])
204227
*/
205228
public byte[] decode(byte[] array) throws DecoderException {
206-
try {
207-
return decodeHex(new String(array, getCharsetName()).toCharArray());
208-
} catch (UnsupportedEncodingException e) {
209-
throw new DecoderException(e.getMessage(), e);
210-
}
229+
return decodeHex(new String(array, getCharset()).toCharArray());
211230
}
212231

213232
/**
@@ -238,19 +257,17 @@ public Object decode(Object object) throws DecoderException {
238257
* represent any given byte.
239258
* <p>
240259
* The conversion from hexadecimal characters to the returned bytes is performed with the charset named by
241-
* {@link #getCharsetName()}.
260+
* {@link #getCharset()}.
242261
* </p>
243262
*
244263
* @param array
245264
* a byte[] to convert to Hex characters
246265
* @return A byte[] containing the bytes of the hexadecimal characters
247-
* @throws IllegalStateException
248-
* if the charsetName is invalid. This API throws {@link IllegalStateException} instead of
249-
* {@link UnsupportedEncodingException} for backward compatibility.
266+
* @since 1.7 No longer throws IllegalStateException if the charsetName is invalid.
250267
* @see #encodeHex(byte[])
251268
*/
252269
public byte[] encode(byte[] array) {
253-
return StringUtils.getBytesUnchecked(encodeHexString(array), getCharsetName());
270+
return encodeHexString(array).getBytes(this.getCharset());
254271
}
255272

256273
/**
@@ -259,7 +276,7 @@ public byte[] encode(byte[] array) {
259276
* characters to represent any given byte.
260277
* <p>
261278
* The conversion from hexadecimal characters to bytes to be encoded to performed with the charset named by
262-
* {@link #getCharsetName()}.
279+
* {@link #getCharset()}.
263280
* </p>
264281
*
265282
* @param object
@@ -271,23 +288,31 @@ public byte[] encode(byte[] array) {
271288
*/
272289
public Object encode(Object object) throws EncoderException {
273290
try {
274-
byte[] byteArray = object instanceof String ? ((String) object).getBytes(getCharsetName()) : (byte[]) object;
291+
byte[] byteArray = object instanceof String ? ((String) object).getBytes(this.getCharset()) : (byte[]) object;
275292
return encodeHex(byteArray);
276293
} catch (ClassCastException e) {
277294
throw new EncoderException(e.getMessage(), e);
278-
} catch (UnsupportedEncodingException e) {
279-
throw new EncoderException(e.getMessage(), e);
280295
}
281296
}
282297

298+
/**
299+
* Gets the charset.
300+
*
301+
* @return the charset.
302+
* @since 1.7
303+
*/
304+
public Charset getCharset() {
305+
return this.charset;
306+
}
307+
283308
/**
284309
* Gets the charset name.
285310
*
286311
* @return the charset name.
287312
* @since 1.4
288313
*/
289314
public String getCharsetName() {
290-
return this.charsetName;
315+
return this.charset.name();
291316
}
292317

293318
/**
@@ -297,6 +322,6 @@ public String getCharsetName() {
297322
*/
298323
@Override
299324
public String toString() {
300-
return super.toString() + "[charsetName=" + this.charsetName + "]";
325+
return super.toString() + "[charsetName=" + this.charset + "]";
301326
}
302327
}

src/test/java/org/apache/commons/codec/binary/HexTest.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.io.UnsupportedEncodingException;
2626
import java.nio.charset.Charset;
27+
import java.nio.charset.UnsupportedCharsetException;
2728
import java.util.Arrays;
2829
import java.util.Random;
2930

@@ -159,7 +160,7 @@ private void testCustomCharset(String name, String parent) throws UnsupportedEnc
159160
Hex utf8Codec = new Hex();
160161
expectedHexString = "48656c6c6f20576f726c64";
161162
byte[] decodedUtf8Bytes = (byte[]) utf8Codec.decode(expectedHexString);
162-
actualStringFromBytes = new String(decodedUtf8Bytes, utf8Codec.getCharsetName());
163+
actualStringFromBytes = new String(decodedUtf8Bytes, utf8Codec.getCharset());
163164
// sanity check:
164165
assertEquals(name, sourceString, actualStringFromBytes);
165166
// actual check:
@@ -168,34 +169,9 @@ private void testCustomCharset(String name, String parent) throws UnsupportedEnc
168169
assertEquals(name, sourceString, actualStringFromBytes);
169170
}
170171

171-
@Test
172-
public void testCustomCharsetBadNameEncodeByteArray() throws UnsupportedEncodingException {
173-
try {
174-
new Hex(BAD_ENCODING_NAME).encode("Hello World".getBytes("UTF-8"));
175-
fail("Expected " + IllegalStateException.class.getName());
176-
} catch (IllegalStateException e) {
177-
// Expected
178-
}
179-
}
180-
181-
@Test
182-
public void testCustomCharsetBadNameEncodeObject() {
183-
try {
184-
new Hex(BAD_ENCODING_NAME).encode("Hello World");
185-
fail("Expected " + EncoderException.class.getName());
186-
} catch (EncoderException e) {
187-
// Expected
188-
}
189-
}
190-
191-
@Test
192-
public void testCustomCharsetBadNameDecodeObject() throws UnsupportedEncodingException {
193-
try {
194-
new Hex(BAD_ENCODING_NAME).decode("Hello World".getBytes("UTF-8"));
195-
fail("Expected " + DecoderException.class.getName());
196-
} catch (DecoderException e) {
197-
// Expected
198-
}
172+
@Test(expected=UnsupportedCharsetException.class)
173+
public void testCustomCharsetBadName() throws UnsupportedEncodingException {
174+
new Hex(BAD_ENCODING_NAME);
199175
}
200176

201177
@Test

0 commit comments

Comments
 (0)