This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch: PrintStream cleanups + 2449 fix


I'm checking this in on the trunk and the branch.  This fixes PR 2449
and also fixes some other places where PrintStream is inconsistent
with the online docs.

2001-04-02  Tom Tromey  <tromey@redhat.com>

	* java/io/PrintStream.java (out): Removed field.  Fixes PR
	java/2449.
	(write): Call flush, not out.flush, per spec.
	(close): Flush output stream, per spec.  Handle
	InterruptedIOException.
	(checkError): Likewise.
	(flush, print, write): Handle InterruptedIOException per spec.
	(PrintStream): Don't create BufferedOutputStream.
	(work_bytes): New field.
	(writeChars): Use work_bytes.  Don't assume `out' is a
	BufferedOutputStream.

Tom

Index: java/io/PrintStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/PrintStream.java,v
retrieving revision 1.9
diff -u -r1.9 PrintStream.java
--- PrintStream.java	2000/03/07 19:55:26	1.9
+++ PrintStream.java	2001/04/02 21:11:10
@@ -1,6 +1,6 @@
 // PrintStream.java - Print string representations
 
-/* Copyright (C) 1998, 1999  Free Software Foundation
+/* Copyright (C) 1998, 1999, 2001  Free Software Foundation
 
    This file is part of libgcj.
 
@@ -18,7 +18,7 @@
 
 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
  * "The Java Language Specification", ISBN 0-201-63451-1
- * Status:  Not finished.
+ * Status:  Believed complete and correct to 1.3
  */
 
 public class PrintStream extends FilterOutputStream
@@ -29,6 +29,7 @@
 
   public boolean checkError ()
   {
+    flush();
     return error;
   }
 
@@ -36,8 +37,13 @@
   {
     try
       {
+	flush();
 	out.close();
       }
+    catch (InterruptedIOException iioe)
+      {
+	Thread.currentThread().interrupt();
+      }
     catch (IOException e)
       {
 	setError ();
@@ -50,6 +56,10 @@
       {
 	out.flush();
       }
+    catch (InterruptedIOException iioe)
+      {
+	Thread.currentThread().interrupt();
+      }
     catch (IOException e)
       {
 	setError ();
@@ -66,6 +76,10 @@
 	if (auto_flush)
 	  flush();
       }
+    catch (InterruptedIOException iioe)
+      {
+	Thread.currentThread().interrupt();
+      }
     catch (IOException e)
       {
 	setError ();
@@ -83,32 +97,26 @@
 	if (auto_flush)
 	  flush();
       }
+    catch (InterruptedIOException iioe)
+      {
+	Thread.currentThread().interrupt();
+      }
     catch (IOException e)
       {
 	setError ();
       }
   }
 
-  /** Writes characters through to the inferior BufferedOutputStream. */
   private void writeChars(char[] buf, int offset, int count)
     throws IOException
   {
     while (count > 0)
       {
-	// We must flush if out.count == out.buf.length.
-	// It is probably a good idea to flush if out.buf is almost full.
-	// This test is an approximation for "almost full".
-	if (out.count + count >= out.buf.length)
-	  {
-	    out.flush();
-	    if (out.count != 0)
-	      throw new IOException("unable to flush output byte buffer");
-	  }
-	converter.setOutput(out.buf, out.count);
+	converter.setOutput(work_bytes, 0);
 	int converted = converter.write(buf, offset, count);
 	offset += converted;
 	count -= converted;
-	out.count = converter.count;
+	out.write(work_bytes, 0, converter.count);
       }
   }
 
@@ -117,20 +125,11 @@
   {
     while (count > 0)
       {
-	// We must flush if out.count == out.buf.length.
-	// It is probably a good idea to flush if out.buf is almost full.
-	// This test is an approximation for "almost full".
-	if (out.count + count >= out.buf.length)
-	  {
-	    out.flush();
-	    if (out.count != 0)
-	      throw new IOException("unable to flush output byte buffer");
-	  }
-	converter.setOutput(out.buf, out.count);
+	converter.setOutput(work_bytes, 0);
 	int converted = converter.write(str, offset, count, work);
 	offset += converted;
 	count -= converted;
-	out.count = converter.count;
+	out.write(work_bytes, 0, converter.count);
       }
   }
 
@@ -239,15 +238,6 @@
   public PrintStream (OutputStream out, boolean af)
   {
     super(out);
-    if (out instanceof BufferedOutputStream)
-      this.out = (BufferedOutputStream) out;
-    else
-      {
-        this.out = new BufferedOutputStream(out, 250);
-	/* PrintStream redefines "out". Explicitly reset FilterOutputStream's
-	 * "out" so that they're referring to the same thing. */
-	super.out = this.out;    
-      }
     converter = UnicodeToBytes.getDefaultEncoder();
     error = false;
     auto_flush = af;
@@ -264,7 +254,11 @@
       {
 	out.write(oneByte);
 	if (auto_flush && oneByte == '\n')
-	  out.flush();
+	  flush();
+      }
+    catch (InterruptedIOException iioe)
+      {
+	Thread.currentThread().interrupt();
       }
     catch (IOException e)
       {
@@ -278,7 +272,11 @@
       {
 	out.write(buffer, offset, count);
 	if (auto_flush)
-	  out.flush();
+	  flush();
+      }
+    catch (InterruptedIOException iioe)
+      {
+	Thread.currentThread().interrupt();
       }
     catch (IOException e)
       {
@@ -286,10 +284,12 @@
       }
   }
 
-  BufferedOutputStream out;
   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];
 
   // True if error occurred.
   private boolean error;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]