Commit fbd98522 by Michael Koch Committed by Michael Koch

PrintStream.java: Merged version from classpath.

2003-06-10  Michael Koch  <konqueror@gmx.de>

	* 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
parent f80f73e1
2003-06-10 Michael Koch <konqueror@gmx.de>
* 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 <tromey@redhat.com> 2003-06-09 Tom Tromey <tromey@redhat.com>
* gnu/gcj/xlib/natFont.cc (getAscent): Correctly access "ascent" * gnu/gcj/xlib/natFont.cc (getAscent): Correctly access "ascent"
......
...@@ -38,8 +38,6 @@ exception statement from your version. */ ...@@ -38,8 +38,6 @@ exception statement from your version. */
package java.io; package java.io;
import gnu.gcj.convert.UnicodeToBytes;
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
* "The Java Language Specification", ISBN 0-201-63451-1 * "The Java Language Specification", ISBN 0-201-63451-1
* Status: Believed complete and correct to 1.3 * Status: Believed complete and correct to 1.3
...@@ -66,20 +64,15 @@ import gnu.gcj.convert.UnicodeToBytes; ...@@ -66,20 +64,15 @@ import gnu.gcj.convert.UnicodeToBytes;
*/ */
public class PrintStream extends FilterOutputStream public class PrintStream extends FilterOutputStream
{ {
/* Notice the implementation is quite similar to OutputStreamWriter. /*
* This leads to some minor duplication, because neither inherits * Ok, why is this class deprecated? It could easily have been extended
* from the other, and we want to maximize performance. */ * to support character encodings. In fact, PrintWriter is basically a
* superset of this except for the write() methods. So let's do something
// Line separator string. * tricky here and just redirect calls in this class to a hidden PrintWriter
private static final char[] line_separator * instance. All the functionality goes there since that is the 'real'
= System.getProperty("line.separator").toCharArray(); * class. The big win of doing this way is that the default character
* encoding is done automagicially by the PrintWriter tree!
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];
/** /**
* This boolean indicates whether or not an error has ever occurred * This boolean indicates whether or not an error has ever occurred
...@@ -94,6 +87,16 @@ public class PrintStream extends FilterOutputStream ...@@ -94,6 +87,16 @@ public class PrintStream extends FilterOutputStream
private boolean auto_flush; 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 <code>PrintStream</code> object to write * This method intializes a new <code>PrintStream</code> object to write
* to the specified output sink. Note that this class is deprecated in * to the specified output sink. Note that this class is deprecated in
* favor of <code>PrintWriter</code>. * favor of <code>PrintWriter</code>.
...@@ -125,7 +128,7 @@ public class PrintStream extends FilterOutputStream ...@@ -125,7 +128,7 @@ public class PrintStream extends FilterOutputStream
{ {
super (out); super (out);
converter = UnicodeToBytes.getDefaultEncoder(); pw = new PrintWriter (out, auto_flush);
this.auto_flush = auto_flush; this.auto_flush = auto_flush;
} }
...@@ -150,7 +153,7 @@ public class PrintStream extends FilterOutputStream ...@@ -150,7 +153,7 @@ public class PrintStream extends FilterOutputStream
{ {
super (out); super (out);
converter = UnicodeToBytes.getEncoder (encoding); pw = new PrintWriter (new OutputStreamWriter (out, encoding), auto_flush);
this.auto_flush = auto_flush; this.auto_flush = auto_flush;
} }
...@@ -165,8 +168,10 @@ public class PrintStream extends FilterOutputStream ...@@ -165,8 +168,10 @@ public class PrintStream extends FilterOutputStream
*/ */
public boolean checkError () public boolean checkError ()
{ {
flush(); if (!closed)
return error_occurred; pw.flush ();
return error_occurred | pw.checkError ();
} }
/** /**
...@@ -183,19 +188,8 @@ public class PrintStream extends FilterOutputStream ...@@ -183,19 +188,8 @@ public class PrintStream extends FilterOutputStream
*/ */
public void close () public void close ()
{ {
try pw.close ();
{ closed = true;
flush();
out.close();
}
catch (InterruptedIOException iioe)
{
Thread.currentThread().interrupt();
}
catch (IOException e)
{
setError ();
}
} }
/** /**
...@@ -204,85 +198,7 @@ public class PrintStream extends FilterOutputStream ...@@ -204,85 +198,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void flush () public void flush ()
{ {
try pw.flush();
{
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);
}
} }
/** /**
...@@ -294,7 +210,7 @@ public class PrintStream extends FilterOutputStream ...@@ -294,7 +210,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void print (boolean bool) public void print (boolean bool)
{ {
print(String.valueOf(bool), false); pw.print (bool);
} }
/** /**
...@@ -305,7 +221,7 @@ public class PrintStream extends FilterOutputStream ...@@ -305,7 +221,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void print (int inum) public void print (int inum)
{ {
print(String.valueOf(inum), false); pw.print (inum);
} }
/** /**
...@@ -316,7 +232,7 @@ public class PrintStream extends FilterOutputStream ...@@ -316,7 +232,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void print (long lnum) public void print (long lnum)
{ {
print(String.valueOf(lnum), false); pw.print (lnum);
} }
/** /**
...@@ -327,7 +243,7 @@ public class PrintStream extends FilterOutputStream ...@@ -327,7 +243,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void print (float fnum) public void print (float fnum)
{ {
print(String.valueOf(fnum), false); pw.print (fnum);
} }
/** /**
...@@ -338,7 +254,7 @@ public class PrintStream extends FilterOutputStream ...@@ -338,7 +254,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void print (double dnum) public void print (double dnum)
{ {
print(String.valueOf(dnum), false); pw.print (dnum);
} }
/** /**
...@@ -350,7 +266,9 @@ public class PrintStream extends FilterOutputStream ...@@ -350,7 +266,9 @@ public class PrintStream extends FilterOutputStream
*/ */
public void print (Object obj) 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 ...@@ -361,7 +279,12 @@ public class PrintStream extends FilterOutputStream
*/ */
public void print (String str) 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 ...@@ -370,10 +293,14 @@ public class PrintStream extends FilterOutputStream
* *
* @param ch The <code>char</code> value to be printed * @param ch The <code>char</code> value to be printed
*/ */
public synchronized void print (char ch) public void print (char ch)
{ {
work[0] = ch; pw.print (ch);
print(work, 0, 1, false);
if (auto_flush)
if ((ch == '\r')
|| (ch == '\n'))
flush ();
} }
/** /**
...@@ -384,7 +311,16 @@ public class PrintStream extends FilterOutputStream ...@@ -384,7 +311,16 @@ public class PrintStream extends FilterOutputStream
*/ */
public void print (char[] charArray) 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 ...@@ -394,7 +330,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void println () public void println ()
{ {
print(line_separator, 0, line_separator.length, false); pw.println ();
} }
/** /**
...@@ -408,7 +344,7 @@ public class PrintStream extends FilterOutputStream ...@@ -408,7 +344,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void println (boolean bool) public void println (boolean bool)
{ {
print(String.valueOf(bool), true); pw.println (bool);
} }
/** /**
...@@ -421,7 +357,7 @@ public class PrintStream extends FilterOutputStream ...@@ -421,7 +357,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void println (int inum) public void println (int inum)
{ {
print(String.valueOf(inum), true); pw.println (inum);
} }
/** /**
...@@ -434,7 +370,7 @@ public class PrintStream extends FilterOutputStream ...@@ -434,7 +370,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void println (long lnum) public void println (long lnum)
{ {
print(String.valueOf(lnum), true); pw.println (lnum);
} }
/** /**
...@@ -447,7 +383,7 @@ public class PrintStream extends FilterOutputStream ...@@ -447,7 +383,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void println (float fnum) public void println (float fnum)
{ {
print(String.valueOf(fnum), true); pw.println (fnum);
} }
/** /**
...@@ -460,7 +396,7 @@ public class PrintStream extends FilterOutputStream ...@@ -460,7 +396,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void println (double dnum) public void println (double dnum)
{ {
print(String.valueOf(dnum), true); pw.println (dnum);
} }
/** /**
...@@ -474,7 +410,7 @@ public class PrintStream extends FilterOutputStream ...@@ -474,7 +410,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void println (Object obj) public void println (Object obj)
{ {
print(obj == null ? "null" : obj.toString(), true); pw.println (obj);
} }
/** /**
...@@ -487,7 +423,7 @@ public class PrintStream extends FilterOutputStream ...@@ -487,7 +423,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void println (String str) public void println (String str)
{ {
print (str == null ? "null" : str, true); pw.println (str);
} }
/** /**
...@@ -498,10 +434,9 @@ public class PrintStream extends FilterOutputStream ...@@ -498,10 +434,9 @@ public class PrintStream extends FilterOutputStream
* *
* @param ch The <code>char</code> value to be printed * @param ch The <code>char</code> value to be printed
*/ */
public synchronized void println (char ch) public void println (char ch)
{ {
work[0] = ch; pw.println (ch);
print(work, 0, 1, true);
} }
/** /**
...@@ -514,7 +449,7 @@ public class PrintStream extends FilterOutputStream ...@@ -514,7 +449,7 @@ public class PrintStream extends FilterOutputStream
*/ */
public void println (char[] charArray) public void println (char[] charArray)
{ {
print(charArray, 0, charArray.length, true); pw.println (charArray);
} }
/** /**
...@@ -526,6 +461,10 @@ public class PrintStream extends FilterOutputStream ...@@ -526,6 +461,10 @@ public class PrintStream extends FilterOutputStream
*/ */
public void write (int oneByte) 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 try
{ {
out.write (oneByte); out.write (oneByte);
...@@ -533,10 +472,6 @@ public class PrintStream extends FilterOutputStream ...@@ -533,10 +472,6 @@ public class PrintStream extends FilterOutputStream
if (auto_flush && oneByte == '\n') if (auto_flush && oneByte == '\n')
flush (); flush ();
} }
catch (InterruptedIOException iioe)
{
Thread.currentThread ().interrupt ();
}
catch (IOException e) catch (IOException e)
{ {
setError (); setError ();
...@@ -553,16 +488,22 @@ public class PrintStream extends FilterOutputStream ...@@ -553,16 +488,22 @@ public class PrintStream extends FilterOutputStream
*/ */
public void write (byte[] buffer, int offset, int len) 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 try
{ {
out.write (buffer, offset, len); out.write (buffer, offset, len);
if (auto_flush) if (auto_flush)
flush (); for (int i = offset; i < len; i++)
} if ((buffer [i] == '\r')
catch (InterruptedIOException iioe) || (buffer [i] == '\n'))
{ {
Thread.currentThread ().interrupt (); flush ();
break;
}
} }
catch (IOException e) catch (IOException e)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment