1 11 package org.eclipse.debug.internal.ui.views.memory.renderings; 12 13 import java.math.BigInteger ; 14 15 16 19 public class RenderingsUtil { 20 21 public static final int LITTLE_ENDIAN = 0; 22 public static final int BIG_ENDIAN = 1; 23 public static final int ENDIANESS_UNKNOWN = 2; 24 25 33 protected static byte[] fillArray(byte[] array, int size, int endianess) 34 { 35 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 36 { 37 byte[] temp = new byte[size]; 38 39 for (int i=0; i<array.length; i++) 40 { 41 temp[i] = array[i]; 42 } 43 44 for (int i=array.length; i<size; i++) 46 { 47 temp[i] = 0; 48 } 49 50 array = temp; 51 return array; 52 } 53 byte[] temp = new byte[size]; 54 55 for (int i=0; i<size - array.length; i++) 56 { 57 temp[i] = 0; 58 } 59 60 int j=0; 61 for (int i=size - array.length; i<size; i++) 63 { 64 temp[i] = array[j]; 65 j++; 66 } 67 68 array = temp; 69 return array; 70 } 71 72 static public BigInteger convertByteArrayToUnsignedLong(byte[] array, int endianess) 73 { 74 if (array.length < 8) 75 { 76 array = fillArray(array, 8, endianess); 77 } 78 79 BigInteger value = new BigInteger ("0"); if (endianess == RenderingsUtil.LITTLE_ENDIAN) 81 { 82 for (int i=0; i< 8; i++) 83 { 84 byte[] temp = new byte[1]; 85 temp[0] = array[i]; 86 BigInteger b = new BigInteger (temp); 87 b = b.and(new BigInteger ("ff", 16)); b = b.shiftLeft(i*8); 89 value = value.or(b); 90 } 91 } 92 else 93 { 94 for (int i=0; i< 8; i++) 95 { 96 byte[] temp = new byte[1]; 97 temp[0] = array[i]; 98 BigInteger b = new BigInteger (temp); 99 b = b.and(new BigInteger ("ff", 16)); b = b.shiftLeft((7-i)*8); 101 value = value.or(b); 102 } 103 } 104 return value; 105 } 106 107 113 static public long convertByteArrayToLong(byte[] array, int endianess) 114 { 115 if (array.length < 8) 116 { 117 array = fillArray(array, 8, endianess); 118 } 119 120 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 121 { 122 long value = 0; 123 for (int i = 0; i < 8; i++) { 124 long b = array[i]; 125 b &= 0xff; 126 value |= (b << (i * 8)); 127 } 128 return value; 129 } 130 long value = 0; 131 for (int i=0; i< 8; i++) 132 { 133 long b = array[i]; 134 b &= 0xff; 135 value |= (b<<((7-i)*8)); 136 } 137 138 return value; 139 } 140 141 static public BigInteger convertByteArrayToSignedBigInt(byte[] array, int endianess) 142 { 143 if (array.length < 16) 144 { 145 array = fillArray(array, 16, endianess); 146 } 147 148 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 149 { 150 byte[] holder = new byte[16]; 152 int j=15; 153 for (int i=0; i<16; i++, j--) 154 { 155 holder[i] = array[j]; 156 } 157 158 BigInteger value = new BigInteger (holder); 160 return value; 161 } 162 BigInteger value = new BigInteger (array); 163 return value; 164 } 165 166 static public BigInteger convertByteArrayToSignedBigInt(byte[] array, int endianess, int arraySize) 167 { 168 if (array.length < arraySize) 169 { 170 array = fillArray(array, arraySize, endianess); 171 } 172 173 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 174 { 175 byte[] holder = new byte[arraySize]; 177 int j=arraySize-1; 178 for (int i=0; i<arraySize; i++, j--) 179 { 180 holder[i] = array[j]; 181 } 182 183 BigInteger value = new BigInteger (holder); 185 return value; 186 } 187 BigInteger value = new BigInteger (array); 188 return value; 189 } 190 191 static public BigInteger convertByteArrayToUnsignedBigInt(byte[] array, int endianess) 192 { 193 if (array.length < 16) 194 { 195 array = fillArray(array, 16, endianess); 196 } 197 198 BigInteger value = new BigInteger ("0"); if (endianess == RenderingsUtil.LITTLE_ENDIAN) 200 { 201 for (int i=0; i< 16; i++) 202 { 203 byte[] temp = new byte[1]; 204 temp[0] = array[i]; 205 BigInteger b = new BigInteger (temp); 206 b = b.and(new BigInteger ("ff", 16)); b = b.shiftLeft(i*8); 208 value = value.or(b); 209 } 210 } 211 else 212 { 213 for (int i=0; i< 16; i++) 214 { 215 byte[] temp = new byte[1]; 216 temp[0] = array[i]; 217 BigInteger b = new BigInteger (temp); 218 b = b.and(new BigInteger ("ff", 16)); b = b.shiftLeft((15-i)*8); 220 value = value.or(b); 221 } 222 } 223 return value; 224 } 225 226 static public BigInteger convertByteArrayToUnsignedBigInt(byte[] array, int endianess, int arraySize) 227 { 228 if (array.length < arraySize) 229 { 230 array = fillArray(array, arraySize, endianess); 231 } 232 233 BigInteger value = new BigInteger ("0"); if (endianess == RenderingsUtil.LITTLE_ENDIAN) 235 { 236 for (int i=0; i< arraySize; i++) 237 { 238 byte[] temp = new byte[1]; 239 temp[0] = array[i]; 240 BigInteger b = new BigInteger (temp); 241 b = b.and(new BigInteger ("ff", 16)); b = b.shiftLeft(i*8); 243 value = value.or(b); 244 } 245 } 246 else 247 { 248 for (int i=0; i< arraySize; i++) 249 { 250 byte[] temp = new byte[1]; 251 temp[0] = array[i]; 252 BigInteger b = new BigInteger (temp); 253 b = b.and(new BigInteger ("ff", 16)); b = b.shiftLeft((arraySize-1-i)*8); 255 value = value.or(b); 256 } 257 } 258 return value; 259 } 260 261 267 static public int convertByteArrayToInt(byte[] array, int endianess) 268 { 269 if (array.length < 4) 270 { 271 array = fillArray(array, 4, endianess); 272 } 273 274 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 275 { 276 int value = 0; 277 for (int i = 0; i < 4; i++) { 278 int b = array[i]; 279 b &= 0xff; 280 value |= (b << (i * 8)); 281 } 282 return value; 283 } 284 int value = 0; 285 for (int i=0; i< 4; i++) 286 { 287 int b = array[i]; 288 b &= 0xff; 289 value |= (b<<((3-i)*8)); 290 } 291 292 return value; 293 } 294 295 301 static public short convertByteArrayToShort(byte[] array, int endianess) 302 { 303 if (array.length < 2) 304 { 305 array = fillArray(array, 2, endianess); 306 } 307 308 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 309 { 310 short value = 0; 311 for (int i = 0; i < 2; i++) { 312 short b = array[i]; 313 b &= 0xff; 314 value |= (b << (i * 8)); 315 } 316 return value; 317 } 318 short value = 0; 319 for (int i=0; i< 2; i++) 320 { 321 short b = array[i]; 322 b &= 0xff; 323 value |= (b<<((1-i)*8)); 324 } 325 return value; 326 } 327 328 334 static public byte[] convertBigIntegerToByteArray(BigInteger i, int endianess) 335 { 336 byte buf[]=new byte[16]; 337 338 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 339 { 340 for (int j=0; j<16; j++) 341 { 342 BigInteger x = i.shiftRight(j*8); 343 buf[j] = x.byteValue(); 344 } 345 return buf; 346 } 347 for (int j=15; j>=0; j--) 348 { 349 BigInteger x = i.shiftRight((15-j)*8); 350 buf[j] = x.byteValue(); 351 } 352 return buf; 353 } 354 355 static public byte[] convertSignedBigIntToByteArray(BigInteger i, int endianess, int arraySize) 356 { 357 byte buf[]=new byte[arraySize]; 358 359 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 360 { 361 for (int j=0; j<arraySize; j++) 362 { 363 BigInteger x = i.shiftRight(j*8); 364 buf[j] = x.byteValue(); 365 } 366 return buf; 367 } 368 for (int j=arraySize-1; j>=0; j--) 369 { 370 BigInteger x = i.shiftRight((arraySize-1-j)*8); 371 buf[j] = x.byteValue(); 372 } 373 return buf; 374 } 375 376 382 static public byte[] convertUnsignedBigIntegerToByteArray(BigInteger i, int endianess) 383 { 384 byte buf[]=new byte[32]; 385 386 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 387 { 388 for (int j=0; j<32; j++) 389 { 390 BigInteger x = i.shiftRight(j*8); 391 buf[j] = x.byteValue(); 392 } 393 return buf; 394 } 395 for (int j=31; j>=0; j--) 396 { 397 BigInteger x = i.shiftRight((31-j)*8); 398 buf[j] = x.byteValue(); 399 } 400 return buf; 401 } 402 403 static public byte[] convertUnsignedBigIntToByteArray(BigInteger i, int endianess, int arraySize) 404 { 405 byte buf[]=new byte[arraySize*2]; 406 407 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 408 { 409 for (int j=0; j<arraySize*2; j++) 410 { 411 BigInteger x = i.shiftRight(j*8); 412 buf[j] = x.byteValue(); 413 } 414 return buf; 415 } 416 for (int j=(arraySize*2)-1; j>=0; j--) 417 { 418 BigInteger x = i.shiftRight(((arraySize*2)-1-j)*8); 419 buf[j] = x.byteValue(); 420 } 421 return buf; 422 } 423 424 430 static public byte[] convertLongToByteArray(long i, int endianess) 431 { 432 byte buf[]=new byte[8]; 433 434 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 435 { 436 for (int j=0; j<8; j++) 437 { 438 buf[j] = new Long (i>>j*8).byteValue(); 439 } 440 return buf; 441 } 442 for (int j=7; j>=0; j--) 443 { 444 buf[j] = new Long (i>>(7-j)*8).byteValue(); 445 } 446 return buf; 447 } 448 449 455 static public byte[] convertIntToByteArray(int i, int endianess) 456 { 457 byte buf[]=new byte[4]; 458 459 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 460 { 461 for (int j=0; j<4; j++) 462 { 463 buf[j] = new Integer (i>>j*8).byteValue(); 464 } 465 return buf; 466 } 467 for (int j=3; j>=0; j--) 468 { 469 buf[j] = new Integer (i>>(3-j)*8).byteValue(); 470 } 471 return buf; 472 } 473 474 480 static public byte[] convertShortToByteArray(short i, int endianess) 481 { 482 byte buf[]=new byte[2]; 483 484 if (endianess == RenderingsUtil.LITTLE_ENDIAN) 485 { 486 for (short j=0; j<2; j++) 487 { 488 buf[j] = new Integer (i>>j*8).byteValue(); 489 } 490 return buf; 491 } 492 for (short j=1; j>=0; j--) 493 { 494 buf[j] = new Integer (i>>(1-j)*8).byteValue(); 495 } 496 return buf; 497 } 498 499 504 static public String convertByteArrayToHexString(byte[] byteArray) 505 { 506 StringBuffer strBuffer = new StringBuffer (); 507 char charArray[]; 508 509 for (int i=0; i<byteArray.length;i ++) 510 { 511 charArray = RenderingsUtil.convertByteToCharArray(byteArray[i]); 512 strBuffer.append(charArray); 513 } 514 515 return strBuffer.toString(); 516 } 517 518 static public char[] convertByteToCharArray(byte aByte) 519 { 520 char charArray[] = new char[2]; 521 int val = aByte; 522 if (val<0) val += 256; 523 charArray[0] = Character.forDigit(val/16, 16); 524 charArray[1] = Character.forDigit(val%16, 16); 525 526 return charArray; 527 } 528 529 537 public static byte[] convertHexStringToByteArray(String str, int numBytes, int numCharsPerByte) throws NumberFormatException 538 { 539 if (str.length() == 0) 540 return null; 541 542 StringBuffer buf = new StringBuffer (str); 543 544 int requiredPadding = numBytes * numCharsPerByte - str.length(); 546 while (requiredPadding > 0) { 547 buf.insert(0, "0"); requiredPadding--; 549 } 550 551 byte[] bytes = new byte[numBytes]; 552 str = buf.toString(); 553 554 for (int i=0; i<bytes.length; i++) 556 { 557 String oneByte = str.substring(i*2, i*2+2); 559 560 Integer number = Integer.valueOf(oneByte, 16); 561 if (number.compareTo(Integer.valueOf(Byte.toString(Byte.MAX_VALUE))) > 0) 562 { 563 int temp = number.intValue(); 564 temp = temp - 256; 565 566 String tempStr = Integer.toString(temp); 567 568 Byte myByte = Byte.valueOf(tempStr); 569 bytes[i] = myByte.byteValue(); 570 } 571 else 572 { 573 Byte myByte = Byte.valueOf(oneByte, 16); 574 bytes[i] = myByte.byteValue(); 575 } 576 } 577 578 return bytes; 579 } 580 } 581 | Popular Tags |