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. If 583 * automatic flushing is enabled then the {@code flush} method will be 584 * invoked. 585 * 586 * <p> Note that the bytes will be written as given; to write characters 587 * that will be translated according to the platform's default character 588 * encoding, use the {@code print(char[])} or {@code println(char[])} 589 * methods. 590 * 591 * @apiNote 592 * Although declared to throw {@code IOException}, this method never 593 * actually does so. Instead, like other methods that this class 594 * overrides, it sets an internal flag which may be tested via the 595 * {@link #checkError()} method. To write an array of bytes without having 596 * to write a {@code catch} block for the {@code IOException}, use either 597 * {@link #writeBytes(byte[] buf) writeBytes(buf)} or 598 * {@link #write(byte[], int, int) write(buf, 0, buf.length)}. 599 * 600 * @implSpec 601 * This method is equivalent to 602 * {@link java.io.PrintStream#write(byte[],int,int) 603 * this.write(buf, 0, buf.length)}. 604 * 605 * @param buf A byte array 606 * 607 * @throws IOException If an I/O error occurs. 608 * 609 * @see #writeBytes(byte[]) 610 * @see #write(byte[],int,int) 611 */ 612 @Override 613 public void write(byte buf[]) throws IOException { 614 this.write(buf, 0, buf.length); 615 } 616 617 /** 618 * Writes all bytes from the specified byte array to this stream. 619 * If automatic flushing is enabled then the {@code flush} method 620 * will be invoked. 621 * 622 * <p> Note that the bytes will be written as given; to write characters 623 * that will be translated according to the platform's default character 624 * encoding, use the {@code print(char[])} or {@code println(char[])} 625 * methods. 626 * 627 * @implSpec 628 * This method is equivalent to 629 * {@link #write(byte[], int, int) this.write(buf, 0, buf.length)}. 630 * 631 * @param buf A byte array 632 * 633 * @since 14 634 */ 635 public void writeBytes(byte buf[]) { 636 this.write(buf, 0, buf.length); 637 } 638 639 /* 640 * The following private methods on the text- and character-output streams 641 * always flush the stream buffers, so that writes to the underlying byte 642 * stream occur as promptly as with the original PrintStream. 643 */ 644 645 private void write(char[] buf) { 646 try { 647 synchronized (this) { 648 ensureOpen(); 649 textOut.write(buf); 650 textOut.flushBuffer(); 651 charOut.flushBuffer(); 652 if (autoFlush) { 653 for (int i = 0; i < buf.length; i++) 654 if (buf[i] == '\n') { 655 out.flush(); 656 break; 657 } 658 } |