1 7 8 package java.io; 9 10 import java.util.Formatter ; 11 import java.util.Locale ; 12 13 14 37 38 public class PrintStream extends FilterOutputStream 39 implements Appendable , Closeable 40 { 41 42 private boolean autoFlush = false; 43 private boolean trouble = false; 44 private Formatter formatter; 45 46 50 private BufferedWriter textOut; 51 private OutputStreamWriter charOut; 52 53 61 public PrintStream(OutputStream out) { 62 this(out, false); 63 } 64 65 72 73 private PrintStream(boolean autoFlush, OutputStream out) 74 { 75 super(out); 76 if (out == null) 77 throw new NullPointerException ("Null output stream"); 78 this.autoFlush = autoFlush; 79 } 80 81 private void init(OutputStreamWriter osw) { 82 this.charOut = osw; 83 this.textOut = new BufferedWriter (osw); 84 } 85 86 98 public PrintStream(OutputStream out, boolean autoFlush) { 99 this(autoFlush, out); 100 init(new OutputStreamWriter (this)); 101 } 102 103 119 public PrintStream(OutputStream out, boolean autoFlush, String encoding) 120 throws UnsupportedEncodingException 121 { 122 this(autoFlush, out); 123 init(new OutputStreamWriter (this, encoding)); 124 } 125 126 153 public PrintStream(String fileName) throws FileNotFoundException { 154 this(false, new FileOutputStream (fileName)); 155 init(new OutputStreamWriter (this)); 156 } 157 158 191 public PrintStream(String fileName, String csn) 192 throws FileNotFoundException , UnsupportedEncodingException 193 { 194 this(false, new FileOutputStream (fileName)); 195 init(new OutputStreamWriter (this, csn)); 196 } 197 198 225 public PrintStream(File file) throws FileNotFoundException { 226 this(false, new FileOutputStream (file)); 227 init(new OutputStreamWriter (this)); 228 } 229 230 263 public PrintStream(File file, String csn) 264 throws FileNotFoundException , UnsupportedEncodingException 265 { 266 this(false, new FileOutputStream (file)); 267 init(new OutputStreamWriter (this, csn)); 268 } 269 270 271 private void ensureOpen() throws IOException { 272 if (out == null) 273 throw new IOException ("Stream closed"); 274 } 275 276 282 public void flush() { 283 synchronized (this) { 284 try { 285 ensureOpen(); 286 out.flush(); 287 } 288 catch (IOException x) { 289 trouble = true; 290 } 291 } 292 } 293 294 private boolean closing = false; 295 296 302 public void close() { 303 synchronized (this) { 304 if (! closing) { 305 closing = true; 306 try { 307 textOut.close(); 308 out.close(); 309 } 310 catch (IOException x) { 311 trouble = true; 312 } 313 textOut = null; 314 charOut = null; 315 out = null; 316 } 317 } 318 } 319 320 338 public boolean checkError() { 339 if (out != null) 340 flush(); 341 return trouble; 342 } 343 344 349 protected void setError() { 350 trouble = true; 351 try { 352 throw new IOException (); 353 } catch (IOException x) { 354 } 355 } 356 357 358 362 363 377 public void write(int b) { 378 try { 379 synchronized (this) { 380 ensureOpen(); 381 out.write(b); 382 if ((b == '\n') && autoFlush) 383 out.flush(); 384 } 385 } 386 catch (InterruptedIOException x) { 387 Thread.currentThread().interrupt(); 388 } 389 catch (IOException x) { 390 trouble = true; 391 } 392 } 393 394 408 public void write(byte buf[], int off, int len) { 409 try { 410 synchronized (this) { 411 ensureOpen(); 412 out.write(buf, off, len); 413 if (autoFlush) 414 out.flush(); 415 } 416 } 417 catch (InterruptedIOException x) { 418 Thread.currentThread().interrupt(); 419 } 420 catch (IOException x) { 421 trouble = true; 422 } 423 } 424 425 430 431 private void write(char buf[]) { 432 try { 433 synchronized (this) { 434 ensureOpen(); 435 textOut.write(buf); 436 textOut.flushBuffer(); 437 charOut.flushBuffer(); 438 if (autoFlush) { 439 for (int i = 0; i < buf.length; i++) 440 if (buf[i] == '\n') 441 out.flush(); 442 } 443 } 444 } 445 catch (InterruptedIOException x) { 446 Thread.currentThread().interrupt(); 447 } 448 catch (IOException x) { 449 trouble = true; 450 } 451 } 452 453 private void write(String s) { 454 try { 455 synchronized (this) { 456 ensureOpen(); 457 textOut.write(s); 458 textOut.flushBuffer(); 459 charOut.flushBuffer(); 460 if (autoFlush && (s.indexOf('\n') >= 0)) 461 out.flush(); 462 } 463 } 464 catch (InterruptedIOException x) { 465 Thread.currentThread().interrupt(); 466 } 467 catch (IOException x) { 468 trouble = true; 469 } 470 } 471 472 private void newLine() { 473 try { 474 synchronized (this) { 475 ensureOpen(); 476 textOut.newLine(); 477 textOut.flushBuffer(); 478 charOut.flushBuffer(); 479 if (autoFlush) 480 out.flush(); 481 } 482 } 483 catch (InterruptedIOException x) { 484 Thread.currentThread().interrupt(); 485 } 486 catch (IOException x) { 487 trouble = true; 488 } 489 } 490 491 492 493 494 503 public void print(boolean b) { 504 write(b ? "true" : "false"); 505 } 506 507 515 public void print(char c) { 516 write(String.valueOf(c)); 517 } 518 519 529 public void print(int i) { 530 write(String.valueOf(i)); 531 } 532 533 543 public void print(long l) { 544 write(String.valueOf(l)); 545 } 546 547 557 public void print(float f) { 558 write(String.valueOf(f)); 559 } 560 561 571 public void print(double d) { 572 write(String.valueOf(d)); 573 } 574 575 585 public void print(char s[]) { 586 write(s); 587 } 588 589 598 public void print(String s) { 599 if (s == null) { 600 s = "null"; 601 } 602 write(s); 603 } 604 605 615 public void print(Object obj) { 616 write(String.valueOf(obj)); 617 } 618 619 620 621 622 628 public void println() { 629 newLine(); 630 } 631 632 639 public void println(boolean x) { 640 synchronized (this) { 641 print(x); 642 newLine(); 643 } 644 } 645 646 653 public void println(char x) { 654 synchronized (this) { 655 print(x); 656 newLine(); 657 } 658 } 659 660 667 public void println(int x) { 668 synchronized (this) { 669 print(x); 670 newLine(); 671 } 672 } 673 674 681 public void println(long x) { 682 synchronized (this) { 683 print(x); 684 newLine(); 685 } 686 } 687 688 695 public void println(float x) { 696 synchronized (this) { 697 print(x); 698 newLine(); 699 } 700 } 701 702 709 public void println(double x) { 710 synchronized (this) { 711 print(x); 712 newLine(); 713 } 714 } 715 716 723 public void println(char x[]) { 724 synchronized (this) { 725 print(x); 726 newLine(); 727 } 728 } 729 730 737 public void println(String x) { 738 synchronized (this) { 739 print(x); 740 newLine(); 741 } 742 } 743 744 751 public void println(Object x) { 752 synchronized (this) { 753 print(x); 754 newLine(); 755 } 756 } 757 758 799 public PrintStream printf(String format, Object ... args) { 800 return format(format, args); 801 } 802 803 849 public PrintStream printf(Locale l, String format, Object ... args) { 850 return format(l, format, args); 851 } 852 853 892 public PrintStream format(String format, Object ... args) { 893 try { 894 synchronized (this) { 895 ensureOpen(); 896 if ((formatter == null) 897 || (formatter.locale() != Locale.getDefault())) 898 formatter = new Formatter ((Appendable ) this); 899 formatter.format(Locale.getDefault(), format, args); 900 } 901 } catch (InterruptedIOException x) { 902 Thread.currentThread().interrupt(); 903 } catch (IOException x) { 904 trouble = true; 905 } 906 return this; 907 } 908 909 949 public PrintStream format(Locale l, String format, Object ... args) { 950 try { 951 synchronized (this) { 952 ensureOpen(); 953 if ((formatter == null) 954 || (formatter.locale() != l)) 955 formatter = new Formatter (this, l); 956 formatter.format(l, format, args); 957 } 958 } catch (InterruptedIOException x) { 959 Thread.currentThread().interrupt(); 960 } catch (IOException x) { 961 trouble = true; 962 } 963 return this; 964 } 965 966 990 public PrintStream append(CharSequence csq) { 991 if (csq == null) 992 print("null"); 993 else 994 print(csq.toString()); 995 return this; 996 } 997 998 1031 public PrintStream append(CharSequence csq, int start, int end) { 1032 CharSequence cs = (csq == null ? "null" : csq); 1033 write(cs.subSequence(start, end).toString()); 1034 return this; 1035 } 1036 1037 1053 public PrintStream append(char c) { 1054 print(c); 1055 return this; 1056 } 1057 1058} 1059 | Popular Tags |