You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by re...@apache.org on 2010/09/21 09:39:05 UTC

svn commit: r999260 - in /harmony/enhanced/java/trunk/classlib/modules/luni/src: main/java/java/io/PipedInputStream.java test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java

Author: regisxu
Date: Tue Sep 21 07:39:05 2010
New Revision: 999260

URL: http://svn.apache.org/viewvc?rev=999260&view=rev
Log:
Apply patch for HARMONY-6636: [classlib][luni]PipedInputStream should not throw exception after the write end close

Modified:
    harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/PipedInputStream.java
    harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/PipedInputStream.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/PipedInputStream.java?rev=999260&r1=999259&r2=999260&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/PipedInputStream.java (original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/main/java/java/io/PipedInputStream.java Tue Sep 21 07:39:05 2010
@@ -158,6 +158,11 @@ public class PipedInputStream extends In
             throw new IOException(Messages.getString("luni.CC")); //$NON-NLS-1$
         }
 
+        if (isClosed && in == -1) {
+            // write end closed and no more need to read
+            return -1;
+        }
+
         if (lastWriter != null && !lastWriter.isAlive() && (in < 0)) {
             // luni.CD=Write end dead
             throw new IOException(Messages.getString("luni.CD")); //$NON-NLS-1$
@@ -246,6 +251,11 @@ public class PipedInputStream extends In
             return 0;
         }
 
+        if (isClosed && in == -1) {
+            // write end closed and no more need to read
+            return -1;
+        }
+
         if (!isConnected) {
             // luni.CB=Not connected
             throw new IOException(Messages.getString("luni.CB")); //$NON-NLS-1$

Modified: harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java?rev=999260&r1=999259&r2=999260&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java (original)
+++ harmony/enhanced/java/trunk/classlib/modules/luni/src/test/api/common/org/apache/harmony/luni/tests/java/io/PipedInputStreamTest.java Tue Sep 21 07:39:05 2010
@@ -361,6 +361,40 @@ public class PipedInputStreamTest extend
                 myRun.pass);
     }
 
+    static class Worker extends Thread {
+        PipedOutputStream out;
+
+        Worker(PipedOutputStream pos) {
+            this.out = pos;
+        }
+
+        public void run() {
+            try {
+                out.write(20);
+                out.close();
+                Thread.sleep(5000);
+            } catch (Exception e) {
+            }
+        }
+    }
+
+    public void test_read_after_write_close() throws Exception{
+        PipedInputStream in = new PipedInputStream();
+        PipedOutputStream out = new PipedOutputStream();
+        in.connect(out);
+        Thread worker = new Worker(out);
+        worker.start();
+        Thread.sleep(2000);
+        assertEquals("Should read 20.", 20, in.read());
+        worker.join();
+        assertEquals("Write end is closed, should return -1", -1, in.read());
+        byte[] buf = new byte[1];
+        assertEquals("Write end is closed, should return -1", -1, in.read(buf, 0, 1));
+        assertEquals("Buf len 0 should return first", 0, in.read(buf, 0, 0));
+        in.close();
+        out.close();
+    }
+
     /**
      * Tears down the fixture, for example, close a network connection. This
      * method is called after a test is executed.