hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szets...@apache.org
Subject svn commit: r1142535 - in /hadoop/common/trunk/common: CHANGES.txt src/java/org/apache/hadoop/io/IOUtils.java src/test/core/org/apache/hadoop/io/TestIOUtils.java
Date Mon, 04 Jul 2011 02:39:14 GMT
Author: szetszwo
Date: Mon Jul  4 02:39:14 2011
New Revision: 1142535

URL: http://svn.apache.org/viewvc?rev=1142535&view=rev
Log:
HADOOP-7437. IOUtils.copybytes will suppress the stream closure exceptions.  Contributed by
Uma Maheswara Rao G

Modified:
    hadoop/common/trunk/common/CHANGES.txt
    hadoop/common/trunk/common/src/java/org/apache/hadoop/io/IOUtils.java
    hadoop/common/trunk/common/src/test/core/org/apache/hadoop/io/TestIOUtils.java

Modified: hadoop/common/trunk/common/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/CHANGES.txt?rev=1142535&r1=1142534&r2=1142535&view=diff
==============================================================================
--- hadoop/common/trunk/common/CHANGES.txt (original)
+++ hadoop/common/trunk/common/CHANGES.txt Mon Jul  4 02:39:14 2011
@@ -340,6 +340,9 @@ Trunk (unreleased changes)
     HADOOP-7428. IPC connection is orphaned with null 'out' member.
     (todd via eli)
 
+    HADOOP-7437. IOUtils.copybytes will suppress the stream closure exceptions.
+    (Uma Maheswara Rao G via szetszwo)
+
 Release 0.22.0 - Unreleased
 
   INCOMPATIBLE CHANGES

Modified: hadoop/common/trunk/common/src/java/org/apache/hadoop/io/IOUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/src/java/org/apache/hadoop/io/IOUtils.java?rev=1142535&r1=1142534&r2=1142535&view=diff
==============================================================================
--- hadoop/common/trunk/common/src/java/org/apache/hadoop/io/IOUtils.java (original)
+++ hadoop/common/trunk/common/src/java/org/apache/hadoop/io/IOUtils.java Mon Jul  4 02:39:14
2011
@@ -136,6 +136,12 @@ public class IOUtils {
         out.write(buf, 0, bytesRead);
         bytesRemaining -= bytesRead;
       }
+      if (close) {
+        out.close();
+        out = null;
+        in.close();
+        in = null;
+      }
     } finally {
       if (close) {
         closeStream(out);

Modified: hadoop/common/trunk/common/src/test/core/org/apache/hadoop/io/TestIOUtils.java
URL: http://svn.apache.org/viewvc/hadoop/common/trunk/common/src/test/core/org/apache/hadoop/io/TestIOUtils.java?rev=1142535&r1=1142534&r2=1142535&view=diff
==============================================================================
--- hadoop/common/trunk/common/src/test/core/org/apache/hadoop/io/TestIOUtils.java (original)
+++ hadoop/common/trunk/common/src/test/core/org/apache/hadoop/io/TestIOUtils.java Mon Jul
 4 02:39:14 2011
@@ -18,6 +18,9 @@
 
 package org.apache.hadoop.io;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -65,4 +68,46 @@ public class TestIOUtils {
     Mockito.verify(inputStream, Mockito.atMost(0)).close();
     Mockito.verify(outputStream, Mockito.atMost(0)).close();
   }
+  
+  @Test
+  public void testCopyBytesWithCountShouldCloseStreamsWhenCloseIsTrue()
+      throws Exception {
+    InputStream inputStream = Mockito.mock(InputStream.class);
+    OutputStream outputStream = Mockito.mock(OutputStream.class);
+    Mockito.doReturn(-1).when(inputStream).read(new byte[4096], 0, 1);
+    IOUtils.copyBytes(inputStream, outputStream, (long) 1, true);
+    Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
+    Mockito.verify(outputStream, Mockito.atLeastOnce()).close();
+  }
+
+  @Test
+  public void testCopyBytesWithCountShouldNotCloseStreamsWhenCloseIsFalse()
+      throws Exception {
+    InputStream inputStream = Mockito.mock(InputStream.class);
+    OutputStream outputStream = Mockito.mock(OutputStream.class);
+    Mockito.doReturn(-1).when(inputStream).read(new byte[4096], 0, 1);
+    IOUtils.copyBytes(inputStream, outputStream, (long) 1, false);
+    Mockito.verify(inputStream, Mockito.atMost(0)).close();
+    Mockito.verify(outputStream, Mockito.atMost(0)).close();
+  }
+
+  @Test
+  public void testCopyBytesWithCountShouldThrowOutTheStreamClosureExceptions()
+      throws Exception {
+    InputStream inputStream = Mockito.mock(InputStream.class);
+    OutputStream outputStream = Mockito.mock(OutputStream.class);
+    Mockito.doReturn(-1).when(inputStream).read(new byte[4096], 0, 1);
+    Mockito.doThrow(new IOException("Exception in closing the stream")).when(
+        outputStream).close();
+    try {
+      IOUtils.copyBytes(inputStream, outputStream, (long) 1, true);
+      fail("Should throw out the exception");
+    } catch (IOException e) {
+      assertEquals("Not throwing the expected exception.",
+          "Exception in closing the stream", e.getMessage());
+    }
+    Mockito.verify(inputStream, Mockito.atLeastOnce()).close();
+    Mockito.verify(outputStream, Mockito.atLeastOnce()).close();
+  }
+  
 }



Mime
View raw message