6363import org .apache .commons .codec .EncoderException ;
6464
6565/**
66- * Hex encoder/ decoder.
66+ * Hex encoder and decoder.
6767 *
6868 * @author <a href="mailto:siege@preoccupied.net">Christopher O'Brien</a>
6969 * @author Tim O'Brien
70- * @version $Id: Hex.java,v 1.8 2003/10/05 21:45:49 tobrien Exp $
70+ * @author Gary Gregory
71+ * @version $Id: Hex.java,v 1.9 2003/11/03 19:03:17 ggregory Exp $
7172 */
7273public class Hex implements BinaryEncoder , BinaryDecoder {
7374
74-
75- /** for building output as Hex */
75+ /**
76+ * Used building output as Hex
77+ */
7678 private static char [] digits = {
7779 '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' ,
7880 '8' , '9' , 'a' , 'b' , 'c' , 'd' , 'e' , 'f'
7981 };
8082
81- public Object encode (Object pObject ) throws EncoderException {
82- if (pObject instanceof String ) {
83- pObject = ((String ) pObject ).getBytes ();
84- }
85-
86- try {
87- return encodeHex ((byte []) pObject );
88- } catch (Exception e ) {
89- throw new EncoderException (e .getMessage ());
90- }
91- }
92-
93- public byte [] encode (byte [] pArray ) {
94- return new String (encodeHex (pArray )).getBytes ();
95- }
96-
97- public Object decode (Object pObject ) throws DecoderException {
98- if (pObject instanceof String ) {
99- pObject = ((String ) pObject ).getBytes ();
100- }
101-
102- try {
103- return decodeHex ((char []) pObject );
104- } catch (Exception e ) {
105- throw new DecoderException (e .getMessage ());
106- }
107- }
108-
109-
110-
111- public byte [] decode (byte [] pArray ) throws DecoderException {
112- return decodeHex (new String (pArray ).toCharArray ());
113- }
83+ /**
84+ * Converts an array of characters representing hexidecimal values into an
85+ * array of bytes of those same values. The returned array will be half the
86+ * length of the passed array, as it takes two characters to represent any
87+ * given byte. An exception is thrown if the passed char array has an odd
88+ * number of elements.
89+ *
90+ * @param data An array of characters containing hexidecimal digits
91+ * @return A byte array containing binary data decoded from
92+ * the supplied char array.
93+ * @throws DecoderException Thrown if an odd number of characters is supplied
94+ * to this function
95+ */
96+ public static byte [] decodeHex (char [] data ) throws DecoderException {
97+
98+ int l = data .length ;
99+
100+ if ((l & 0x01 ) != 0 ) {
101+ throw new DecoderException ("Odd number of characters." );
102+ }
103+
104+ byte [] out = new byte [l >> 1 ];
105+
106+ // two characters form the hex value.
107+ for (int i = 0 , j = 0 ; j < l ; i ++) {
108+ int f = Character .digit (data [j ++], 16 ) << 4 ;
109+ f = f | Character .digit (data [j ++], 16 );
110+ out [i ] = (byte ) (f & 0xFF );
111+ }
112+
113+ return out ;
114+ }
114115
115116 /**
116117 * Converts an array of bytes into an array of characters representing the
117118 * hexidecimal values of each byte in order. The returned array will be
118119 * double the length of the passed array, as it takes two characters to
119120 * represent any given byte.
120121 *
121- * @param data array of byte to convert to Hex characters
122+ * @param data a byte[] to convert to Hex characters
122123 * @return A char[] containing hexidecimal characters
123124 */
124125 public static char [] encodeHex (byte [] data ) {
@@ -135,41 +136,81 @@ public static char[] encodeHex(byte[] data) {
135136
136137 return out ;
137138 }
138-
139-
140-
139+
141140 /**
142- * Converts an array of characters representing hexidecimal values into an
141+ * Converts an array of character bytes representing hexidecimal values into an
143142 * array of bytes of those same values. The returned array will be half the
144143 * length of the passed array, as it takes two characters to represent any
145144 * given byte. An exception is thrown if the passed char array has an odd
146145 * number of elements.
147146 *
148- * @param data An array of characters containing hexidecimal digits
149- * @return A byte array array containing binary data decoded from
150- * the supplied char array.
151- * @throws Exception Thrown if an odd number of characters is supplied
147+ * @param array An array of character bytes containing hexidecimal digits
148+ * @return A byte array containing binary data decoded from
149+ * the supplied byte array (representing characters) .
150+ * @throws DecoderException Thrown if an odd number of characters is supplied
152151 * to this function
152+ * @see #decodeHex(char[])
153153 */
154- public static byte [] decodeHex (char [] data ) throws DecoderException {
155-
156- int l = data .length ;
157-
158- if ((l & 0x01 ) != 0 ) {
159- throw new DecoderException ("odd number of characters." );
160- }
161-
162- byte [] out = new byte [l >> 1 ];
163-
164- // two characters form the hex value.
165- for (int i = 0 , j = 0 ; j < l ; i ++) {
166- int f = Character .digit (data [j ++], 16 ) << 4 ;
167- f = f | Character .digit (data [j ++], 16 );
168- out [i ] = (byte ) (f & 0xFF );
169- }
154+ public byte [] decode (byte [] array ) throws DecoderException {
155+ return decodeHex (new String (array ).toCharArray ());
156+ }
157+
158+ /**
159+ * Converts a String or an array of character bytes representing hexidecimal values into an
160+ * array of bytes of those same values. The returned array will be half the
161+ * length of the passed String or array, as it takes two characters to represent any
162+ * given byte. An exception is thrown if the passed char array has an odd
163+ * number of elements.
164+ *
165+ * @param object A String or, an array of character bytes containing hexidecimal digits
166+ * @return A byte array containing binary data decoded from
167+ * the supplied byte array (representing characters).
168+ * @throws DecoderException Thrown if an odd number of characters is supplied
169+ * to this function or the object is not a String or char[]
170+ * @see #decodeHex(char[])
171+ */
172+ public Object decode (Object object ) throws DecoderException {
173+ try {
174+ char [] charArray = object instanceof String ? ((String ) object ).toCharArray () : (char []) object ;
175+ return decodeHex (charArray );
176+ } catch (ClassCastException e ) {
177+ throw new DecoderException (e .getMessage ());
178+ }
179+ }
180+
181+ /**
182+ * Converts an array of bytes into an array of bytes for the characters representing the
183+ * hexidecimal values of each byte in order. The returned array will be
184+ * double the length of the passed array, as it takes two characters to
185+ * represent any given byte.
186+ *
187+ * @param array a byte[] to convert to Hex characters
188+ * @return A byte[] containing the bytes of the hexidecimal characters
189+ * @see #encodeHex(byte[])
190+ */
191+ public byte [] encode (byte [] array ) {
192+ return new String (encodeHex (array )).getBytes ();
193+ }
170194
171- return out ;
172- }
195+ /**
196+ * Converts a String or an array of bytes into an array of characters representing the
197+ * hexidecimal values of each byte in order. The returned array will be
198+ * double the length of the passed String or array, as it takes two characters to
199+ * represent any given byte.
200+ *
201+ * @param object a String, or byte[] to convert to Hex characters
202+ * @return A char[] containing hexidecimal characters
203+ * @throws EncoderException Thrown if the given object is not a String or byte[]
204+ * @see #encodeHex(byte[])
205+ */
206+ public Object encode (Object object ) throws EncoderException {
207+ try {
208+ byte [] byteArray = object instanceof String ? ((String ) object ).getBytes () : (byte []) object ;
209+ return encodeHex (byteArray );
210+ } catch (ClassCastException e ) {
211+ throw new EncoderException (e .getMessage ());
212+ }
213+ }
173214
174215}
175216
0 commit comments