Browse Source

Minor update to HADOOP-7429.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1141415 13f79535-47bb-0310-9956-ffa450edef68
Eli Collins 14 năm trước cách đây
mục cha
commit
ad95cb1f0d
1 tập tin đã thay đổi với 18 bổ sung10 xóa
  1. 18 10
      common/src/java/org/apache/hadoop/io/IOUtils.java

+ 18 - 10
common/src/java/org/apache/hadoop/io/IOUtils.java

@@ -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);
+      }
     }
   }