You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2013/06/02 15:17:21 UTC

svn commit: r1488708 - in /commons/proper/io/trunk/src: changes/changes.xml main/java/org/apache/commons/io/FileUtils.java

Author: sebb
Date: Sun Jun  2 13:17:02 2013
New Revision: 1488708

URL: http://svn.apache.org/r1488708
Log:
IO-383 FileUtils.doCopyFile caches the file size; needs to be documented

Modified:
    commons/proper/io/trunk/src/changes/changes.xml
    commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java

Modified: commons/proper/io/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/changes/changes.xml?rev=1488708&r1=1488707&r2=1488708&view=diff
==============================================================================
--- commons/proper/io/trunk/src/changes/changes.xml (original)
+++ commons/proper/io/trunk/src/changes/changes.xml Sun Jun  2 13:17:02 2013
@@ -47,6 +47,10 @@ The <action> type attribute can be add,u
   <body>
     <!-- The release date is the date RC is cut -->
     <release version="2.5" date="2013-??-??" description="New features and bug fixes.">    
+      <action issue="IO-383" dev="sebb" type="fix">
+         FileUtils.doCopyFile caches the file size; needs to be documented
+         Added Javadoc; show file lengths in exception message
+      </action>
       <action issue="IO-239" dev="sebb" type="update">
          Convert IOCase to a Java 1.5+ Enumeration
          [N.B. this is binary compatible]

Modified: commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?rev=1488708&r1=1488707&r2=1488708&view=diff
==============================================================================
--- commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java (original)
+++ commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java Sun Jun  2 13:17:02 2013
@@ -996,6 +996,7 @@ public class FileUtils {
      * @throws NullPointerException if source or destination is {@code null}
      * @throws IOException if source or destination is invalid
      * @throws IOException if an IO error occurs during copying
+     * @throws IOException if the output file length is not the same as the input file length after the copy completes
      * @see #copyFile(File, File, boolean)
      * @since 1.3
      */
@@ -1029,7 +1030,9 @@ public class FileUtils {
      * @throws NullPointerException if source or destination is {@code null}
      * @throws IOException if source or destination is invalid
      * @throws IOException if an IO error occurs during copying
+     * @throws IOException if the output file length is not the same as the input file length after the copy completes
      * @see #copyFileToDirectory(File, File)
+     * @see #copyFile(File, File, boolean)
      */
     public static void copyFile(final File srcFile, final File destFile) throws IOException {
         copyFile(srcFile, destFile, true);
@@ -1057,7 +1060,9 @@ public class FileUtils {
      * @throws NullPointerException if source or destination is {@code null}
      * @throws IOException if source or destination is invalid
      * @throws IOException if an IO error occurs during copying
+     * @throws IOException if the output file length is not the same as the input file length after the copy completes
      * @see #copyFileToDirectory(File, File, boolean)
+     * @see #doCopyFile(File, File, boolean)
      */
     public static void copyFile(final File srcFile, final File destFile,
             final boolean preserveFileDate) throws IOException {
@@ -1116,11 +1121,15 @@ public class FileUtils {
 
     /**
      * Internal copy file method.
+     * This caches the original file length, and throws an IOException 
+     * if the output file length is different from the current input file length.
+     * So it may fail if the file changes size. 
      *
      * @param srcFile  the validated source file, must not be {@code null}
      * @param destFile  the validated destination file, must not be {@code null}
      * @param preserveFileDate  whether to preserve the file date
      * @throws IOException if an error occurs
+     * @throws IOException if the output file length is not the same as the input file length after the copy completes
      */
     private static void doCopyFile(final File srcFile, final File destFile, final boolean preserveFileDate) throws IOException {
         if (destFile.exists() && destFile.isDirectory()) {
@@ -1150,9 +1159,11 @@ public class FileUtils {
             IOUtils.closeQuietly(fis);
         }
 
-        if (srcFile.length() != destFile.length()) {
+        final long srcLen = srcFile.length();
+        final long dstLen = destFile.length();
+        if (srcLen != dstLen) {
             throw new IOException("Failed to copy full contents from '" +
-                    srcFile + "' to '" + destFile + "'");
+                    srcFile + "' to '" + destFile + "' Expected length: " + srcLen +" Actual: " + dstLen);
         }
         if (preserveFileDate) {
             destFile.setLastModified(srcFile.lastModified());