Browse Source

HADOOP-4325. SocketInputStream.read() should return -1 in case EOF.
(Raghu Angadi)


git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@701476 13f79535-47bb-0310-9956-ffa450edef68

Raghu Angadi 16 years ago
parent
commit
4f5cb81442

+ 3 - 0
CHANGES.txt

@@ -44,6 +44,9 @@ Trunk (unreleased changes)
     HADOOP-4207. Update derby jar file to release 10.4.2 release.
     (Prasad Chakka via dhruba)
 
+    HADOOP-4325. SocketInputStream.read() should return -1 in case EOF.
+    (Raghu Angadi)
+
 Release 0.19.0 - Unreleased
 
   INCOMPATIBLE CHANGES

+ 7 - 2
src/core/org/apache/hadoop/net/SocketInputStream.java

@@ -113,10 +113,15 @@ public class SocketInputStream extends InputStream
      * probably no need to optimize or encourage single byte read.
      */
     byte[] buf = new byte[1];
-    if (read(buf, 0, 1) > 0) {
+    int ret = read(buf, 0, 1);
+    if (ret > 0) {
       return (byte)buf[0];
     }
-    throw new IOException("Could not read from stream");
+    if (ret != -1) {
+      // unexpected
+      throw new IOException("Could not read from stream");
+    }
+    return ret;
   }
 
   public int read(byte[] b, int off, int len) throws IOException {

+ 6 - 2
src/test/org/apache/hadoop/net/TestSocketIOWithTimeout.java

@@ -132,12 +132,16 @@ public class TestSocketIOWithTimeout extends TestCase {
       //make sure the channels are still open
       assertTrue(source.isOpen());
       assertTrue(sink.isOpen());
+
+      out.close();
+      assertFalse(sink.isOpen());
+      
+      // close sink and expect -1 from source.read()
+      assertEquals(-1, in.read());
       
       // make sure close() closes the underlying channel.
       in.close();
       assertFalse(source.isOpen());
-      out.close();
-      assertFalse(sink.isOpen());
       
     } finally {
       if (source != null) {