< prev index next >

src/java.base/share/classes/java/io/PrintStream.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 393      *          denies write access to the file
 394      *
 395      * @since  10
 396      */
 397     public PrintStream(File file, Charset charset) throws IOException {
 398         this(false, requireNonNull(charset, "charset"), new FileOutputStream(file));
 399     }
 400 
 401     /** Check to make sure that the stream has not been closed */
 402     private void ensureOpen() throws IOException {
 403         if (out == null)
 404             throw new IOException("Stream closed");
 405     }
 406 
 407     /**
 408      * Flushes the stream.  This is done by writing any buffered output bytes to
 409      * the underlying output stream and then flushing that stream.
 410      *
 411      * @see        java.io.OutputStream#flush()
 412      */

 413     public void flush() {
 414         synchronized (this) {
 415             try {
 416                 ensureOpen();
 417                 out.flush();
 418             }
 419             catch (IOException x) {
 420                 trouble = true;
 421             }
 422         }
 423     }
 424 
 425     private boolean closing = false; /* To avoid recursive closing */
 426 
 427     /**
 428      * Closes the stream.  This is done by flushing the stream and then closing
 429      * the underlying output stream.
 430      *
 431      * @see        java.io.OutputStream#close()
 432      */

 433     public void close() {
 434         synchronized (this) {
 435             if (! closing) {
 436                 closing = true;
 437                 try {
 438                     textOut.close();
 439                     out.close();
 440                 }
 441                 catch (IOException x) {
 442                     trouble = true;
 443                 }
 444                 textOut = null;
 445                 charOut = null;
 446                 out = null;
 447             }
 448         }
 449     }
 450 
 451     /**
 452      * Flushes the stream and checks its error state. The internal error state


 504 
 505     /*
 506      * Exception-catching, synchronized output operations,
 507      * which also implement the write() methods of OutputStream
 508      */
 509 
 510     /**
 511      * Writes the specified byte to this stream.  If the byte is a newline and
 512      * automatic flushing is enabled then the {@code flush} method will be
 513      * invoked.
 514      *
 515      * <p> Note that the byte is written as given; to write a character that
 516      * will be translated according to the platform's default character
 517      * encoding, use the {@code print(char)} or {@code println(char)}
 518      * methods.
 519      *
 520      * @param  b  The byte to be written
 521      * @see #print(char)
 522      * @see #println(char)
 523      */

 524     public void write(int b) {
 525         try {
 526             synchronized (this) {
 527                 ensureOpen();
 528                 out.write(b);
 529                 if ((b == '\n') && autoFlush)
 530                     out.flush();
 531             }
 532         }
 533         catch (InterruptedIOException x) {
 534             Thread.currentThread().interrupt();
 535         }
 536         catch (IOException x) {
 537             trouble = true;
 538         }
 539     }
 540 
 541     /**
 542      * Writes {@code len} bytes from the specified byte array starting at
 543      * offset {@code off} to this stream.  If automatic flushing is
 544      * enabled then the {@code flush} method will be invoked.
 545      *
 546      * <p> Note that the bytes will be written as given; to write characters
 547      * that will be translated according to the platform's default character
 548      * encoding, use the {@code print(char)} or {@code println(char)}
 549      * methods.
 550      *
 551      * @param  buf   A byte array
 552      * @param  off   Offset from which to start taking bytes
 553      * @param  len   Number of bytes to write
 554      */

 555     public void write(byte buf[], int off, int len) {
 556         try {
 557             synchronized (this) {
 558                 ensureOpen();
 559                 out.write(buf, off, len);
 560                 if (autoFlush)
 561                     out.flush();
 562             }
 563         }
 564         catch (InterruptedIOException x) {
 565             Thread.currentThread().interrupt();
 566         }
 567         catch (IOException x) {
 568             trouble = true;
 569         }
 570     }
 571 






























 572     /*
 573      * The following private methods on the text- and character-output streams
 574      * always flush the stream buffers, so that writes to the underlying byte
 575      * stream occur as promptly as with the original PrintStream.
 576      */
 577 
 578     private void write(char[] buf) {
 579         try {
 580             synchronized (this) {
 581                 ensureOpen();
 582                 textOut.write(buf);
 583                 textOut.flushBuffer();
 584                 charOut.flushBuffer();
 585                 if (autoFlush) {
 586                     for (int i = 0; i < buf.length; i++)
 587                         if (buf[i] == '\n') {
 588                             out.flush();
 589                             break;
 590                         }
 591                 }


   1 /*
   2  * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 393      *          denies write access to the file
 394      *
 395      * @since  10
 396      */
 397     public PrintStream(File file, Charset charset) throws IOException {
 398         this(false, requireNonNull(charset, "charset"), new FileOutputStream(file));
 399     }
 400 
 401     /** Check to make sure that the stream has not been closed */
 402     private void ensureOpen() throws IOException {
 403         if (out == null)
 404             throw new IOException("Stream closed");
 405     }
 406 
 407     /**
 408      * Flushes the stream.  This is done by writing any buffered output bytes to
 409      * the underlying output stream and then flushing that stream.
 410      *
 411      * @see        java.io.OutputStream#flush()
 412      */
 413     @Override
 414     public void flush() {
 415         synchronized (this) {
 416             try {
 417                 ensureOpen();
 418                 out.flush();
 419             }
 420             catch (IOException x) {
 421                 trouble = true;
 422             }
 423         }
 424     }
 425 
 426     private boolean closing = false; /* To avoid recursive closing */
 427 
 428     /**
 429      * Closes the stream.  This is done by flushing the stream and then closing
 430      * the underlying output stream.
 431      *
 432      * @see        java.io.OutputStream#close()
 433      */
 434     @Override
 435     public void close() {
 436         synchronized (this) {
 437             if (! closing) {
 438                 closing = true;
 439                 try {
 440                     textOut.close();
 441                     out.close();
 442                 }
 443                 catch (IOException x) {
 444                     trouble = true;
 445                 }
 446                 textOut = null;
 447                 charOut = null;
 448                 out = null;
 449             }
 450         }
 451     }
 452 
 453     /**
 454      * Flushes the stream and checks its error state. The internal error state


 506 
 507     /*
 508      * Exception-catching, synchronized output operations,
 509      * which also implement the write() methods of OutputStream
 510      */
 511 
 512     /**
 513      * Writes the specified byte to this stream.  If the byte is a newline and
 514      * automatic flushing is enabled then the {@code flush} method will be
 515      * invoked.
 516      *
 517      * <p> Note that the byte is written as given; to write a character that
 518      * will be translated according to the platform's default character
 519      * encoding, use the {@code print(char)} or {@code println(char)}
 520      * methods.
 521      *
 522      * @param  b  The byte to be written
 523      * @see #print(char)
 524      * @see #println(char)
 525      */
 526     @Override
 527     public void write(int b) {
 528         try {
 529             synchronized (this) {
 530                 ensureOpen();
 531                 out.write(b);
 532                 if ((b == '\n') && autoFlush)
 533                     out.flush();
 534             }
 535         }
 536         catch (InterruptedIOException x) {
 537             Thread.currentThread().interrupt();
 538         }
 539         catch (IOException x) {
 540             trouble = true;
 541         }
 542     }
 543 
 544     /**
 545      * Writes {@code len} bytes from the specified byte array starting at
 546      * offset {@code off} to this stream.  If automatic flushing is
 547      * enabled then the {@code flush} method will be invoked.
 548      *
 549      * <p> Note that the bytes will be written as given; to write characters
 550      * that will be translated according to the platform's default character
 551      * encoding, use the {@code print(char[])} or {@code println(char[])}
 552      * methods.
 553      *
 554      * @param  buf   A byte array
 555      * @param  off   Offset from which to start taking bytes
 556      * @param  len   Number of bytes to write
 557      */
 558     @Override
 559     public void write(byte buf[], int off, int len) {
 560         try {
 561             synchronized (this) {
 562                 ensureOpen();
 563                 out.write(buf, off, len);
 564                 if (autoFlush)
 565                     out.flush();
 566             }
 567         }
 568         catch (InterruptedIOException x) {
 569             Thread.currentThread().interrupt();
 570         }
 571         catch (IOException x) {
 572             trouble = true;
 573         }
 574     }
 575 
 576     /**
 577      * Writes all bytes from the specified byte array to this stream.
 578      * If automatic flushing is enabled then the {@code flush} method
 579      * will be invoked.
 580      *
 581      * <p> Note that the bytes will be written as given; to write characters
 582      * that will be translated according to the platform's default character
 583      * encoding, use the {@code print(char[])} or {@code println(char[])}
 584      * methods.
 585      *
 586      * @param  buf   A byte array
 587      */
 588     @Override
 589     public void write(byte buf[]) {
 590         try {
 591             synchronized (this) {
 592                 ensureOpen();
 593                 out.write(buf, 0, buf.length);
 594                 if (autoFlush)
 595                     out.flush();
 596             }
 597         }
 598         catch (InterruptedIOException x) {
 599             Thread.currentThread().interrupt();
 600         }
 601         catch (IOException x) {
 602             trouble = true;
 603         }
 604     }
 605 
 606     /*
 607      * The following private methods on the text- and character-output streams
 608      * always flush the stream buffers, so that writes to the underlying byte
 609      * stream occur as promptly as with the original PrintStream.
 610      */
 611 
 612     private void write(char[] buf) {
 613         try {
 614             synchronized (this) {
 615                 ensureOpen();
 616                 textOut.write(buf);
 617                 textOut.flushBuffer();
 618                 charOut.flushBuffer();
 619                 if (autoFlush) {
 620                     for (int i = 0; i < buf.length; i++)
 621                         if (buf[i] == '\n') {
 622                             out.flush();
 623                             break;
 624                         }
 625                 }


< prev index next >