You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by ay...@apache.org on 2007/06/18 17:38:03 UTC

svn commit: r548382 - in /harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/io/BufferedWriter.java test/java/tests/api/java/io/BufferedWriterTest.java

Author: ayza
Date: Mon Jun 18 08:38:02 2007
New Revision: 548382

URL: http://svn.apache.org/viewvc?view=rev&rev=548382
Log:
Applying patch from HARMONY-4178 ( [classlib][io] Compatibility: BufferedWriter.close() flushes the underlying writer)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java
    harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java?view=diff&rev=548382&r1=548381&r2=548382
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/BufferedWriter.java Mon Jun 18 08:38:02 2007
@@ -85,7 +85,7 @@
     public void close() throws IOException {
         synchronized (lock) {
             if (!isClosed()) {
-                flush();
+                flushInternal();
                 out.close();
                 buf = null;
                 out = null;
@@ -106,12 +106,19 @@
             if (isClosed()) {
                 throw new IOException(Msg.getString("K005d")); //$NON-NLS-1$
             }
-            if (pos > 0) {
-                out.write(buf, 0, pos);
-            }
-            pos = 0;
+            flushInternal();
             out.flush();
         }
+    }
+
+    /**
+     * Flushes the internal buffer.
+     */
+    private void flushInternal() throws IOException {
+        if (pos > 0) {
+            out.write(buf, 0, pos);
+        }
+        pos = 0;
     }
 
     /**

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java?view=diff&rev=548382&r1=548381&r2=548382
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java (original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/io/BufferedWriterTest.java Mon Jun 18 08:38:02 2007
@@ -19,6 +19,7 @@
 
 import java.io.BufferedWriter;
 import java.io.IOException;
+import java.io.Writer;
 
 import tests.support.Support_StringWriter;
 
@@ -51,6 +52,33 @@
 		assertTrue("Used in tests", true);
 	}
 
+    private static class MockWriter extends Writer { 
+        StringBuffer sb = new StringBuffer();
+        boolean flushCalled = false;
+        
+        public void write(char[] buf, int off, int len) throws IOException {
+            for (int i = off; i < off + len; i++) {
+                sb.append(buf[i]);
+            }
+        }
+
+        public void close() throws IOException {
+            System.out.println("close");
+        }
+
+        public void flush() throws IOException {
+            flushCalled = true;
+        }
+
+        public String getWritten() {
+            return sb.toString();
+        }
+
+        public boolean isFlushCalled() {
+            return flushCalled;
+        }
+    }
+
 	/**
 	 * @tests java.io.BufferedWriter#close()
 	 */
@@ -62,6 +90,22 @@
 		} catch (IOException e) {
 		}
 		assertTrue("Write after close", !sw.toString().equals(testString));
+
+        // Regression test for HARMONY-4178
+        try {
+            MockWriter mw = new MockWriter();
+            BufferedWriter bw = new BufferedWriter(mw);
+            bw.write('a');
+            bw.close();
+
+            // flush should not be called on underlying stream
+            assertFalse("Flush was called in the underlying stream", mw.isFlushCalled());
+
+            // on the other hand the BufferedWriter itself should flush the buffer
+            assertEquals("BufferdWriter do not flush itself before close", "a", mw.getWritten());
+        } catch (IOException ioe) {
+            fail("Exception during close test: " + ioe);
+        }
 	}
 
 	/**