398 * denies write access to the file 399 * 400 * @since 10 401 */ 402 public PrintStream(File file, Charset charset) throws IOException { 403 this(false, requireNonNull(charset, "charset"), new FileOutputStream(file)); 404 } 405 406 /** Check to make sure that the stream has not been closed */ 407 private void ensureOpen() throws IOException { 408 if (out == null) 409 throw new IOException("Stream closed"); 410 } 411 412 /** 413 * Flushes the stream. This is done by writing any buffered output bytes to 414 * the underlying output stream and then flushing that stream. 415 * 416 * @see java.io.OutputStream#flush() 417 */ 418 public void flush() { 419 synchronized (this) { 420 try { 421 ensureOpen(); 422 out.flush(); 423 } 424 catch (IOException x) { 425 trouble = true; 426 } 427 } 428 } 429 430 private boolean closing = false; /* To avoid recursive closing */ 431 432 /** 433 * Closes the stream. This is done by flushing the stream and then closing 434 * the underlying output stream. 435 * 436 * @see java.io.OutputStream#close() 437 */ 438 public void close() { 439 synchronized (this) { 440 if (! closing) { 441 closing = true; 442 try { 443 textOut.close(); 444 out.close(); 445 } 446 catch (IOException x) { 447 trouble = true; 448 } 449 textOut = null; 450 charOut = null; 451 out = null; 452 } 453 } 454 } 455 456 /** 457 * Flushes the stream and checks its error state. The internal error state 509 510 /* 511 * Exception-catching, synchronized output operations, 512 * which also implement the write() methods of OutputStream 513 */ 514 515 /** 516 * Writes the specified byte to this stream. If the byte is a newline and 517 * automatic flushing is enabled then the {@code flush} method will be 518 * invoked. 519 * 520 * <p> Note that the byte is written as given; to write a character that 521 * will be translated according to the platform's default character 522 * encoding, use the {@code print(char)} or {@code println(char)} 523 * methods. 524 * 525 * @param b The byte to be written 526 * @see #print(char) 527 * @see #println(char) 528 */ 529 public void write(int b) { 530 try { 531 synchronized (this) { 532 ensureOpen(); 533 out.write(b); 534 if ((b == '\n') && autoFlush) 535 out.flush(); 536 } 537 } 538 catch (InterruptedIOException x) { 539 Thread.currentThread().interrupt(); 540 } 541 catch (IOException x) { 542 trouble = true; 543 } 544 } 545 546 /** 547 * Writes {@code len} bytes from the specified byte array starting at 548 * offset {@code off} to this stream. If automatic flushing is 549 * enabled then the {@code flush} method will be invoked. 550 * 551 * <p> Note that the bytes will be written as given; to write characters 552 * that will be translated according to the platform's default character 553 * encoding, use the {@code print(char)} or {@code println(char)} 554 * methods. 555 * 556 * @param buf A byte array 557 * @param off Offset from which to start taking bytes 558 * @param len Number of bytes to write 559 */ 560 public void write(byte buf[], int off, int len) { 561 try { 562 synchronized (this) { 563 ensureOpen(); 564 out.write(buf, off, len); 565 if (autoFlush) 566 out.flush(); 567 } 568 } 569 catch (InterruptedIOException x) { 570 Thread.currentThread().interrupt(); 571 } 572 catch (IOException x) { 573 trouble = true; 574 } 575 } 576 577 /* 578 * The following private methods on the text- and character-output streams 579 * always flush the stream buffers, so that writes to the underlying byte 580 * stream occur as promptly as with the original PrintStream. 581 */ 582 583 private void write(char[] buf) { 584 try { 585 synchronized (this) { 586 ensureOpen(); 587 textOut.write(buf); 588 textOut.flushBuffer(); 589 charOut.flushBuffer(); 590 if (autoFlush) { 591 for (int i = 0; i < buf.length; i++) 592 if (buf[i] == '\n') { 593 out.flush(); 594 break; 595 } 596 } | 398 * denies write access to the file 399 * 400 * @since 10 401 */ 402 public PrintStream(File file, Charset charset) throws IOException { 403 this(false, requireNonNull(charset, "charset"), new FileOutputStream(file)); 404 } 405 406 /** Check to make sure that the stream has not been closed */ 407 private void ensureOpen() throws IOException { 408 if (out == null) 409 throw new IOException("Stream closed"); 410 } 411 412 /** 413 * Flushes the stream. This is done by writing any buffered output bytes to 414 * the underlying output stream and then flushing that stream. 415 * 416 * @see java.io.OutputStream#flush() 417 */ 418 @Override 419 public void flush() { 420 synchronized (this) { 421 try { 422 ensureOpen(); 423 out.flush(); 424 } 425 catch (IOException x) { 426 trouble = true; 427 } 428 } 429 } 430 431 private boolean closing = false; /* To avoid recursive closing */ 432 433 /** 434 * Closes the stream. This is done by flushing the stream and then closing 435 * the underlying output stream. 436 * 437 * @see java.io.OutputStream#close() 438 */ 439 @Override 440 public void close() { 441 synchronized (this) { 442 if (! closing) { 443 closing = true; 444 try { 445 textOut.close(); 446 out.close(); 447 } 448 catch (IOException x) { 449 trouble = true; 450 } 451 textOut = null; 452 charOut = null; 453 out = null; 454 } 455 } 456 } 457 458 /** 459 * Flushes the stream and checks its error state. The internal error state 511 512 /* 513 * Exception-catching, synchronized output operations, 514 * which also implement the write() methods of OutputStream 515 */ 516 517 /** 518 * Writes the specified byte to this stream. If the byte is a newline and 519 * automatic flushing is enabled then the {@code flush} method will be 520 * invoked. 521 * 522 * <p> Note that the byte is written as given; to write a character that 523 * will be translated according to the platform's default character 524 * encoding, use the {@code print(char)} or {@code println(char)} 525 * methods. 526 * 527 * @param b The byte to be written 528 * @see #print(char) 529 * @see #println(char) 530 */ 531 @Override 532 public void write(int b) { 533 try { 534 synchronized (this) { 535 ensureOpen(); 536 out.write(b); 537 if ((b == '\n') && autoFlush) 538 out.flush(); 539 } 540 } 541 catch (InterruptedIOException x) { 542 Thread.currentThread().interrupt(); 543 } 544 catch (IOException x) { 545 trouble = true; 546 } 547 } 548 549 /** 550 * Writes {@code len} bytes from the specified byte array starting at 551 * offset {@code off} to this stream. If automatic flushing is 552 * enabled then the {@code flush} method will be invoked. 553 * 554 * <p> Note that the bytes will be written as given; to write characters 555 * that will be translated according to the platform's default character 556 * encoding, use the {@code print(char)} or {@code println(char)} 557 * methods. 558 * 559 * @param buf A byte array 560 * @param off Offset from which to start taking bytes 561 * @param len Number of bytes to write 562 */ 563 @Override 564 public void write(byte buf[], int off, int len) { 565 try { 566 synchronized (this) { 567 ensureOpen(); 568 out.write(buf, off, len); 569 if (autoFlush) 570 out.flush(); 571 } 572 } 573 catch (InterruptedIOException x) { 574 Thread.currentThread().interrupt(); 575 } 576 catch (IOException x) { 577 trouble = true; 578 } 579 } 580 581 /** 582 * Writes all bytes from the specified byte array to this stream. 583 * The automatic flushing setting is ignored. The internal flag 584 * tested by {@link #checkError()} will <i>not</i> be set if an 585 * {@code IOException} is thrown. 586 * 587 * <p> Note that the bytes will be written as given; to write characters 588 * that will be translated according to the platform's default character 589 * encoding, use the {@code print(char[])} or {@code println(char[])} 590 * methods. 591 * 592 * @apiNote 593 * Unlike other overridden methods in this class, this method may throw 594 * an {@code IOException}. To write an array of bytes without being 595 * required to handle an {@code IOException}, use either 596 * {@link #writeBytes(byte[] buf) writeBytes(buf)} or 597 * {@link #write(byte[],int,int) write(buf,0,buf.length)}. 598 * 599 * @implSpec 600 * The default implementation is equivalent to 601 * {@link java.io.FilterOutputStream#write(byte[],int,int) 602 * super.write(buf,0,buf.length)}. 603 * 604 * @param buf A byte array 605 * 606 * @see #writeBytes(byte[]) 607 * @see #write(byte[],int,int) 608 */ 609 @Override 610 public void write(byte buf[]) throws IOException { 611 super.write(buf, 0, buf.length); 612 } 613 614 /** 615 * Writes all bytes from the specified byte array to this stream. 616 * If automatic flushing is enabled then the {@code flush} method 617 * will be invoked. 618 * 619 * <p> Note that the bytes will be written as given; to write characters 620 * that will be translated according to the platform's default character 621 * encoding, use the {@code print(char[])} or {@code println(char[])} 622 * methods. 623 * 624 * @implSpec 625 * The default implementation is equivalent to 626 * {@link #write(byte[],int,int) this.write(buf, 0, buf.length)}. 627 * 628 * @param buf A byte array 629 * 630 * @since 14 631 */ 632 public void writeBytes(byte buf[]) { 633 this.write(buf, 0, buf.length); 634 } 635 636 /* 637 * The following private methods on the text- and character-output streams 638 * always flush the stream buffers, so that writes to the underlying byte 639 * stream occur as promptly as with the original PrintStream. 640 */ 641 642 private void write(char[] buf) { 643 try { 644 synchronized (this) { 645 ensureOpen(); 646 textOut.write(buf); 647 textOut.flushBuffer(); 648 charOut.flushBuffer(); 649 if (autoFlush) { 650 for (int i = 0; i < buf.length; i++) 651 if (buf[i] == '\n') { 652 out.flush(); 653 break; 654 } 655 } |