1717
1818package org .apache .commons .codec .binary ;
1919
20+ import java .nio .ByteBuffer ;
2021import java .nio .charset .Charset ;
2122
2223import org .apache .commons .codec .BinaryDecoder ;
@@ -109,6 +110,20 @@ public static char[] encodeHex(final byte[] data) {
109110 return encodeHex (data , true );
110111 }
111112
113+ /**
114+ * Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order.
115+ * The returned array will be double the length of the passed array, as it takes two characters to represent any
116+ * given byte.
117+ *
118+ * @param data
119+ * a byte buffer to convert to Hex characters
120+ * @return A char[] containing hexadecimal characters
121+ * @since 1.11
122+ */
123+ public static char [] encodeHex (final ByteBuffer data ) {
124+ return encodeHex (data , true );
125+ }
126+
112127 /**
113128 * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
114129 * The returned array will be double the length of the passed array, as it takes two characters to represent any
@@ -125,6 +140,22 @@ public static char[] encodeHex(final byte[] data, final boolean toLowerCase) {
125140 return encodeHex (data , toLowerCase ? DIGITS_LOWER : DIGITS_UPPER );
126141 }
127142
143+ /**
144+ * Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order.
145+ * The returned array will be double the length of the passed array, as it takes two characters to represent any
146+ * given byte.
147+ *
148+ * @param data
149+ * a byte buffer to convert to Hex characters
150+ * @param toLowerCase
151+ * <code>true</code> converts to lowercase, <code>false</code> to uppercase
152+ * @return A char[] containing hexadecimal characters
153+ * @since 1.11
154+ */
155+ public static char [] encodeHex (final ByteBuffer data , final boolean toLowerCase ) {
156+ return encodeHex (data , toLowerCase ? DIGITS_LOWER : DIGITS_UPPER );
157+ }
158+
128159 /**
129160 * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order.
130161 * The returned array will be double the length of the passed array, as it takes two characters to represent any
@@ -148,6 +179,22 @@ protected static char[] encodeHex(final byte[] data, final char[] toDigits) {
148179 return out ;
149180 }
150181
182+ /**
183+ * Converts a byte buffer into an array of characters representing the hexadecimal values of each byte in order.
184+ * The returned array will be double the length of the passed array, as it takes two characters to represent any
185+ * given byte.
186+ *
187+ * @param data
188+ * a byte buffer to convert to Hex characters
189+ * @param toDigits
190+ * the output alphabet
191+ * @return A char[] containing hexadecimal characters
192+ * @since 1.11
193+ */
194+ protected static char [] encodeHex (final ByteBuffer data , final char [] toDigits ) {
195+ return encodeHex (data .array (), toDigits );
196+ }
197+
151198 /**
152199 * Converts an array of bytes into a String representing the hexadecimal values of each byte in order. The returned
153200 * String will be double the length of the passed array, as it takes two characters to represent any given byte.
@@ -161,6 +208,19 @@ public static String encodeHexString(final byte[] data) {
161208 return new String (encodeHex (data ));
162209 }
163210
211+ /**
212+ * Converts a byte buffer into a String representing the hexadecimal values of each byte in order. The returned
213+ * String will be double the length of the passed array, as it takes two characters to represent any given byte.
214+ *
215+ * @param data
216+ * a byte buffer to convert to Hex characters
217+ * @return A String containing hexadecimal characters
218+ * @since 1.11
219+ */
220+ public static String encodeHexString (final ByteBuffer data ) {
221+ return new String (encodeHex (data ));
222+ }
223+
164224 /**
165225 * Converts a hexadecimal character to an integer.
166226 *
@@ -232,13 +292,30 @@ public byte[] decode(final byte[] array) throws DecoderException {
232292 return decodeHex (new String (array , getCharset ()).toCharArray ());
233293 }
234294
295+ /**
296+ * Converts a buffer of character bytes representing hexadecimal values into an array of bytes of those same values.
297+ * The returned array will be half the length of the passed array, as it takes two characters to represent any given
298+ * byte. An exception is thrown if the passed char array has an odd number of elements.
299+ *
300+ * @param buffer
301+ * An array of character bytes containing hexadecimal digits
302+ * @return A byte array containing binary data decoded from the supplied byte array (representing characters).
303+ * @throws DecoderException
304+ * Thrown if an odd number of characters is supplied to this function
305+ * @see #decodeHex(char[])
306+ * @since 1.11
307+ */
308+ public byte [] decode (final ByteBuffer buffer ) throws DecoderException {
309+ return decodeHex (new String (buffer .array (), getCharset ()).toCharArray ());
310+ }
311+
235312 /**
236313 * Converts a String or an array of character bytes representing hexadecimal values into an array of bytes of those
237314 * same values. The returned array will be half the length of the passed String or array, as it takes two characters
238315 * to represent any given byte. An exception is thrown if the passed char array has an odd number of elements.
239316 *
240317 * @param object
241- * A String or, an array of character bytes containing hexadecimal digits
318+ * A String, ByteBuffer, byte[], or an array of character bytes containing hexadecimal digits
242319 * @return A byte array containing binary data decoded from the supplied byte array (representing characters).
243320 * @throws DecoderException
244321 * Thrown if an odd number of characters is supplied to this function or the object is not a String or
@@ -247,11 +324,18 @@ public byte[] decode(final byte[] array) throws DecoderException {
247324 */
248325 @ Override
249326 public Object decode (final Object object ) throws DecoderException {
250- try {
251- final char [] charArray = object instanceof String ? ((String ) object ).toCharArray () : (char []) object ;
252- return decodeHex (charArray );
253- } catch (final ClassCastException e ) {
254- throw new DecoderException (e .getMessage (), e );
327+ if (object instanceof String ) {
328+ return decode (((String ) object ).toCharArray ());
329+ } else if (object instanceof byte []) {
330+ return decode ((byte []) object );
331+ } else if (object instanceof ByteBuffer ) {
332+ return decode ((ByteBuffer ) object );
333+ } else {
334+ try {
335+ return decodeHex ((char []) object );
336+ } catch (final ClassCastException e ) {
337+ throw new DecoderException (e .getMessage (), e );
338+ }
255339 }
256340 }
257341
@@ -275,6 +359,25 @@ public byte[] encode(final byte[] array) {
275359 return encodeHexString (array ).getBytes (this .getCharset ());
276360 }
277361
362+ /**
363+ * Converts byte buffer into an array of bytes for the characters representing the hexadecimal values of each
364+ * byte in order. The returned array will be double the length of the passed array, as it takes two characters to
365+ * represent any given byte.
366+ * <p>
367+ * The conversion from hexadecimal characters to the returned bytes is performed with the charset named by
368+ * {@link #getCharset()}.
369+ * </p>
370+ *
371+ * @param array
372+ * a byte buffer to convert to Hex characters
373+ * @return A byte[] containing the bytes of the hexadecimal characters
374+ * @see #encodeHex(byte[])
375+ * @since 1.11
376+ */
377+ public byte [] encode (final ByteBuffer array ) {
378+ return encodeHexString (array ).getBytes (this .getCharset ());
379+ }
380+
278381 /**
279382 * Converts a String or an array of bytes into an array of characters representing the hexadecimal values of each
280383 * byte in order. The returned array will be double the length of the passed String or array, as it takes two
@@ -285,21 +388,27 @@ public byte[] encode(final byte[] array) {
285388 * </p>
286389 *
287390 * @param object
288- * a String, or byte[] to convert to Hex characters
391+ * a String, ByteBuffer, or byte[] to convert to Hex characters
289392 * @return A char[] containing hexadecimal characters
290393 * @throws EncoderException
291394 * Thrown if the given object is not a String or byte[]
292395 * @see #encodeHex(byte[])
293396 */
294397 @ Override
295398 public Object encode (final Object object ) throws EncoderException {
296- try {
297- final byte [] byteArray = object instanceof String ?
298- ((String ) object ).getBytes (this .getCharset ()) : (byte []) object ;
299- return encodeHex (byteArray );
300- } catch (final ClassCastException e ) {
301- throw new EncoderException (e .getMessage (), e );
399+ byte [] byteArray ;
400+ if (object instanceof String ) {
401+ byteArray = ((String ) object ).getBytes (this .getCharset ());
402+ } else if (object instanceof ByteBuffer ) {
403+ byteArray = ((ByteBuffer ) object ).array ();
404+ } else {
405+ try {
406+ byteArray = (byte []) object ;
407+ } catch (final ClassCastException e ) {
408+ throw new EncoderException (e .getMessage (), e );
409+ }
302410 }
411+ return encodeHex (byteArray );
303412 }
304413
305414 /**
0 commit comments