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.commons.codec.binary; 18 19 import org.apache.commons.codec.BinaryDecoder; 20 import org.apache.commons.codec.BinaryEncoder; 21 import org.apache.commons.codec.DecoderException; 22 import org.apache.commons.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.13 2004/04/18 18:22:33 ggregory Exp $ 30 * 31 * @deprecated Please use {@link java.net.URL#openConnection} instead. 32 * Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a> 33 * for further details. 34 */ 35 @Deprecated 36 public class Hex implements BinaryEncoder, BinaryDecoder { 37 38 /** 39 * Used building output as Hex 40 */ 41 private static final char[] DIGITS = { 42 '0', '1', '2', '3', '4', '5', '6', '7', 43 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 44 }; 45 46 /** 47 * Converts an array of characters representing hexidecimal values into an 48 * array of bytes of those same values. The returned array will be half the 49 * length of the passed array, as it takes two characters to represent any 50 * given byte. An exception is thrown if the passed char array has an odd 51 * number of elements. 52 * 53 * @param data An array of characters containing hexidecimal digits 54 * @return A byte array containing binary data decoded from 55 * the supplied char array. 56 * @throws DecoderException Thrown if an odd number or illegal of characters 57 * is supplied 58 */ decodeHex(char[] data)59 public static byte[] decodeHex(char[] data) throws DecoderException { 60 61 int len = data.length; 62 63 if ((len & 0x01) != 0) { 64 throw new DecoderException("Odd number of characters."); 65 } 66 67 byte[] out = new byte[len >> 1]; 68 69 // two characters form the hex value. 70 for (int i = 0, j = 0; j < len; i++) { 71 int f = toDigit(data[j], j) << 4; 72 j++; 73 f = f | toDigit(data[j], j); 74 j++; 75 out[i] = (byte) (f & 0xFF); 76 } 77 78 return out; 79 } 80 81 /** 82 * Converts a hexadecimal character to an integer. 83 * 84 * @param ch A character to convert to an integer digit 85 * @param index The index of the character in the source 86 * @return An integer 87 * @throws DecoderException Thrown if ch is an illegal hex character 88 */ toDigit(char ch, int index)89 protected static int toDigit(char ch, int index) throws DecoderException { 90 int digit = Character.digit(ch, 16); 91 if (digit == -1) { 92 throw new DecoderException("Illegal hexadecimal charcter " + ch + " at index " + index); 93 } 94 return digit; 95 } 96 97 /** 98 * Converts an array of bytes into an array of characters representing the hexidecimal values of each byte in order. 99 * The returned array will be double the length of the passed array, as it takes two characters to represent any 100 * given byte. 101 * 102 * @param data 103 * a byte[] to convert to Hex characters 104 * @return A char[] containing hexidecimal characters 105 */ encodeHex(byte[] data)106 public static char[] encodeHex(byte[] data) { 107 108 int l = data.length; 109 110 char[] out = new char[l << 1]; 111 112 // two characters form the hex value. 113 for (int i = 0, j = 0; i < l; i++) { 114 out[j++] = DIGITS[(0xF0 & data[i]) >>> 4 ]; 115 out[j++] = DIGITS[ 0x0F & data[i] ]; 116 } 117 118 return out; 119 } 120 121 /** 122 * Converts an array of character bytes representing hexidecimal values into an 123 * array of bytes of those same values. The returned array will be half the 124 * length of the passed array, as it takes two characters to represent any 125 * given byte. An exception is thrown if the passed char array has an odd 126 * number of elements. 127 * 128 * @param array An array of character bytes containing hexidecimal digits 129 * @return A byte array containing binary data decoded from 130 * the supplied byte array (representing characters). 131 * @throws DecoderException Thrown if an odd number of characters is supplied 132 * to this function 133 * @see #decodeHex(char[]) 134 */ decode(byte[] array)135 public byte[] decode(byte[] array) throws DecoderException { 136 return decodeHex(new String(array).toCharArray()); 137 } 138 139 /** 140 * Converts a String or an array of character bytes representing hexidecimal values into an 141 * array of bytes of those same values. The returned array will be half the 142 * length of the passed String or array, as it takes two characters to represent any 143 * given byte. An exception is thrown if the passed char array has an odd 144 * number of elements. 145 * 146 * @param object A String or, an array of character bytes containing hexidecimal digits 147 * @return A byte array containing binary data decoded from 148 * the supplied byte array (representing characters). 149 * @throws DecoderException Thrown if an odd number of characters is supplied 150 * to this function or the object is not a String or char[] 151 * @see #decodeHex(char[]) 152 */ decode(Object object)153 public Object decode(Object object) throws DecoderException { 154 try { 155 char[] charArray = object instanceof String ? ((String) object).toCharArray() : (char[]) object; 156 return decodeHex(charArray); 157 } catch (ClassCastException e) { 158 throw new DecoderException(e.getMessage()); 159 } 160 } 161 162 /** 163 * Converts an array of bytes into an array of bytes for the characters representing the 164 * hexidecimal values of each byte in order. The returned array will be 165 * double the length of the passed array, as it takes two characters to 166 * represent any given byte. 167 * 168 * @param array a byte[] to convert to Hex characters 169 * @return A byte[] containing the bytes of the hexidecimal characters 170 * @see #encodeHex(byte[]) 171 */ encode(byte[] array)172 public byte[] encode(byte[] array) { 173 return new String(encodeHex(array)).getBytes(); 174 } 175 176 /** 177 * Converts a String or an array of bytes into an array of characters representing the 178 * hexidecimal values of each byte in order. The returned array will be 179 * double the length of the passed String or array, as it takes two characters to 180 * represent any given byte. 181 * 182 * @param object a String, or byte[] to convert to Hex characters 183 * @return A char[] containing hexidecimal characters 184 * @throws EncoderException Thrown if the given object is not a String or byte[] 185 * @see #encodeHex(byte[]) 186 */ encode(Object object)187 public Object encode(Object object) throws EncoderException { 188 try { 189 byte[] byteArray = object instanceof String ? ((String) object).getBytes() : (byte[]) object; 190 return encodeHex(byteArray); 191 } catch (ClassCastException e) { 192 throw new EncoderException(e.getMessage()); 193 } 194 } 195 196 } 197 198