1717
1818package org .apache .commons .codec .binary ;
1919
20- import java .io .UnsupportedEncodingException ;
20+ import java .nio .charset .Charset ;
21+ import java .nio .charset .UnsupportedCharsetException ;
2122
2223import org .apache .commons .codec .BinaryDecoder ;
2324import org .apache .commons .codec .BinaryEncoder ;
2425import org .apache .commons .codec .CharEncoding ;
26+ import org .apache .commons .codec .Charsets ;
2527import org .apache .commons .codec .DecoderException ;
2628import org .apache .commons .codec .EncoderException ;
2729
3537 */
3638public 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}
0 commit comments