1 11 package org.eclipse.jdi.internal; 12 13 14 import java.io.PrintWriter ; 15 import java.util.ArrayList ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 public class VerboseWriter { 20 21 public static final int VERBOSE_DESCRIPTION_LENGTH = 21; 22 23 public static final int VERBOSE_HEX_BYTES_PER_LINE = 16; 24 25 public static final int VERBOSE_HEX_WIDTH = 16*3+2; 26 27 28 int fExtraVerboseLines = 0; 29 30 31 private PrintWriter fOutput; 32 33 private List fLineBuffer; 34 35 private int fPosition; 36 37 private boolean fNewLine = true; 38 39 43 public VerboseWriter(PrintWriter out) { 44 fOutput = out; 45 fLineBuffer = new ArrayList (); 46 fPosition = 0; 47 fLineBuffer.add(new StringBuffer ()); 48 } 49 50 55 public void println() { 56 while (fExtraVerboseLines > 0) { 57 fExtraVerboseLines--; 58 markLn(); 59 } 60 61 markLn(); 62 } 63 64 67 public void println(String description, byte value) { 68 printDescription(description); 69 printHex(value); 70 println(); 71 } 72 73 76 public void println(String description, short value) { 77 printDescription(description); 78 printHex(value); 79 println(); 80 } 81 82 85 public void println(String description, int value) { 86 printDescription(description); 87 printHex(value); 88 println(); 89 } 90 91 94 public void println(String description, long value) { 95 printDescription(description); 96 printHex(value); 97 println(); 98 } 99 100 103 public void println(String description, byte value, Map valueToString) { 104 printDescription(description); 105 printHex(value); 106 printValue(value, valueToString); 107 println(); 108 } 109 110 113 public void println(String description, short value, Map valueToString) { 114 printDescription(description); 115 printHex(value); 116 printValue(value, valueToString); 117 println(); 118 } 119 120 123 public void println(String description, int value, Map valueToString) { 124 printDescription(description); 125 printHex(value); 126 printValue(value, valueToString); 127 println(); 128 } 129 130 133 public void println(String description, byte value, String [] bitNames) { 134 printDescription(description); 135 printHex(value); 136 printValue(value, bitNames); 137 println(); 138 } 139 140 143 public void println(String description, short value, String [] bitNames) { 144 printDescription(description); 145 printHex(value); 146 printValue(value, bitNames); 147 println(); 148 } 149 150 153 public void println(String description, int value, String [] bitNames) { 154 printDescription(description); 155 printHex(value); 156 printValue(value, bitNames); 157 println(); 158 } 159 160 163 public void println(String description, String value) { 164 printDescription(description); 165 printHex(value); 166 print(value); 167 println(); 168 } 169 170 173 public void println(String description, boolean value) { 174 printDescription(description); 175 printHex(value); 176 print(Boolean.valueOf(value).toString()); 177 println(); 178 } 179 180 183 public void println(String description, char value) { 184 printDescription(description); 185 printHex(value); 186 print(value); 187 println(); 188 } 189 190 193 public void println(String description, double value) { 194 printDescription(description); 195 printHex(value); 196 print(new Double (value).toString()); 197 println(); 198 } 199 200 203 public void println(String description, float value) { 204 printDescription(description); 205 printHex(value); 206 print(new Float (value).toString()); 207 println(); 208 } 209 210 213 public void println(String description, byte[] value) { 214 printDescription(description); 215 printHex(value); 216 println(); 217 } 218 219 222 public void printWidth(String str, int width) { 223 print(str); 224 int spaces = width - str.length(); 225 if (spaces > 0) { 226 for (int i = 0; i < spaces; i++) { 227 print(' '); 228 } 229 } 230 } 231 232 235 public void printDescription(String str) { 236 printWidth(str, VERBOSE_DESCRIPTION_LENGTH); 237 } 238 239 242 public void printHexSubstitution(String str) { 243 print(' '); 245 printWidth(str, VERBOSE_HEX_WIDTH - 1); 246 } 247 248 251 private static void appendHexByte(byte b, char[] buffer, int pos) { 252 int count = 2; 253 254 int abspos = 3*pos; 255 buffer[abspos] = ' '; 256 do { 257 int t = b & 15; 258 if (t > 9) { 259 t = t - 10 + 'a'; 260 } else { 261 t += '0'; 262 } 263 buffer[count-- + abspos] = (char) t; 264 b >>>= 4; 265 } while (count > 0); 266 } 267 268 271 private static void appendHexSpaces(char[] buffer, int pos) { 272 for (int i = 3*pos; i <= VERBOSE_HEX_WIDTH - 3; i+=3) { 273 buffer[i] = ' '; 274 buffer[i+1] = ' '; 275 buffer[i+2] = ' '; 276 } 277 278 buffer[VERBOSE_HEX_WIDTH - 1] = ' '; 280 buffer[VERBOSE_HEX_WIDTH - 2] = ' '; 281 } 282 283 286 public void printHex(byte b) { 287 char buffer[] = new char[VERBOSE_HEX_WIDTH]; 288 appendHexByte(b, buffer, 0); 289 appendHexSpaces(buffer, 1); 290 print(buffer); 291 } 292 293 296 public void printHex(short s) { 297 char buffer[] = new char[VERBOSE_HEX_WIDTH]; 298 for (int i = 1; i >= 0; i--) 299 appendHexByte((byte)(s >>> i*8), buffer, 1 - i); 300 appendHexSpaces(buffer, 2); 301 print(buffer); 302 } 303 304 307 public void printHex(int integer) { 308 char buffer[] = new char[VERBOSE_HEX_WIDTH]; 309 for (int i = 3; i >= 0; i--) 310 appendHexByte((byte)(integer >>> i*8), buffer, 3 - i); 311 appendHexSpaces(buffer, 4); 312 print(buffer); 313 } 314 315 318 public void printHex(long l) { 319 char buffer[] = new char[VERBOSE_HEX_WIDTH]; 320 for (int i = 7; i >= 0; i--) 321 appendHexByte((byte)(l >>> i*8), buffer, 7 - i); 322 appendHexSpaces(buffer, 8); 323 print(buffer); 324 } 325 326 329 public void printHex(boolean b) { 330 printHexSubstitution("<boolean>"); } 332 333 336 public void printHex(char c) { 337 printHexSubstitution("<char>"); } 339 340 343 public void printHex(double d) { 344 printHexSubstitution("<double>"); } 346 347 350 public void printHex(float f) { 351 printHexSubstitution("<float>"); } 353 354 357 public void printHex(String str) { 358 printHexSubstitution("<string>"); } 360 361 367 public void printHex(byte[] bytes) { 368 int startPosition = position(); 369 char linebuf[] = new char[VERBOSE_HEX_WIDTH]; 370 int extraLines = 0; 371 int byteOnLine = 0; 372 373 for (int i = 0; i < bytes.length; i++) { 374 if (byteOnLine == VERBOSE_HEX_BYTES_PER_LINE) { 375 appendHexSpaces(linebuf, VERBOSE_HEX_BYTES_PER_LINE); 376 if (extraLines++ > 0) { 377 printDescription(""); } 379 print(linebuf); 380 markLn(); 381 byteOnLine = 0; 382 } 383 appendHexByte(bytes[i], linebuf, byteOnLine++); 384 } 385 appendHexSpaces(linebuf, byteOnLine); 386 if (extraLines > 0) { 387 printDescription(""); } 389 390 fExtraVerboseLines += extraLines; 391 print(linebuf); 392 if (extraLines > 0) { 393 gotoPosition(startPosition); 394 } 395 } 396 397 400 public void printValue(int value, Map valueToString) { 401 Integer val = new Integer (value); 402 if (valueToString == null) { 403 print(val.toString()); 404 return; 405 } 406 String result = (String )valueToString.get(val); 407 if (result == null) { 408 print(val.toString() + JDIMessages.VerboseWriter___unknown_value__1); 409 } else { 410 print(result); 411 } 412 } 413 414 417 public void printValue(byte value, String [] bitNames) { 418 printValue(value & 0xff, bitNames); 419 } 420 421 424 public void printValue(short value, String [] bitNames) { 425 printValue(value & 0xffff, bitNames); 426 } 427 428 431 public void printValue(int value, String [] bitNames) { 432 Integer val = new Integer (value); 433 if (bitNames == null) { 434 print(val.toString()); 435 return; 436 } 437 438 boolean bitsSet = false; 439 440 for (int i = 0; i < bitNames.length; i++) { 441 if ((1 << i & value) == 0) { 443 continue; 444 } 445 446 String bitString = bitNames[i]; 448 if (bitString == null) { 449 bitString = JDIMessages.VerboseWriter__unknown_bit__2; 450 } 451 452 if (!bitsSet) { 453 print(bitString); 454 } else { 455 print(" & "); print(bitString); 457 } 458 bitsSet = true; 459 } 460 461 if (!bitsSet) { 462 print(JDIMessages.VerboseWriter__none__4); 463 } 464 } 465 466 470 private void checkForNewLine() { 471 if (fNewLine) { 472 ((StringBuffer )(fLineBuffer.get(fPosition))).setLength(0); 473 fNewLine = false; 474 } 475 } 476 477 480 public void print(String str) { 481 checkForNewLine(); 482 ((StringBuffer )(fLineBuffer.get(fPosition))).append(str); 483 } 484 485 488 public void print(char c) { 489 checkForNewLine(); 490 ((StringBuffer )(fLineBuffer.get(fPosition))).append(c); 491 } 492 493 496 public void print(char[] c) { 497 checkForNewLine(); 498 ((StringBuffer )(fLineBuffer.get(fPosition))).append(c); 499 } 500 501 504 public void println(String str) { 505 print(str); 506 println(); 507 } 508 509 513 public void flush() { 514 synchronized(fOutput) { 515 int bufSize = fLineBuffer.size(); 516 517 for (int i = 0; i < bufSize - 1; i++) 518 fOutput.println(new String ((StringBuffer )fLineBuffer.get(i))); 519 520 StringBuffer lastLine = (StringBuffer )fLineBuffer.get(bufSize - 1); 522 if (lastLine.length() > 0) 523 fOutput.print(new String (lastLine)); 524 525 fOutput.flush(); 526 fLineBuffer.clear(); 527 fPosition = 0; 528 fLineBuffer.add(new StringBuffer ()); 529 } 530 } 531 532 538 public void gotoPosition(int pos) { 539 int delta = pos - fPosition; 540 if (delta < 0) { 541 fPosition = pos; 542 } else { 543 while (delta-- > 0) 544 println(); 545 } 546 } 547 548 551 public void printLines(int lines) { 552 gotoPosition(fPosition + lines); 553 } 554 555 558 public int position() { 559 return fPosition; 560 } 561 562 565 public void markLn() { 566 if (++fPosition == fLineBuffer.size()) { 567 fLineBuffer.add(new StringBuffer ()); 568 } 569 570 fNewLine = true; 571 } 572 } 573 | Popular Tags |