You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by gg...@apache.org on 2003/11/03 20:03:17 UTC
cvs commit: jakarta-commons/codec/src/java/org/apache/commons/codec/binary Hex.java
ggregory 2003/11/03 11:03:17
Modified: codec/src/java/org/apache/commons/codec/binary Hex.java
Log:
PR: 24360
Submitted by: Gary Gregory
[codec] ClassCastException in Hex.decode(Object) fixed.
Javadoc.
Revision Changes Path
1.9 +105 -64 jakarta-commons/codec/src/java/org/apache/commons/codec/binary/Hex.java
Index: Hex.java
===================================================================
RCS file: /home/cvs/jakarta-commons/codec/src/java/org/apache/commons/codec/binary/Hex.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Hex.java 5 Oct 2003 21:45:49 -0000 1.8
+++ Hex.java 3 Nov 2003 19:03:17 -0000 1.9
@@ -63,54 +63,55 @@
import org.apache.commons.codec.EncoderException;
/**
- * Hex encoder/decoder.
+ * Hex encoder and decoder.
*
* @author <a href="mailto:siege@preoccupied.net">Christopher O'Brien</a>
* @author Tim O'Brien
+ * @author Gary Gregory
* @version $Id$
*/
public class Hex implements BinaryEncoder, BinaryDecoder {
-
- /** for building output as Hex */
+ /**
+ * Used building output as Hex
+ */
private static char[] digits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
- public Object encode(Object pObject) throws EncoderException {
- if(pObject instanceof String) {
- pObject = ((String) pObject).getBytes();
- }
-
- try {
- return encodeHex((byte[]) pObject);
- } catch(Exception e) {
- throw new EncoderException(e.getMessage());
- }
- }
-
- public byte[] encode(byte[] pArray) {
- return new String(encodeHex(pArray)).getBytes();
- }
-
- public Object decode(Object pObject) throws DecoderException {
- if(pObject instanceof String) {
- pObject = ((String) pObject).getBytes();
- }
-
- try {
- return decodeHex((char[]) pObject);
- } catch(Exception e) {
- throw new DecoderException(e.getMessage());
- }
- }
-
-
-
- public byte[] decode(byte[] pArray) throws DecoderException {
- return decodeHex(new String(pArray).toCharArray());
- }
+ /**
+ * Converts an array of characters representing hexidecimal values into an
+ * array of bytes of those same values. The returned array will be half the
+ * length of the passed array, as it takes two characters to represent any
+ * given byte. An exception is thrown if the passed char array has an odd
+ * number of elements.
+ *
+ * @param data An array of characters containing hexidecimal digits
+ * @return A byte array containing binary data decoded from
+ * the supplied char array.
+ * @throws DecoderException Thrown if an odd number of characters is supplied
+ * to this function
+ */
+ public static byte[] decodeHex(char[] data) throws DecoderException {
+
+ int l = data.length;
+
+ if ((l & 0x01) != 0) {
+ throw new DecoderException("Odd number of characters.");
+ }
+
+ byte[] out = new byte[l >> 1];
+
+ // two characters form the hex value.
+ for (int i = 0, j = 0; j < l; i++) {
+ int f = Character.digit(data[j++], 16) << 4;
+ f = f | Character.digit(data[j++], 16);
+ out[i] = (byte) (f & 0xFF);
+ }
+
+ return out;
+ }
/**
* Converts an array of bytes into an array of characters representing the
@@ -118,7 +119,7 @@
* double the length of the passed array, as it takes two characters to
* represent any given byte.
*
- * @param data array of byte to convert to Hex characters
+ * @param data a byte[] to convert to Hex characters
* @return A char[] containing hexidecimal characters
*/
public static char[] encodeHex(byte[] data) {
@@ -135,41 +136,81 @@
return out;
}
-
-
-
+
/**
- * Converts an array of characters representing hexidecimal values into an
+ * Converts an array of character bytes representing hexidecimal values into an
* array of bytes of those same values. The returned array will be half the
* length of the passed array, as it takes two characters to represent any
* given byte. An exception is thrown if the passed char array has an odd
* number of elements.
*
- * @param data An array of characters containing hexidecimal digits
- * @return A byte array array containing binary data decoded from
- * the supplied char array.
- * @throws Exception Thrown if an odd number of characters is supplied
+ * @param array An array of character bytes containing hexidecimal digits
+ * @return A byte array containing binary data decoded from
+ * the supplied byte array (representing characters).
+ * @throws DecoderException Thrown if an odd number of characters is supplied
* to this function
+ * @see #decodeHex(char[])
*/
- public static byte[] decodeHex(char[] data) throws DecoderException {
-
- int l = data.length;
-
- if ((l & 0x01) != 0) {
- throw new DecoderException("odd number of characters.");
- }
-
- byte[] out = new byte[l >> 1];
-
- // two characters form the hex value.
- for (int i = 0, j = 0; j < l; i++) {
- int f = Character.digit(data[j++], 16) << 4;
- f = f | Character.digit(data[j++], 16);
- out[i] = (byte) (f & 0xFF);
- }
+ public byte[] decode(byte[] array) throws DecoderException {
+ return decodeHex(new String(array).toCharArray());
+ }
+
+ /**
+ * Converts a String or an array of character bytes representing hexidecimal values into an
+ * array of bytes of those same values. The returned array will be half the
+ * length of the passed String or array, as it takes two characters to represent any
+ * given byte. An exception is thrown if the passed char array has an odd
+ * number of elements.
+ *
+ * @param object A String or, an array of character bytes containing hexidecimal digits
+ * @return A byte array containing binary data decoded from
+ * the supplied byte array (representing characters).
+ * @throws DecoderException Thrown if an odd number of characters is supplied
+ * to this function or the object is not a String or char[]
+ * @see #decodeHex(char[])
+ */
+ public Object decode(Object object) throws DecoderException {
+ try {
+ char[] charArray = object instanceof String ? ((String) object).toCharArray() : (char[]) object;
+ return decodeHex(charArray);
+ } catch (ClassCastException e) {
+ throw new DecoderException(e.getMessage());
+ }
+ }
+
+ /**
+ * Converts an array of bytes into an array of bytes for the characters representing the
+ * hexidecimal values of each byte in order. The returned array will be
+ * double the length of the passed array, as it takes two characters to
+ * represent any given byte.
+ *
+ * @param array a byte[] to convert to Hex characters
+ * @return A byte[] containing the bytes of the hexidecimal characters
+ * @see #encodeHex(byte[])
+ */
+ public byte[] encode(byte[] array) {
+ return new String(encodeHex(array)).getBytes();
+ }
- return out;
- }
+ /**
+ * Converts a String or an array of bytes into an array of characters representing the
+ * hexidecimal values of each byte in order. The returned array will be
+ * double the length of the passed String or array, as it takes two characters to
+ * represent any given byte.
+ *
+ * @param object a String, or byte[] to convert to Hex characters
+ * @return A char[] containing hexidecimal characters
+ * @throws EncoderException Thrown if the given object is not a String or byte[]
+ * @see #encodeHex(byte[])
+ */
+ public Object encode(Object object) throws EncoderException {
+ try {
+ byte[] byteArray = object instanceof String ? ((String) object).getBytes() : (byte[]) object;
+ return encodeHex(byteArray);
+ } catch (ClassCastException e) {
+ throw new EncoderException(e.getMessage());
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org