# HG changeset patch # User redestad # Date 1544805216 -3600 # Fri Dec 14 17:33:36 2018 +0100 # Node ID ec3a7fe0e1f1f50638688a5ee7b0cefd90809747 # Parent 8180809085a41f75def446c315344ffbc3deb990 8215412: Optimize PrintStream.println methods Reviewed-by: TBD 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 @@ -597,6 +597,29 @@ } } + private void writeln(char buf[]) { + try { + synchronized (this) { + ensureOpen(); + textOut.write(buf); + textOut.newLine(); + textOut.flushBuffer(); + charOut.flushBuffer(); + if (autoFlush) { + for (int i = 0; i < buf.length; i++) + if (buf[i] == '\n') + out.flush(); + } + } + } + catch (InterruptedIOException x) { + Thread.currentThread().interrupt(); + } + catch (IOException x) { + trouble = true; + } + } + private void write(String s) { try { synchronized (this) { @@ -616,6 +639,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,10 +823,7 @@ * @param x The {@code boolean} to be printed */ public void println(boolean x) { - synchronized (this) { - print(x); - newLine(); - } + writeln(String.valueOf(x)); } /** @@ -794,10 +834,7 @@ * @param x The {@code char} to be printed. */ public void println(char x) { - synchronized (this) { - print(x); - newLine(); - } + writeln(String.valueOf(x)); } /** @@ -808,10 +845,7 @@ * @param x The {@code int} to be printed. */ public void println(int x) { - synchronized (this) { - print(x); - newLine(); - } + writeln(String.valueOf(x)); } /** @@ -822,10 +856,7 @@ * @param x a The {@code long} to be printed. */ public void println(long x) { - synchronized (this) { - print(x); - newLine(); - } + writeln(String.valueOf(x)); } /** @@ -836,10 +867,7 @@ * @param x The {@code float} to be printed. */ public void println(float x) { - synchronized (this) { - print(x); - newLine(); - } + writeln(String.valueOf(x)); } /** @@ -850,10 +878,7 @@ * @param x The {@code double} to be printed. */ public void println(double x) { - synchronized (this) { - print(x); - newLine(); - } + writeln(String.valueOf(x)); } /** @@ -864,10 +889,7 @@ * @param x an array of chars to print. */ public void println(char x[]) { - synchronized (this) { - print(x); - newLine(); - } + writeln(x); } /** @@ -878,10 +900,7 @@ * @param x The {@code String} to be printed. */ public void println(String x) { - synchronized (this) { - print(x); - newLine(); - } + writeln(String.valueOf(x)); } /** @@ -894,11 +913,7 @@ * @param x The {@code Object} to be printed. */ public void println(Object x) { - String s = String.valueOf(x); - synchronized (this) { - print(s); - newLine(); - } + println(String.valueOf(x)); }