1 16 package org.apache.axis.types ; 17 18 import org.apache.axis.utils.Messages; 19 20 import java.io.ByteArrayOutputStream ; 21 22 27 public class HexBinary extends Object implements java.io.Serializable { 28 29 byte[] m_value = null; 30 31 public HexBinary() { 32 } 33 34 public HexBinary(String string){ 35 m_value = decode(string); 36 } 37 38 public HexBinary(byte[] bytes){ 39 m_value = bytes; 40 } 41 42 public byte[] getBytes(){ 43 return m_value; 44 } 45 46 public String toString(){ 47 return encode(m_value); 48 } 49 50 public int hashCode(){ 51 return super.hashCode(); 53 } 54 55 public boolean equals(Object object){ 56 String s1 = object.toString(); 58 String s2 = this.toString(); 59 return s1.equals(s2); 60 } 61 62 public static final String ERROR_ODD_NUMBER_OF_DIGITS = 63 Messages.getMessage("oddDigits00"); 64 public static final String ERROR_BAD_CHARACTER_IN_HEX_STRING = 65 Messages.getMessage("badChars01"); 66 67 69 public static final int[] DEC = { 71 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 74 00, 01, 02, 03, 04, 05, 06, 07, 8, 9, -1, -1, -1, -1, -1, -1, 75 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 76 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 77 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, 78 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 80 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 81 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 82 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 83 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 84 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 85 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 86 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 87 }; 88 89 99 public static byte[] decode(String digits) { 100 101 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 102 for (int i = 0; i < digits.length(); i += 2) { 103 char c1 = digits.charAt(i); 104 if ((i+1) >= digits.length()) 105 throw new IllegalArgumentException 106 (ERROR_ODD_NUMBER_OF_DIGITS); 107 char c2 = digits.charAt(i + 1); 108 byte b = 0; 109 if ((c1 >= '0') && (c1 <= '9')) 110 b += ((c1 - '0') * 16); 111 else if ((c1 >= 'a') && (c1 <= 'f')) 112 b += ((c1 - 'a' + 10) * 16); 113 else if ((c1 >= 'A') && (c1 <= 'F')) 114 b += ((c1 - 'A' + 10) * 16); 115 else 116 throw new IllegalArgumentException 117 (ERROR_BAD_CHARACTER_IN_HEX_STRING); 118 if ((c2 >= '0') && (c2 <= '9')) 119 b += (c2 - '0'); 120 else if ((c2 >= 'a') && (c2 <= 'f')) 121 b += (c2 - 'a' + 10); 122 else if ((c2 >= 'A') && (c2 <= 'F')) 123 b += (c2 - 'A' + 10); 124 else 125 throw new IllegalArgumentException 126 (ERROR_BAD_CHARACTER_IN_HEX_STRING); 127 baos.write(b); 128 } 129 return (baos.toByteArray()); 130 131 } 132 133 134 140 public static String encode(byte bytes[]) { 141 142 StringBuffer sb = new StringBuffer (bytes.length * 2); 143 for (int i = 0; i < bytes.length; i++) { 144 sb.append(convertDigit((int) (bytes[i] >> 4))); 145 sb.append(convertDigit((int) (bytes[i] & 0x0f))); 146 } 147 return (sb.toString()); 148 149 } 150 151 160 public static int convert2Int( byte[] hex ) { 161 163 int len; 166 if(hex.length < 4 ) return 0; 167 if( DEC[hex[0]]<0 ) 168 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 169 len = DEC[hex[0]]; 170 len = len << 4; 171 if( DEC[hex[1]]<0 ) 172 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 173 len += DEC[hex[1]]; 174 len = len << 4; 175 if( DEC[hex[2]]<0 ) 176 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 177 len += DEC[hex[2]]; 178 len = len << 4; 179 if( DEC[hex[3]]<0 ) 180 throw new IllegalArgumentException (ERROR_BAD_CHARACTER_IN_HEX_STRING); 181 len += DEC[hex[3]]; 182 return len; 183 } 184 185 191 private static char convertDigit(int value) { 192 193 value &= 0x0f; 194 if (value >= 10) 195 return ((char) (value - 10 + 'a')); 196 else 197 return ((char) (value + '0')); 198 199 } 200 } 201 | Popular Tags |