From b63853f9fd9cc94ebcfcfd908f82384a83641638 Mon Sep 17 00:00:00 2001 From: Bryce McKinlay Date: Sun, 10 Aug 2003 02:53:17 +0000 Subject: re PR libgcj/11778 (System.out PrintStream does too much buffering) * java/io/PrintStream.java (print): Always flush if auto_flush is set. Don't check for newline characters. (write (int)): Implement without using a temporary array. (write (byte[], int, int): Always flush if auto_flush is set. Don't check for newline characters. Fixes PR libgcj/11778. From-SVN: r70284 --- libjava/java/io/PrintStream.java | 41 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'libjava/java/io') diff --git a/libjava/java/io/PrintStream.java b/libjava/java/io/PrintStream.java index 8366852..d2a2117 100644 --- a/libjava/java/io/PrintStream.java +++ b/libjava/java/io/PrintStream.java @@ -95,7 +95,9 @@ public class PrintStream extends FilterOutputStream * This method intializes a new PrintStream object to write * to the specified output sink. This constructor also allows "auto-flush" * functionality to be specified where the stream will be flushed after - * every line is terminated or newline character is written. + * every print or println call, when the + * write methods with array arguments are called, or when a + * single new-line character is written. *

* * @param out The OutputStream to write to. @@ -114,7 +116,9 @@ public class PrintStream extends FilterOutputStream * This method intializes a new PrintStream object to write * to the specified output sink. This constructor also allows "auto-flush" * functionality to be specified where the stream will be flushed after - * every line is terminated or newline character is written. + * every print or println call, when the + * write methods with array arguments are called, or when a + * single new-line character is written. *

* * @param out The OutputStream to write to. @@ -256,10 +260,8 @@ public class PrintStream extends FilterOutputStream { pw.print (str); - if (str != null && auto_flush) - if ((str.indexOf ('\r') != -1) - || (str.indexOf ('\n') != -1)) - flush (); + if (auto_flush) + flush (); } /** @@ -422,9 +424,21 @@ public class PrintStream extends FilterOutputStream */ public void write (int oneByte) { - byte[] data = new byte [1]; - data [0] = (byte) (oneByte & 0xff); - write (data, 0, 1); + // We actually have to implement this method. Flush first so that + // things get written in the right order. + flush(); + + try + { + out.write (oneByte & 0xff); + + if (auto_flush && (oneByte == '\n')) + flush (); + } + catch (IOException e) + { + setError (); + } } /** @@ -446,19 +460,12 @@ public class PrintStream extends FilterOutputStream out.write (buffer, offset, len); if (auto_flush) - for (int i = offset; i < len; i++) - if ((buffer [i] == '\r') - || (buffer [i] == '\n')) - { - flush (); - break; - } + flush (); } catch (IOException e) { setError (); } } - } // class PrintStream -- cgit v1.1