1 55 56 package org.apache.bsf.util.event.generator; 57 58 67 public class ByteUtility 68 { 69 public static byte[] addBytes(byte[] array,byte[] value) 70 { 71 if( null != array ) 72 { 73 byte newarray[] = new byte[array.length + value.length]; 74 System.arraycopy(array,0,newarray,0,array.length); 75 System.arraycopy(value,0,newarray,array.length,value.length); 76 array = newarray; 77 } 78 else 79 { 80 array = value; 81 } 82 return array; 83 } 84 public static byte[] addBytes(byte[] array, byte value) 85 { 86 if( null != array ) 87 { 88 byte newarray[] = new byte[array.length + 1]; 89 System.arraycopy(array,0,newarray,0,array.length); 90 newarray[newarray.length-1] = value; 91 array = newarray; 92 } 93 else 94 { 95 array = new byte[1]; 96 array[0] = value; 97 } 98 return array; 99 } 100 public static byte[] addBytes(byte[] array, int value) 101 { 102 if( null != array ) 103 { 104 byte newarray[] = new byte[array.length + 3]; 105 System.arraycopy(array,0,newarray,0,array.length); 106 newarray[newarray.length-3] = (byte) (( value >> 16 ) & 0xFF); 107 newarray[newarray.length-2] = (byte) (( value >> 8 ) & 0xFF); 108 newarray[newarray.length-1] = (byte) ( value & 0xFF); 109 array = newarray; 110 } 111 else 112 { 113 array = new byte[3]; 114 array[0] = (byte) (( value >> 16 ) & 0xFF); 115 array[1] = (byte) (( value >> 8 ) & 0xFF); 116 array[2] = (byte) ( value & 0xFF); 117 } 118 return array; 119 } 120 public static byte[] addBytes(byte[] array, long value) 121 { 122 if( null != array ) 123 { 124 byte newarray[] = new byte[array.length + 4]; 125 System.arraycopy(array,0,newarray,0,array.length); 126 newarray[newarray.length-4] = (byte) (( value >> 24 ) & 0xFF); 127 newarray[newarray.length-3] = (byte) (( value >> 16 ) & 0xFF); 128 newarray[newarray.length-2] = (byte) (( value >> 8 ) & 0xFF); 129 newarray[newarray.length-1] = (byte) ( value & 0xFF); 130 array = newarray; 131 } 132 else 133 { 134 array = new byte[4]; 135 array[0] = (byte) (( value >> 24 ) & 0xFF); 136 array[1] = (byte) (( value >> 16 ) & 0xFF); 137 array[2] = (byte) (( value >> 8 ) & 0xFF); 138 array[3] = (byte) (value & 0xFF); 139 } 140 return array; 141 } 142 public static byte[] addBytes(byte[] array,String value) 143 { 144 if( null != value ) 145 { 146 if( null != array) 147 { 148 byte newarray[] = new byte[array.length + value.length()]; 149 System.arraycopy(array,0,newarray,0,array.length); 150 System.arraycopy(value.getBytes(),0,newarray,array.length,value.length()); 151 array = newarray; 152 } 153 else 154 { 155 array = value.getBytes(); 156 } 157 } 158 return array; 159 } 160 public static byte[] addBytes(byte[] array, short value) 161 { 162 if( null != array) 163 { 164 byte newarray[] = new byte[array.length + 2]; 165 System.arraycopy(array,0,newarray,0,array.length); 166 newarray[newarray.length-2] = (byte) (( value >> 8 ) & 0xFF); 167 newarray[newarray.length-1] = (byte) ( value & 0xFF); 168 array = newarray; 169 } 170 else 171 { 172 array = new byte[2]; 173 array[0] = (byte) (( value >> 8 ) & 0xFF); 174 array[1] = (byte) ( value & 0xFF); 175 } 176 return array; 177 } 178 public static double byteArrayToDouble(byte high[], byte low[]) 179 { 180 double temp = 0; 181 temp += (((long)high[0]) & 0xFF) << 56; 183 temp += (((long)high[1]) & 0xFF) << 48; 184 temp += (((long)high[2]) & 0xFF) << 40; 185 temp += (((long)high[3]) & 0xFF) << 32; 186 temp += (((long)low[0]) & 0xFF) << 24; 188 temp += (((long)low[1]) & 0xFF) << 16; 189 temp += (((long)low[2]) & 0xFF) << 8; 190 temp += (((long)low[3]) & 0xFF); 191 return temp; 192 } 193 public static double byteArrayToDounle(byte value[]) 194 { 195 byte high[] = new byte[4]; 196 byte low[] = new byte[4]; 197 high[0] = value[0]; 198 high[1] = value[1]; 199 high[2] = value[2]; 200 high[3] = value[3]; 201 low[0] = value[4]; 202 low[1] = value[5]; 203 low[2] = value[6]; 204 low[3] = value[7]; 205 return byteArrayToDouble(high,low); 206 } 207 public static float byteArrayToFloat(byte value[]) 208 { 209 float temp = 0; 210 temp += (((int)value[0]) & 0xFF) << 24; 211 temp += (((int)value[1]) & 0xFF) << 16; 212 temp += (((int)value[2]) & 0xFF) << 8; 213 temp += (((int)value[3]) & 0xFF); 214 return temp; 215 } 216 public static int byteArrayToInt(byte value[]) 217 { 218 int temp = 0; 219 temp += (((int)value[0]) & 0xFF) << 24; 220 temp += (((int)value[1]) & 0xFF) << 16; 221 temp += (((int)value[2]) & 0xFF) << 8; 222 temp += (((int)value[3]) & 0xFF); 223 return temp; 224 } 225 public static long byteArrayToLong(byte value[]) 226 { 227 byte high[] = new byte[4]; 228 byte low[] = new byte[4]; 229 high[0] = value[0]; 230 high[1] = value[1]; 231 high[2] = value[2]; 232 high[3] = value[3]; 233 low[0] = value[4]; 234 low[1] = value[5]; 235 low[2] = value[6]; 236 low[3] = value[7]; 237 return byteArrayToLong(high,low); 238 } 239 public static long byteArrayToLong(byte high[], byte low[]) 240 { 241 long temp = 0; 242 temp += (((long)high[0]) & 0xFF) << 56; 244 temp += (((long)high[1]) & 0xFF) << 48; 245 temp += (((long)high[2]) & 0xFF) << 40; 246 temp += (((long)high[3]) & 0xFF) << 32; 247 temp += (((long)low[0]) & 0xFF) << 24; 249 temp += (((long)low[1]) & 0xFF) << 16; 250 temp += (((long)low[2]) & 0xFF) << 8; 251 temp += (((long)low[3]) & 0xFF); 252 return temp; 253 } 254 public static short byteArrayToShort(byte value[]) 256 { 257 short temp = 0; 258 temp += (((int)value[0]) & 0xFF) << 8; 259 temp += (((int)value[1]) & 0xFF); 260 return temp; 261 } 262 public static String byteToHexString(byte value) 263 { 264 String temp = null; 265 266 switch( (value & 0xF0) >> 4 ) 267 { 268 case 0: 269 temp = "0"; 270 break; 271 case 1: 272 temp = "1"; 273 break; 274 case 2: 275 temp = "2"; 276 break; 277 case 3: 278 temp = "3"; 279 break; 280 case 4: 281 temp = "4"; 282 break; 283 case 5: 284 temp = "5"; 285 break; 286 case 6: 287 temp = "6"; 288 break; 289 case 7: 290 temp = "7"; 291 break; 292 case 8: 293 temp = "8"; 294 break; 295 case 9: 296 temp = "9"; 297 break; 298 case 10: 299 temp = "A"; 300 break; 301 case 11: 302 temp = "B"; 303 break; 304 case 12: 305 temp = "C"; 306 break; 307 case 13: 308 temp = "D"; 309 break; 310 case 14: 311 temp = "E"; 312 break; 313 case 15: 314 temp = "F"; 315 break; 316 } 317 switch( (value & 0x0F) ) 318 { 319 case 0: 320 temp += "0"; 321 break; 322 case 1: 323 temp += "1"; 324 break; 325 case 2: 326 temp += "2"; 327 break; 328 case 3: 329 temp += "3"; 330 break; 331 case 4: 332 temp += "4"; 333 break; 334 case 5: 335 temp += "5"; 336 break; 337 case 6: 338 temp += "6"; 339 break; 340 case 7: 341 temp += "7"; 342 break; 343 case 8: 344 temp += "8"; 345 break; 346 case 9: 347 temp += "9"; 348 break; 349 case 10: 350 temp += "A"; 351 break; 352 case 11: 353 temp += "B"; 354 break; 355 case 12: 356 temp += "C"; 357 break; 358 case 13: 359 temp += "D"; 360 break; 361 case 14: 362 temp += "E"; 363 break; 364 case 15: 365 temp += "F"; 366 break; 367 } 368 return temp; 369 } 370 } 371 | Popular Tags |