From fbd98522decbb7d17a0105a045a7b2aa077e048b Mon Sep 17 00:00:00 2001 From: Michael Koch Date: Tue, 10 Jun 2003 17:15:19 +0000 Subject: [PATCH] PrintStream.java: Merged version from classpath. 2003-06-10 Michael Koch * java/io/PrintStream.java: Merged version from classpath. (close): Removed sychronized keyword. This class is not garantied to be thread-safe. (write): Likewise. From-SVN: r67717 --- libjava/ChangeLog | 8 ++ libjava/java/io/PrintStream.java | 229 ++++++++++++------------------- 2 files changed, 93 insertions(+), 144 deletions(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index 88b59f05811..997cf1ecffc 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,11 @@ +2003-06-10 Michael Koch + + * java/io/PrintStream.java: + Merged version from classpath. + (close): Removed sychronized keyword. This class is not garantied to + be thread-safe. + (write): Likewise. + 2003-06-09 Tom Tromey * gnu/gcj/xlib/natFont.cc (getAscent): Correctly access "ascent" diff --git a/libjava/java/io/PrintStream.java b/libjava/java/io/PrintStream.java index 489b3dd0dcd..af926de41db 100644 --- a/libjava/java/io/PrintStream.java +++ b/libjava/java/io/PrintStream.java @@ -38,8 +38,6 @@ exception statement from your version. */ package java.io; -import gnu.gcj.convert.UnicodeToBytes; - /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 * "The Java Language Specification", ISBN 0-201-63451-1 * Status: Believed complete and correct to 1.3 @@ -66,20 +64,15 @@ import gnu.gcj.convert.UnicodeToBytes; */ public class PrintStream extends FilterOutputStream { - /* Notice the implementation is quite similar to OutputStreamWriter. - * This leads to some minor duplication, because neither inherits - * from the other, and we want to maximize performance. */ - - // Line separator string. - private static final char[] line_separator - = System.getProperty("line.separator").toCharArray(); - - UnicodeToBytes converter; - - // Work buffer of characters for converter. - char[] work = new char[100]; - // Work buffer of bytes where we temporarily keep converter output. - byte[] work_bytes = new byte[100]; + /* + * Ok, why is this class deprecated? It could easily have been extended + * to support character encodings. In fact, PrintWriter is basically a + * superset of this except for the write() methods. So let's do something + * tricky here and just redirect calls in this class to a hidden PrintWriter + * instance. All the functionality goes there since that is the 'real' + * class. The big win of doing this way is that the default character + * encoding is done automagicially by the PrintWriter tree! + */ /** * This boolean indicates whether or not an error has ever occurred @@ -93,6 +86,16 @@ public class PrintStream extends FilterOutputStream */ private boolean auto_flush; + /** + * The PrintWriter instance this object writes to + */ + private PrintWriter pw; + + /** + * Lets us know if the stream is closed + */ + private boolean closed; + /** * This method intializes a new PrintStream object to write * to the specified output sink. Note that this class is deprecated in @@ -125,7 +128,7 @@ public class PrintStream extends FilterOutputStream { super (out); - converter = UnicodeToBytes.getDefaultEncoder(); + pw = new PrintWriter (out, auto_flush); this.auto_flush = auto_flush; } @@ -150,7 +153,7 @@ public class PrintStream extends FilterOutputStream { super (out); - converter = UnicodeToBytes.getEncoder (encoding); + pw = new PrintWriter (new OutputStreamWriter (out, encoding), auto_flush); this.auto_flush = auto_flush; } @@ -165,8 +168,10 @@ public class PrintStream extends FilterOutputStream */ public boolean checkError () { - flush(); - return error_occurred; + if (!closed) + pw.flush (); + + return error_occurred | pw.checkError (); } /** @@ -183,19 +188,8 @@ public class PrintStream extends FilterOutputStream */ public void close () { - try - { - flush(); - out.close(); - } - catch (InterruptedIOException iioe) - { - Thread.currentThread().interrupt(); - } - catch (IOException e) - { - setError (); - } + pw.close (); + closed = true; } /** @@ -204,85 +198,7 @@ public class PrintStream extends FilterOutputStream */ public void flush () { - try - { - out.flush(); - } - catch (InterruptedIOException iioe) - { - Thread.currentThread().interrupt(); - } - catch (IOException e) - { - setError (); - } - } - - private synchronized void print (String str, boolean println) - { - try - { - writeChars(str, 0, str.length()); - if (println) - writeChars(line_separator, 0, line_separator.length); - if (auto_flush) - flush(); - } - catch (InterruptedIOException iioe) - { - Thread.currentThread().interrupt(); - } - catch (IOException e) - { - setError (); - } - } - - private synchronized void print (char[] chars, int pos, int len, - boolean println) - { - try - { - writeChars(chars, pos, len); - if (println) - writeChars(line_separator, 0, line_separator.length); - if (auto_flush) - flush(); - } - catch (InterruptedIOException iioe) - { - Thread.currentThread().interrupt(); - } - catch (IOException e) - { - setError (); - } - } - - private void writeChars(char[] buf, int offset, int count) - throws IOException - { - while (count > 0 || converter.havePendingBytes()) - { - converter.setOutput(work_bytes, 0); - int converted = converter.write(buf, offset, count); - offset += converted; - count -= converted; - out.write(work_bytes, 0, converter.count); - } - } - - private void writeChars(String str, int offset, int count) - throws IOException - { - while (count > 0 || converter.havePendingBytes()) - { - converter.setOutput(work_bytes, 0); - int converted = converter.write(str, offset, count, work); - offset += converted; - count -= converted; - out.write(work_bytes, 0, converter.count); - } + pw.flush(); } /** @@ -294,7 +210,7 @@ public class PrintStream extends FilterOutputStream */ public void print (boolean bool) { - print(String.valueOf(bool), false); + pw.print (bool); } /** @@ -305,7 +221,7 @@ public class PrintStream extends FilterOutputStream */ public void print (int inum) { - print(String.valueOf(inum), false); + pw.print (inum); } /** @@ -316,7 +232,7 @@ public class PrintStream extends FilterOutputStream */ public void print (long lnum) { - print(String.valueOf(lnum), false); + pw.print (lnum); } /** @@ -327,7 +243,7 @@ public class PrintStream extends FilterOutputStream */ public void print (float fnum) { - print(String.valueOf(fnum), false); + pw.print (fnum); } /** @@ -338,7 +254,7 @@ public class PrintStream extends FilterOutputStream */ public void print (double dnum) { - print(String.valueOf(dnum), false); + pw.print (dnum); } /** @@ -350,7 +266,9 @@ public class PrintStream extends FilterOutputStream */ public void print (Object obj) { - print(obj == null ? "null" : obj.toString(), false); + // Don't call pw directly. Convert to String so we scan for newline + // characters on auto-flush; + print (String.valueOf (obj)); } /** @@ -361,7 +279,12 @@ public class PrintStream extends FilterOutputStream */ public void print (String str) { - print(str == null ? "null" : str, false); + pw.print (str); + + if (auto_flush) + if ((str.indexOf ('\r') != -1) + || (str.indexOf ('\n') != -1)) + flush (); } /** @@ -370,10 +293,14 @@ public class PrintStream extends FilterOutputStream * * @param ch The char value to be printed */ - public synchronized void print (char ch) + public void print (char ch) { - work[0] = ch; - print(work, 0, 1, false); + pw.print (ch); + + if (auto_flush) + if ((ch == '\r') + || (ch == '\n')) + flush (); } /** @@ -384,7 +311,16 @@ public class PrintStream extends FilterOutputStream */ public void print (char[] charArray) { - print(charArray, 0, charArray.length, false); + pw.print (charArray); + + if (auto_flush) + for (int i = 0; i < charArray.length; i++) + if ((charArray [i] == '\r') + || (charArray [i] == '\n')) + { + flush (); + break; + } } /** @@ -394,7 +330,7 @@ public class PrintStream extends FilterOutputStream */ public void println () { - print(line_separator, 0, line_separator.length, false); + pw.println (); } /** @@ -408,7 +344,7 @@ public class PrintStream extends FilterOutputStream */ public void println (boolean bool) { - print(String.valueOf(bool), true); + pw.println (bool); } /** @@ -421,7 +357,7 @@ public class PrintStream extends FilterOutputStream */ public void println (int inum) { - print(String.valueOf(inum), true); + pw.println (inum); } /** @@ -434,7 +370,7 @@ public class PrintStream extends FilterOutputStream */ public void println (long lnum) { - print(String.valueOf(lnum), true); + pw.println (lnum); } /** @@ -447,7 +383,7 @@ public class PrintStream extends FilterOutputStream */ public void println (float fnum) { - print(String.valueOf(fnum), true); + pw.println (fnum); } /** @@ -460,7 +396,7 @@ public class PrintStream extends FilterOutputStream */ public void println (double dnum) { - print(String.valueOf(dnum), true); + pw.println (dnum); } /** @@ -474,7 +410,7 @@ public class PrintStream extends FilterOutputStream */ public void println (Object obj) { - print(obj == null ? "null" : obj.toString(), true); + pw.println (obj); } /** @@ -487,7 +423,7 @@ public class PrintStream extends FilterOutputStream */ public void println (String str) { - print (str == null ? "null" : str, true); + pw.println (str); } /** @@ -498,10 +434,9 @@ public class PrintStream extends FilterOutputStream * * @param ch The char value to be printed */ - public synchronized void println (char ch) + public void println (char ch) { - work[0] = ch; - print(work, 0, 1, true); + pw.println (ch); } /** @@ -514,7 +449,7 @@ public class PrintStream extends FilterOutputStream */ public void println (char[] charArray) { - print(charArray, 0, charArray.length, true); + pw.println (charArray); } /** @@ -526,6 +461,10 @@ public class PrintStream extends FilterOutputStream */ public void write (int oneByte) { + // Sigh, we actually have to implement this method. Flush first so that + // things get written in the right order. + flush (); + try { out.write (oneByte); @@ -533,10 +472,6 @@ public class PrintStream extends FilterOutputStream if (auto_flush && oneByte == '\n') flush (); } - catch (InterruptedIOException iioe) - { - Thread.currentThread ().interrupt (); - } catch (IOException e) { setError (); @@ -553,16 +488,22 @@ public class PrintStream extends FilterOutputStream */ public void write (byte[] buffer, int offset, int len) { + // We actually have to implement this method too. Flush first so that + // things get written in the right order. + flush(); + try { out.write (buffer, offset, len); if (auto_flush) - flush (); - } - catch (InterruptedIOException iioe) - { - Thread.currentThread ().interrupt (); + for (int i = offset; i < len; i++) + if ((buffer [i] == '\r') + || (buffer [i] == '\n')) + { + flush (); + break; + } } catch (IOException e) { -- 2.30.2