src/java.base/share/classes/java/io/PrintStream.java
changeset 53148 17d568776429
parent 48252 77b88d8f8380
child 55727 070ffafc4294
child 58678 9cf78a70fa4f
equal deleted inserted replaced
53147:db1d11c253d8 53148:17d568776429
   573      * The following private methods on the text- and character-output streams
   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
   574      * always flush the stream buffers, so that writes to the underlying byte
   575      * stream occur as promptly as with the original PrintStream.
   575      * stream occur as promptly as with the original PrintStream.
   576      */
   576      */
   577 
   577 
   578     private void write(char buf[]) {
   578     private void write(char[] buf) {
   579         try {
   579         try {
   580             synchronized (this) {
   580             synchronized (this) {
   581                 ensureOpen();
   581                 ensureOpen();
   582                 textOut.write(buf);
   582                 textOut.write(buf);
   583                 textOut.flushBuffer();
   583                 textOut.flushBuffer();
   584                 charOut.flushBuffer();
   584                 charOut.flushBuffer();
   585                 if (autoFlush) {
   585                 if (autoFlush) {
   586                     for (int i = 0; i < buf.length; i++)
   586                     for (int i = 0; i < buf.length; i++)
   587                         if (buf[i] == '\n')
   587                         if (buf[i] == '\n') {
   588                             out.flush();
   588                             out.flush();
       
   589                             break;
       
   590                         }
   589                 }
   591                 }
       
   592             }
       
   593         } catch (InterruptedIOException x) {
       
   594             Thread.currentThread().interrupt();
       
   595         } catch (IOException x) {
       
   596             trouble = true;
       
   597         }
       
   598     }
       
   599 
       
   600     // Used to optimize away back-to-back flushing and synchronization when
       
   601     // using println, but since subclasses could exist which depend on
       
   602     // observing a call to print followed by newLine() we only use this if
       
   603     // getClass() == PrintStream.class to avoid compatibility issues.
       
   604     private void writeln(char[] buf) {
       
   605         try {
       
   606             synchronized (this) {
       
   607                 ensureOpen();
       
   608                 textOut.write(buf);
       
   609                 textOut.newLine();
       
   610                 textOut.flushBuffer();
       
   611                 charOut.flushBuffer();
       
   612                 if (autoFlush)
       
   613                     out.flush();
   590             }
   614             }
   591         }
   615         }
   592         catch (InterruptedIOException x) {
   616         catch (InterruptedIOException x) {
   593             Thread.currentThread().interrupt();
   617             Thread.currentThread().interrupt();
   594         }
   618         }
   614         catch (IOException x) {
   638         catch (IOException x) {
   615             trouble = true;
   639             trouble = true;
   616         }
   640         }
   617     }
   641     }
   618 
   642 
       
   643     // Used to optimize away back-to-back flushing and synchronization when
       
   644     // using println, but since subclasses could exist which depend on
       
   645     // observing a call to print followed by newLine we only use this if
       
   646     // getClass() == PrintStream.class to avoid compatibility issues.
       
   647     private void writeln(String s) {
       
   648         try {
       
   649             synchronized (this) {
       
   650                 ensureOpen();
       
   651                 textOut.write(s);
       
   652                 textOut.newLine();
       
   653                 textOut.flushBuffer();
       
   654                 charOut.flushBuffer();
       
   655                 if (autoFlush)
       
   656                     out.flush();
       
   657             }
       
   658         }
       
   659         catch (InterruptedIOException x) {
       
   660             Thread.currentThread().interrupt();
       
   661         }
       
   662         catch (IOException x) {
       
   663             trouble = true;
       
   664         }
       
   665     }
       
   666 
   619     private void newLine() {
   667     private void newLine() {
   620         try {
   668         try {
   621             synchronized (this) {
   669             synchronized (this) {
   622                 ensureOpen();
   670                 ensureOpen();
   623                 textOut.newLine();
   671                 textOut.newLine();
   778      * {@link #println()}.
   826      * {@link #println()}.
   779      *
   827      *
   780      * @param x  The {@code boolean} to be printed
   828      * @param x  The {@code boolean} to be printed
   781      */
   829      */
   782     public void println(boolean x) {
   830     public void println(boolean x) {
   783         synchronized (this) {
   831         if (getClass() == PrintStream.class) {
   784             print(x);
   832             writeln(String.valueOf(x));
   785             newLine();
   833         } else {
       
   834             synchronized (this) {
       
   835                 print(x);
       
   836                 newLine();
       
   837             }
   786         }
   838         }
   787     }
   839     }
   788 
   840 
   789     /**
   841     /**
   790      * Prints a character and then terminate the line.  This method behaves as
   842      * Prints a character and then terminate the line.  This method behaves as
   792      * {@link #println()}.
   844      * {@link #println()}.
   793      *
   845      *
   794      * @param x  The {@code char} to be printed.
   846      * @param x  The {@code char} to be printed.
   795      */
   847      */
   796     public void println(char x) {
   848     public void println(char x) {
   797         synchronized (this) {
   849         if (getClass() == PrintStream.class) {
   798             print(x);
   850             writeln(String.valueOf(x));
   799             newLine();
   851         } else {
       
   852             synchronized (this) {
       
   853                 print(x);
       
   854                 newLine();
       
   855             }
   800         }
   856         }
   801     }
   857     }
   802 
   858 
   803     /**
   859     /**
   804      * Prints an integer and then terminate the line.  This method behaves as
   860      * Prints an integer and then terminate the line.  This method behaves as
   806      * {@link #println()}.
   862      * {@link #println()}.
   807      *
   863      *
   808      * @param x  The {@code int} to be printed.
   864      * @param x  The {@code int} to be printed.
   809      */
   865      */
   810     public void println(int x) {
   866     public void println(int x) {
   811         synchronized (this) {
   867         if (getClass() == PrintStream.class) {
   812             print(x);
   868             writeln(String.valueOf(x));
   813             newLine();
   869         } else {
       
   870             synchronized (this) {
       
   871                 print(x);
       
   872                 newLine();
       
   873             }
   814         }
   874         }
   815     }
   875     }
   816 
   876 
   817     /**
   877     /**
   818      * Prints a long and then terminate the line.  This method behaves as
   878      * Prints a long and then terminate the line.  This method behaves as
   820      * {@link #println()}.
   880      * {@link #println()}.
   821      *
   881      *
   822      * @param x  a The {@code long} to be printed.
   882      * @param x  a The {@code long} to be printed.
   823      */
   883      */
   824     public void println(long x) {
   884     public void println(long x) {
   825         synchronized (this) {
   885         if (getClass() == PrintStream.class) {
   826             print(x);
   886             writeln(String.valueOf(x));
   827             newLine();
   887         } else {
       
   888             synchronized (this) {
       
   889                 print(x);
       
   890                 newLine();
       
   891             }
   828         }
   892         }
   829     }
   893     }
   830 
   894 
   831     /**
   895     /**
   832      * Prints a float and then terminate the line.  This method behaves as
   896      * Prints a float and then terminate the line.  This method behaves as
   834      * {@link #println()}.
   898      * {@link #println()}.
   835      *
   899      *
   836      * @param x  The {@code float} to be printed.
   900      * @param x  The {@code float} to be printed.
   837      */
   901      */
   838     public void println(float x) {
   902     public void println(float x) {
   839         synchronized (this) {
   903         if (getClass() == PrintStream.class) {
   840             print(x);
   904             writeln(String.valueOf(x));
   841             newLine();
   905         } else {
       
   906             synchronized (this) {
       
   907                 print(x);
       
   908                 newLine();
       
   909             }
   842         }
   910         }
   843     }
   911     }
   844 
   912 
   845     /**
   913     /**
   846      * Prints a double and then terminate the line.  This method behaves as
   914      * Prints a double and then terminate the line.  This method behaves as
   848      * {@link #println()}.
   916      * {@link #println()}.
   849      *
   917      *
   850      * @param x  The {@code double} to be printed.
   918      * @param x  The {@code double} to be printed.
   851      */
   919      */
   852     public void println(double x) {
   920     public void println(double x) {
   853         synchronized (this) {
   921         if (getClass() == PrintStream.class) {
   854             print(x);
   922             writeln(String.valueOf(x));
   855             newLine();
   923         } else {
       
   924             synchronized (this) {
       
   925                 print(x);
       
   926                 newLine();
       
   927             }
   856         }
   928         }
   857     }
   929     }
   858 
   930 
   859     /**
   931     /**
   860      * Prints an array of characters and then terminate the line.  This method
   932      * Prints an array of characters and then terminate the line.  This method
   861      * behaves as though it invokes {@link #print(char[])} and
   933      * behaves as though it invokes {@link #print(char[])} and
   862      * then {@link #println()}.
   934      * then {@link #println()}.
   863      *
   935      *
   864      * @param x  an array of chars to print.
   936      * @param x  an array of chars to print.
   865      */
   937      */
   866     public void println(char x[]) {
   938     public void println(char[] x) {
   867         synchronized (this) {
   939         if (getClass() == PrintStream.class) {
   868             print(x);
   940             writeln(x);
   869             newLine();
   941         } else {
       
   942             synchronized (this) {
       
   943                 print(x);
       
   944                 newLine();
       
   945             }
   870         }
   946         }
   871     }
   947     }
   872 
   948 
   873     /**
   949     /**
   874      * Prints a String and then terminate the line.  This method behaves as
   950      * Prints a String and then terminate the line.  This method behaves as
   876      * {@link #println()}.
   952      * {@link #println()}.
   877      *
   953      *
   878      * @param x  The {@code String} to be printed.
   954      * @param x  The {@code String} to be printed.
   879      */
   955      */
   880     public void println(String x) {
   956     public void println(String x) {
   881         synchronized (this) {
   957         if (getClass() == PrintStream.class) {
   882             print(x);
   958             writeln(String.valueOf(x));
   883             newLine();
   959         } else {
       
   960             synchronized (this) {
       
   961                 print(x);
       
   962                 newLine();
       
   963             }
   884         }
   964         }
   885     }
   965     }
   886 
   966 
   887     /**
   967     /**
   888      * Prints an Object and then terminate the line.  This method calls
   968      * Prints an Object and then terminate the line.  This method calls
   893      *
   973      *
   894      * @param x  The {@code Object} to be printed.
   974      * @param x  The {@code Object} to be printed.
   895      */
   975      */
   896     public void println(Object x) {
   976     public void println(Object x) {
   897         String s = String.valueOf(x);
   977         String s = String.valueOf(x);
   898         synchronized (this) {
   978         if (getClass() == PrintStream.class) {
   899             print(s);
   979             // need to apply String.valueOf again since first invocation
   900             newLine();
   980             // might return null
       
   981             writeln(String.valueOf(s));
       
   982         } else {
       
   983             synchronized (this) {
       
   984                 print(s);
       
   985                 newLine();
       
   986             }
   901         }
   987         }
   902     }
   988     }
   903 
   989 
   904 
   990 
   905     /**
   991     /**