< 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


 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


 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      * Writes all bytes from the specified byte array to this stream.
 574      * If automatic flushing is enabled then the {@code flush} method
 575      * will be invoked.
 576      *
 577      * <p> Note that the bytes will be written as given; to write characters
 578      * that will be translated according to the platform's default character
 579      * encoding, use the {@code print(char[])} or {@code println(char[])}
 580      * methods.
 581      *
 582      * @apiNote
 583      * This method is equivalent to {@link #write(byte[],int,int)
 584      * write(b, 0, b.length)}.
 585      *
 586      * @param  buf   A byte array
 587      *
 588      * @since 14
 589      */
 590     public void writeBytes(byte buf[]) {
 591         this.write(buf, 0, buf.length);
 592     }
 593 
 594     /*
 595      * The following private methods on the text- and character-output streams
 596      * always flush the stream buffers, so that writes to the underlying byte
 597      * stream occur as promptly as with the original PrintStream.
 598      */
 599 
 600     private void write(char[] buf) {
 601         try {
 602             synchronized (this) {
 603                 ensureOpen();
 604                 textOut.write(buf);
 605                 textOut.flushBuffer();
 606                 charOut.flushBuffer();
 607                 if (autoFlush) {
 608                     for (int i = 0; i < buf.length; i++)
 609                         if (buf[i] == '\n') {
 610                             out.flush();
 611                             break;
 612                         }
 613                 }


< prev index next >