Skip to content

Commit c5154bc

Browse files
committed
[CODEC-194] Support java.nio.ByteBuffer in org.apache.commons.codec.binary.Hex.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1637888 13f79535-47bb-0310-9956-ffa450edef68
1 parent a355684 commit c5154bc

4 files changed

Lines changed: 266 additions & 24 deletions

File tree

src/changes/changes.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ The <action> type attribute can be add,update,fix,remove.
4343
</properties>
4444
<body>
4545
<release version="1.11" date="DD MM 2014" description="Feature and fix release.">
46-
46+
<action dev="ggregory" type="add" issue="CODEC-194">Support java.nio.ByteBuffer in org.apache.commons.codec.binary.Hex</action>
4747
</release>
4848
<release version="1.10" date="5 November 2014" description="Feature and fix release.">
4949
<action dev="ggregory" type="add" issue="CODEC-192" due-to="Thomas Neidhart">Add Daitch-Mokotoff Soundex</action>

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

Lines changed: 122 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

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

20+
import java.nio.ByteBuffer;
2021
import java.nio.charset.Charset;
2122

2223
import 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
/**

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

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

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

2324
import org.apache.commons.codec.CharEncoding;
@@ -96,6 +97,41 @@ private static byte[] getBytes(final String string, final Charset charset) {
9697
return string.getBytes(charset);
9798
}
9899

100+
/**
101+
* Calls {@link String#getBytes(Charset)}
102+
*
103+
* @param string
104+
* The string to encode (if null, return null).
105+
* @param charset
106+
* The {@link Charset} to encode the <code>String</code>
107+
* @return the encoded bytes
108+
* @since 1.11
109+
*/
110+
private static ByteBuffer getByteBuffer(final String string, final Charset charset) {
111+
if (string == null) {
112+
return null;
113+
}
114+
return ByteBuffer.wrap(string.getBytes(charset));
115+
}
116+
117+
/**
118+
* Encodes the given string into a byte buffer using the UTF-8 charset, storing the result into a new byte
119+
* array.
120+
*
121+
* @param string
122+
* the String to encode, may be <code>null</code>
123+
* @return encoded bytes, or <code>null</code> if the input string was <code>null</code>
124+
* @throws NullPointerException
125+
* Thrown if {@link Charsets#UTF_8} is not initialized, which should never happen since it is
126+
* required by the Java platform specification.
127+
* @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
128+
* @see #getBytesUnchecked(String, String)
129+
* @since 1.11
130+
*/
131+
public static ByteBuffer getByteBufferUtf8(final String string) {
132+
return getByteBuffer(string, Charsets.UTF_8);
133+
}
134+
99135
/**
100136
* Encodes the given string into a sequence of bytes using the ISO-8859-1 charset, storing the result into a new
101137
* byte array.

0 commit comments

Comments
 (0)