You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by el...@apache.org on 2011/06/30 09:04:58 UTC

svn commit: r1141415 - /hadoop/common/trunk/common/src/java/org/apache/hadoop/io/IOUtils.java

Author: eli
Date: Thu Jun 30 07:04:58 2011
New Revision: 1141415

URL: http://svn.apache.org/viewvc?rev=1141415&view=rev
Log:
Minor update to HADOOP-7429.

Modified:
    hadoop/common/trunk/common/src/java/org/apache/hadoop/io/IOUtils.java

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=1141415&r1=1141414&r2=1141415&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 Thu Jun 30 07:04:58 2011
@@ -115,24 +115,32 @@ public class IOUtils {
    * @param in InputStream to read from
    * @param out OutputStream to write to
    * @param count number of bytes to copy
+   * @param close whether to close the streams
    * @throws IOException if bytes can not be read or written
    */
-  public static void copyBytes(InputStream in, OutputStream out, long count)
-      throws IOException {
+  public static void copyBytes(InputStream in, OutputStream out, long count,
+      boolean close) throws IOException {
     byte buf[] = new byte[4096];
     long bytesRemaining = count;
     int bytesRead;
 
-    while (bytesRemaining > 0) {
-      int bytesToRead = (int)
-        (bytesRemaining < buf.length ? bytesRemaining : buf.length);
+    try {
+      while (bytesRemaining > 0) {
+        int bytesToRead = (int)
+          (bytesRemaining < buf.length ? bytesRemaining : buf.length);
 
-      bytesRead = in.read(buf, 0, bytesToRead);
-      if (bytesRead == -1)
-        break;
+        bytesRead = in.read(buf, 0, bytesToRead);
+        if (bytesRead == -1)
+          break;
 
-      out.write(buf, 0, bytesRead);
-      bytesRemaining -= bytesRead;
+        out.write(buf, 0, bytesRead);
+        bytesRemaining -= bytesRead;
+      }
+    } finally {
+      if (close) {
+        closeStream(out);
+        closeStream(in);
+      }
     }
   }