You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2008/12/04 04:30:29 UTC

svn commit: r723199 - in /commons/proper/io/trunk/src: java/org/apache/commons/io/FileUtils.java test/org/apache/commons/io/FileUtilsTestCase.java

Author: niallp
Date: Wed Dec  3 19:30:23 2008
New Revision: 723199

URL: http://svn.apache.org/viewvc?rev=723199&view=rev
Log:
Fix fir IO-175 IOUtils.doCopyFile() issues with very large files and closing file input streams - thanks to David Sitsky

Modified:
    commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
    commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java

Modified: commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java?rev=723199&r1=723198&r2=723199&view=diff
==============================================================================
--- commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/java/org/apache/commons/io/FileUtils.java Wed Dec  3 19:30:23 2008
@@ -98,6 +98,11 @@
     public static final long ONE_MB = ONE_KB * ONE_KB;
 
     /**
+     * The number of bytes in a 50 MB.
+     */
+    private static final long FIFTY_MB = ONE_MB * 50;
+
+    /**
      * The number of bytes in a gigabyte.
      */
     public static final long ONE_GB = ONE_KB * ONE_MB;
@@ -671,16 +676,27 @@
             throw new IOException("Destination '" + destFile + "' exists but is a directory");
         }
 
-        FileChannel input = new FileInputStream(srcFile).getChannel();
+        FileInputStream fis = null;
+        FileOutputStream fos = null;
+        FileChannel input = null;
+        FileChannel output = null;
         try {
-            FileChannel output = new FileOutputStream(destFile).getChannel();
-            try {
-                output.transferFrom(input, 0, input.size());
-            } finally {
-                IOUtils.closeQuietly(output);
+            fis = new FileInputStream(srcFile);
+            fos = new FileOutputStream(destFile);
+            input  = fis.getChannel();
+            output = fos.getChannel();
+            long size = input.size();
+            long pos = 0;
+            long count = 0;
+            while (pos < size) {
+                count = (size - pos) > FIFTY_MB ? FIFTY_MB : (size - pos);
+                pos += output.transferFrom(input, pos, count);
             }
         } finally {
+            IOUtils.closeQuietly(output);
+            IOUtils.closeQuietly(fos);
             IOUtils.closeQuietly(input);
+            IOUtils.closeQuietly(fis);
         }
 
         if (srcFile.length() != destFile.length()) {

Modified: commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java?rev=723199&r1=723198&r2=723199&view=diff
==============================================================================
--- commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java (original)
+++ commons/proper/io/trunk/src/test/org/apache/commons/io/FileUtilsTestCase.java Wed Dec  3 19:30:23 2008
@@ -643,6 +643,21 @@
             testFile1.lastModified() == destination.lastModified());*/  
     }
 
+    public void IGNOREtestCopyFileLarge() throws Exception {
+
+        File largeFile = new File(getTestDirectory(), "large.txt");
+        File destination = new File(getTestDirectory(), "copylarge.txt");
+
+        System.out.println("START:   " + new java.util.Date());
+        createFile(largeFile, FileUtils.ONE_GB);
+        System.out.println("CREATED: " + new java.util.Date());
+        FileUtils.copyFile(largeFile, destination);
+        System.out.println("COPIED:  " + new java.util.Date());
+
+        assertTrue("Check Exist", destination.exists());
+        assertTrue("Check Full copy", destination.length() == largeFile.length());
+    }
+
     public void testCopyFile2() throws Exception {
         File destination = new File(getTestDirectory(), "copy2.txt");