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
*/
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
*/
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
* to the specified output sink. Note that this class is deprecated in
{
super (out);
- converter = UnicodeToBytes.getDefaultEncoder();
+ pw = new PrintWriter (out, auto_flush);
this.auto_flush = auto_flush;
}
{
super (out);
- converter = UnicodeToBytes.getEncoder (encoding);
+ pw = new PrintWriter (new OutputStreamWriter (out, encoding), auto_flush);
this.auto_flush = auto_flush;
}
*/
public boolean checkError ()
{
- flush();
- return error_occurred;
+ if (!closed)
+ pw.flush ();
+
+ return error_occurred | pw.checkError ();
}
/**
*/
public void close ()
{
- try
- {
- flush();
- out.close();
- }
- catch (InterruptedIOException iioe)
- {
- Thread.currentThread().interrupt();
- }
- catch (IOException e)
- {
- setError ();
- }
+ pw.close ();
+ closed = true;
}
/**
*/
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();
}
/**
*/
public void print (boolean bool)
{
- print(String.valueOf(bool), false);
+ pw.print (bool);
}
/**
*/
public void print (int inum)
{
- print(String.valueOf(inum), false);
+ pw.print (inum);
}
/**
*/
public void print (long lnum)
{
- print(String.valueOf(lnum), false);
+ pw.print (lnum);
}
/**
*/
public void print (float fnum)
{
- print(String.valueOf(fnum), false);
+ pw.print (fnum);
}
/**
*/
public void print (double dnum)
{
- print(String.valueOf(dnum), false);
+ pw.print (dnum);
}
/**
*/
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));
}
/**
*/
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 ();
}
/**
*
* @param ch The <code>char</code> 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 ();
}
/**
*/
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;
+ }
}
/**
*/
public void println ()
{
- print(line_separator, 0, line_separator.length, false);
+ pw.println ();
}
/**
*/
public void println (boolean bool)
{
- print(String.valueOf(bool), true);
+ pw.println (bool);
}
/**
*/
public void println (int inum)
{
- print(String.valueOf(inum), true);
+ pw.println (inum);
}
/**
*/
public void println (long lnum)
{
- print(String.valueOf(lnum), true);
+ pw.println (lnum);
}
/**
*/
public void println (float fnum)
{
- print(String.valueOf(fnum), true);
+ pw.println (fnum);
}
/**
*/
public void println (double dnum)
{
- print(String.valueOf(dnum), true);
+ pw.println (dnum);
}
/**
*/
public void println (Object obj)
{
- print(obj == null ? "null" : obj.toString(), true);
+ pw.println (obj);
}
/**
*/
public void println (String str)
{
- print (str == null ? "null" : str, true);
+ pw.println (str);
}
/**
*
* @param ch The <code>char</code> 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);
}
/**
*/
public void println (char[] charArray)
{
- print(charArray, 0, charArray.length, true);
+ pw.println (charArray);
}
/**
*/
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);
if (auto_flush && oneByte == '\n')
flush ();
}
- catch (InterruptedIOException iioe)
- {
- Thread.currentThread ().interrupt ();
- }
catch (IOException e)
{
setError ();
*/
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)
{