# HG changeset patch # User redestad # Date 1546459870 -3600 # Wed Jan 02 21:11:10 2019 +0100 # Node ID 74b293b2e1121b41b1bae02b3fd5e2e5e76d28ba # Parent dee9426ef417704ba0e9b9898bf40424fb49b63d 8215412: Optimize PrintStream.println methods Reviewed-by: rriggs, dfuchs, forax diff --git a/src/java.base/share/classes/java/io/PrintStream.java b/src/java.base/share/classes/java/io/PrintStream.java --- a/src/java.base/share/classes/java/io/PrintStream.java +++ b/src/java.base/share/classes/java/io/PrintStream.java @@ -575,7 +575,7 @@ * stream occur as promptly as with the original PrintStream. */ - private void write(char buf[]) { + private void write(char[] buf) { try { synchronized (this) { ensureOpen(); @@ -584,10 +584,30 @@ charOut.flushBuffer(); if (autoFlush) { for (int i = 0; i < buf.length; i++) - if (buf[i] == '\n') + if (buf[i] == '\n') { out.flush(); + break; + } } } + } catch (InterruptedIOException x) { + Thread.currentThread().interrupt(); + } catch (IOException x) { + trouble = true; + } + } + + private void writeln(char[] buf) { + try { + synchronized (this) { + ensureOpen(); + textOut.write(buf); + textOut.newLine(); + textOut.flushBuffer(); + charOut.flushBuffer(); + if (autoFlush) + out.flush(); + } } catch (InterruptedIOException x) { Thread.currentThread().interrupt(); @@ -616,6 +636,26 @@ } } + private void writeln(String s) { + try { + synchronized (this) { + ensureOpen(); + textOut.write(s); + textOut.newLine(); + textOut.flushBuffer(); + charOut.flushBuffer(); + if (autoFlush) + out.flush(); + } + } + catch (InterruptedIOException x) { + Thread.currentThread().interrupt(); + } + catch (IOException x) { + trouble = true; + } + } + private void newLine() { try { synchronized (this) { @@ -780,9 +820,13 @@ * @param x The {@code boolean} to be printed */ public void println(boolean x) { - synchronized (this) { - print(x); - newLine(); + if (getClass() == PrintStream.class) { + writeln(String.valueOf(x)); + } else { + synchronized (this) { + print(x); + newLine(); + } } } @@ -794,9 +838,13 @@ * @param x The {@code char} to be printed. */ public void println(char x) { - synchronized (this) { - print(x); - newLine(); + if (getClass() == PrintStream.class) { + writeln(String.valueOf(x)); + } else { + synchronized (this) { + print(x); + newLine(); + } } } @@ -808,9 +856,13 @@ * @param x The {@code int} to be printed. */ public void println(int x) { - synchronized (this) { - print(x); - newLine(); + if (getClass() == PrintStream.class) { + writeln(String.valueOf(x)); + } else { + synchronized (this) { + print(x); + newLine(); + } } } @@ -822,9 +874,13 @@ * @param x a The {@code long} to be printed. */ public void println(long x) { - synchronized (this) { - print(x); - newLine(); + if (getClass() == PrintStream.class) { + writeln(String.valueOf(x)); + } else { + synchronized (this) { + print(x); + newLine(); + } } } @@ -836,9 +892,13 @@ * @param x The {@code float} to be printed. */ public void println(float x) { - synchronized (this) { - print(x); - newLine(); + if (getClass() == PrintStream.class) { + writeln(String.valueOf(x)); + } else { + synchronized (this) { + print(x); + newLine(); + } } } @@ -850,9 +910,13 @@ * @param x The {@code double} to be printed. */ public void println(double x) { - synchronized (this) { - print(x); - newLine(); + if (getClass() == PrintStream.class) { + writeln(String.valueOf(x)); + } else { + synchronized (this) { + print(x); + newLine(); + } } } @@ -863,10 +927,14 @@ * * @param x an array of chars to print. */ - public void println(char x[]) { - synchronized (this) { - print(x); - newLine(); + public void println(char[] x) { + if (getClass() == PrintStream.class) { + writeln(x); + } else { + synchronized (this) { + print(x); + newLine(); + } } } @@ -878,9 +946,13 @@ * @param x The {@code String} to be printed. */ public void println(String x) { - synchronized (this) { - print(x); - newLine(); + if (getClass() == PrintStream.class) { + writeln(String.valueOf(x)); + } else { + synchronized (this) { + print(x); + newLine(); + } } } @@ -895,9 +967,15 @@ */ public void println(Object x) { String s = String.valueOf(x); - synchronized (this) { - print(s); - newLine(); + if (getClass() == PrintStream.class) { + // need to apply String.valueOf again since first invocation + // might return null + writeln(String.valueOf(s)); + } else { + synchronized (this) { + print(s); + newLine(); + } } }