View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */ 
16  
17  package org.apache.asn1.codec.binary;
18  
19  import org.apache.asn1.codec.BinaryDecoder;
20  import org.apache.asn1.codec.BinaryEncoder;
21  import org.apache.asn1.codec.DecoderException;
22  import org.apache.asn1.codec.EncoderException;
23  
24  /***
25   * Hex encoder and decoder.
26   * 
27   * @since 1.1
28   * @author Apache Software Foundation
29   * @version $Id: Hex.java,v 1.14 2004/07/28 19:27:45 ggregory Exp $
30   */
31  public class Hex implements BinaryEncoder, BinaryDecoder {
32  
33      /*** 
34       * Used to build output as Hex 
35       */
36      private static final char[] DIGITS = {
37          '0', '1', '2', '3', '4', '5', '6', '7',
38             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
39      };
40  
41      /***
42       * Converts an array of characters representing hexidecimal values into an
43       * array of bytes of those same values. The returned array will be half the
44       * length of the passed array, as it takes two characters to represent any
45       * given byte. An exception is thrown if the passed char array has an odd
46       * number of elements.
47       * 
48       * @param data An array of characters containing hexidecimal digits
49       * @return A byte array containing binary data decoded from
50       *         the supplied char array.
51       * @throws DecoderException Thrown if an odd number or illegal of characters 
52       *         is supplied
53       */
54      public static byte[] decodeHex(char[] data) throws DecoderException {
55  
56          int len = data.length;
57  
58          if ((len & 0x01) != 0) {
59              throw new DecoderException("Odd number of characters.");
60          }
61  
62          byte[] out = new byte[len >> 1];
63  
64          // two characters form the hex value.
65          for (int i = 0, j = 0; j < len; i++) {
66              int f = toDigit(data[j], j) << 4;
67              j++;
68              f = f | toDigit(data[j], j);
69              j++;
70              out[i] = (byte) (f & 0xFF);
71          }
72  
73          return out;
74      }
75  
76      /***
77       * Converts a hexadecimal character to an integer.
78       *  
79       * @param ch A character to convert to an integer digit
80       * @param index The index of the character in the source
81       * @return An integer
82       * @throws DecoderException Thrown if ch is an illegal hex character
83       */
84      protected static int toDigit(char ch, int index) throws DecoderException {
85          int digit = Character.digit(ch, 16);
86          if (digit == -1) {
87              throw new DecoderException("Illegal hexadecimal charcter " + ch + " at index " + index);
88          }
89          return digit;
90      }
91  
92      /***
93       * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order.
94       * The returned array will be double the length of the passed array, as it takes two characters to represent any
95       * given byte.
96       * 
97       * @param data
98       *                  a byte[] to convert to Hex characters
99       * @return A char[] containing hexidecimal characters
100      */
101     public static char[] encodeHex(byte[] data) {
102 
103         int l = data.length;
104 
105            char[] out = new char[l << 1];
106 
107            // two characters form the hex value.
108            for (int i = 0, j = 0; i < l; i++) {
109                out[j++] = DIGITS[(0xF0 & data[i]) >>> 4 ];
110                out[j++] = DIGITS[ 0x0F & data[i] ];
111            }
112 
113            return out;
114     }
115 	
116     /***
117      * Converts an array of character bytes representing hexidecimal values into an
118      * array of bytes of those same values. The returned array will be half the
119      * length of the passed array, as it takes two characters to represent any
120      * given byte. An exception is thrown if the passed char array has an odd
121      * number of elements.
122      * 
123      * @param array An array of character bytes containing hexidecimal digits
124      * @return A byte array containing binary data decoded from
125      *         the supplied byte array (representing characters).
126      * @throws DecoderException Thrown if an odd number of characters is supplied
127      *                   to this function
128      * @see #decodeHex(char[])
129      */
130 	public byte[] decode(byte[] array) throws DecoderException {
131 		return decodeHex(new String(array).toCharArray());
132 	}
133 	
134     /***
135      * Converts a String or an array of character bytes representing hexidecimal values into an
136      * array of bytes of those same values. The returned array will be half the
137      * length of the passed String or array, as it takes two characters to represent any
138      * given byte. An exception is thrown if the passed char array has an odd
139      * number of elements.
140      * 
141      * @param object A String or, an array of character bytes containing hexidecimal digits
142      * @return A byte array containing binary data decoded from
143      *         the supplied byte array (representing characters).
144      * @throws DecoderException Thrown if an odd number of characters is supplied
145      *                   to this function or the object is not a String or char[]
146      * @see #decodeHex(char[])
147      */
148 	public Object decode(Object object) throws DecoderException {
149 		try {
150             char[] charArray = object instanceof String ? ((String) object).toCharArray() : (char[]) object;
151 		    return decodeHex(charArray);
152 		} catch (ClassCastException e) {
153 		    throw new DecoderException(e.getMessage());
154 		}
155 	}
156 	
157     /***
158      * Converts an array of bytes into an array of bytes for the characters representing the
159      * hexidecimal values of each byte in order. The returned array will be
160      * double the length of the passed array, as it takes two characters to
161      * represent any given byte.
162      *
163      * @param array a byte[] to convert to Hex characters
164      * @return A byte[] containing the bytes of the hexidecimal characters
165      * @see #encodeHex(byte[])
166      */
167 	public byte[] encode(byte[] array) {
168 		return new String(encodeHex(array)).getBytes();
169 	}
170 
171     /***
172      * Converts a String or an array of bytes into an array of characters representing the
173      * hexidecimal values of each byte in order. The returned array will be
174      * double the length of the passed String or array, as it takes two characters to
175      * represent any given byte.
176      *
177      * @param object a String, or byte[] to convert to Hex characters
178      * @return A char[] containing hexidecimal characters
179      * @throws EncoderException Thrown if the given object is not a String or byte[]
180      * @see #encodeHex(byte[])
181      */
182 	public Object encode(Object object) throws EncoderException {	
183 		try {
184             byte[] byteArray = object instanceof String ? ((String) object).getBytes() : (byte[]) object;
185 			return encodeHex(byteArray);
186 		} catch (ClassCastException e) {
187 			throw new EncoderException(e.getMessage());
188 		}
189 	}
190 
191 }
192