< prev index next >

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

Print this page




 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                 }
 590             }
 591         }
 592         catch (InterruptedIOException x) {
 593             Thread.currentThread().interrupt();
 594         }
 595         catch (IOException x) {
 596             trouble = true;
 597         }
 598     }


 711      * bytes are written in exactly the manner of the {@link
 712      * #write(int)} method.
 713      *
 714      * @param      d   The {@code double} to be printed
 715      * @see        java.lang.Double#toString(double)
 716      */
 717     public void print(double d) {
 718         write(String.valueOf(d));
 719     }
 720 
 721     /**
 722      * Prints an array of characters.  The characters are converted into bytes
 723      * according to the platform's default character encoding, and these bytes
 724      * are written in exactly the manner of the
 725      * {@link #write(int)} method.
 726      *
 727      * @param      s   The array of chars to be printed
 728      *
 729      * @throws  NullPointerException  If {@code s} is {@code null}
 730      */
 731     public void print(char s[]) {
 732         write(s);
 733     }
 734 
 735     /**
 736      * Prints a string.  If the argument is {@code null} then the string
 737      * {@code "null"} is printed.  Otherwise, the string's characters are
 738      * converted into bytes according to the platform's default character
 739      * encoding, and these bytes are written in exactly the manner of the
 740      * {@link #write(int)} method.
 741      *
 742      * @param      s   The {@code String} to be printed
 743      */
 744     public void print(String s) {
 745         write(String.valueOf(s));
 746     }
 747 
 748     /**
 749      * Prints an object.  The string produced by the {@link
 750      * java.lang.String#valueOf(Object)} method is translated into bytes
 751      * according to the platform's default character encoding, and these bytes


 846      * Prints a double and then terminate the line.  This method behaves as
 847      * though it invokes {@link #print(double)} and then
 848      * {@link #println()}.
 849      *
 850      * @param x  The {@code double} to be printed.
 851      */
 852     public void println(double x) {
 853         synchronized (this) {
 854             print(x);
 855             newLine();
 856         }
 857     }
 858 
 859     /**
 860      * Prints an array of characters and then terminate the line.  This method
 861      * behaves as though it invokes {@link #print(char[])} and
 862      * then {@link #println()}.
 863      *
 864      * @param x  an array of chars to print.
 865      */
 866     public void println(char x[]) {
 867         synchronized (this) {
 868             print(x);
 869             newLine();
 870         }
 871     }
 872 
 873     /**
 874      * Prints a String and then terminate the line.  This method behaves as
 875      * though it invokes {@link #print(String)} and then
 876      * {@link #println()}.
 877      *
 878      * @param x  The {@code String} to be printed.
 879      */
 880     public void println(String x) {
 881         synchronized (this) {
 882             print(x);
 883             newLine();
 884         }
 885     }
 886 




 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                 }
 590             }
 591         }
 592         catch (InterruptedIOException x) {
 593             Thread.currentThread().interrupt();
 594         }
 595         catch (IOException x) {
 596             trouble = true;
 597         }
 598     }


 711      * bytes are written in exactly the manner of the {@link
 712      * #write(int)} method.
 713      *
 714      * @param      d   The {@code double} to be printed
 715      * @see        java.lang.Double#toString(double)
 716      */
 717     public void print(double d) {
 718         write(String.valueOf(d));
 719     }
 720 
 721     /**
 722      * Prints an array of characters.  The characters are converted into bytes
 723      * according to the platform's default character encoding, and these bytes
 724      * are written in exactly the manner of the
 725      * {@link #write(int)} method.
 726      *
 727      * @param      s   The array of chars to be printed
 728      *
 729      * @throws  NullPointerException  If {@code s} is {@code null}
 730      */
 731     public void print(char[] s) {
 732         write(s);
 733     }
 734 
 735     /**
 736      * Prints a string.  If the argument is {@code null} then the string
 737      * {@code "null"} is printed.  Otherwise, the string's characters are
 738      * converted into bytes according to the platform's default character
 739      * encoding, and these bytes are written in exactly the manner of the
 740      * {@link #write(int)} method.
 741      *
 742      * @param      s   The {@code String} to be printed
 743      */
 744     public void print(String s) {
 745         write(String.valueOf(s));
 746     }
 747 
 748     /**
 749      * Prints an object.  The string produced by the {@link
 750      * java.lang.String#valueOf(Object)} method is translated into bytes
 751      * according to the platform's default character encoding, and these bytes


 846      * Prints a double and then terminate the line.  This method behaves as
 847      * though it invokes {@link #print(double)} and then
 848      * {@link #println()}.
 849      *
 850      * @param x  The {@code double} to be printed.
 851      */
 852     public void println(double x) {
 853         synchronized (this) {
 854             print(x);
 855             newLine();
 856         }
 857     }
 858 
 859     /**
 860      * Prints an array of characters and then terminate the line.  This method
 861      * behaves as though it invokes {@link #print(char[])} and
 862      * then {@link #println()}.
 863      *
 864      * @param x  an array of chars to print.
 865      */
 866     public void println(char[] x) {
 867         synchronized (this) {
 868             print(x);
 869             newLine();
 870         }
 871     }
 872 
 873     /**
 874      * Prints a String and then terminate the line.  This method behaves as
 875      * though it invokes {@link #print(String)} and then
 876      * {@link #println()}.
 877      *
 878      * @param x  The {@code String} to be printed.
 879      */
 880     public void println(String x) {
 881         synchronized (this) {
 882             print(x);
 883             newLine();
 884         }
 885     }
 886 


< prev index next >