1 55 package org.jboss.axis.types; 56 57 import org.jboss.axis.utils.JavaUtils; 58 import org.jboss.axis.utils.Messages; 59 60 import java.io.ByteArrayOutputStream ; 61 62 67 public class HexBinary 68 { 69 byte[] m_value = null; 70 71 public HexBinary() 72 { 73 } 74 75 public HexBinary(String string) 76 { 77 m_value = decode(string); 78 } 79 80 public HexBinary(byte[] bytes) 81 { 82 m_value = bytes; 83 } 84 85 public HexBinary(Byte [] bytes) 86 { 87 m_value = new byte[bytes.length]; 88 for (int i = 0; i < bytes.length; i++) 89 m_value[i] = bytes[i].byteValue(); 90 } 91 92 public byte[] getBytes() 93 { 94 return m_value; 95 } 96 97 public String toString() 98 { 99 return encode(m_value); 100 } 101 102 public int hashCode() 103 { 104 return super.hashCode(); 106 } 107 108 public boolean equals(Object object) 109 { 110 String s1 = object.toString(); 112 String s2 = this.toString(); 113 return s1.equals(s2); 114 } 115 116 public static final String ERROR_ODD_NUMBER_OF_DIGITS = 117 Messages.getMessage("oddDigits00"); 118 public static final String ERROR_BAD_CHARACTER_IN_HEX_STRING = 119 Messages.getMessage("badChars01"); 120 121 123 public static final int[] DEC = { 125 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 126 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 127 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 128 00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1, 129 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 130 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 131 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 132 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 133 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 134 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 135 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 136 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 137 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 138 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 139 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 140 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 141 }; 142 143 152 public static byte[] decode(String digits) 153 { 154 155 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 156 for (int i = 0; i < digits.length(); i += 2) 157 { 158 char c1 = digits.charAt(i); 159 if ((i + 1) >= digits.length()) 160 throw new IllegalArgumentException 161 (ERROR_ODD_NUMBER_OF_DIGITS); 162 char c2 = digits.charAt(i + 1); 163 byte b = 0; 164 if ((c1 >= '0') && (c1 <= '9')) 165 b += ((c1 - '0') * 16); 166 else if ((c1 >= 'a') && (c1 <= 'f')) 167 b += ((c1 - 'a' + 10) * 16); 168 else if ((c1 >= 'A') && (c1 <= 'F')) 169 b += ((c1 - 'A' + 10) * 16); 170 else 171 throw new IllegalArgumentException 172 (ERROR_BAD_CHARACTER_IN_HEX_STRING); 173 if ((c2 >= '0') && (c2 <= '9')) 174 b += (c2 - '0'); 175 else if ((c2 >= 'a') && (c2 <= 'f')) 176 b += (c2 - 'a' + 10); 177 else if ((c2 >= 'A') && (c2 <= 'F')) 178 b += (c2 - 'A' + 10); 179 else 180 throw new IllegalArgumentException 181 (ERROR_BAD_CHARACTER_IN_HEX_STRING); 182 baos.write(b); 183 } 184 return (baos.toByteArray()); 185 186 } 187 188 189 195 public static String encode(byte bytes[]) 196 { 197 198 StringBuffer sb = new StringBuffer (bytes.length * 2); 199 for (int i = 0; i < bytes.length; i++) 200 { 201 sb.append(convertDigit((int)(bytes[i] >> 4))); 202 sb.append(convertDigit((int)(bytes[i] & 0x0f))); 203 } 204 return (sb.toString()); 205 } 206 207 213 public static String encode(Byte bytes[]) 214 { 215 return encode((byte[])JavaUtils.convert(bytes, byte[].class)); 216 } 217 218 226 public static int convert2Int(byte[] hex) 227 { 228 230 int len; 233 if (hex.length < 4) return 0; 234 if (DEC[hex[0]] < 0) 235 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 236 len = DEC[hex[0]]; 237 len = len << 4; 238 if (DEC[hex[1]] < 0) 239 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 240 len += DEC[hex[1]]; 241 len = len << 4; 242 if (DEC[hex[2]] < 0) 243 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 244 len += DEC[hex[2]]; 245 len = len << 4; 246 if (DEC[hex[3]] < 0) 247 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 248 len += DEC[hex[3]]; 249 return len; 250 } 251 252 258 private static char convertDigit(int value) 259 { 260 261 value &= 0x0f; 262 if (value >= 10) 263 return ((char)(value - 10 + 'a')); 264 else 265 return ((char)(value + '0')); 266 267 } 268 } 269 | Popular Tags |