@@ -996,6 +996,7 @@ public static void copyFileToDirectory(final File srcFile, final File destDir) t
996996 * @throws NullPointerException if source or destination is {@code null}
997997 * @throws IOException if source or destination is invalid
998998 * @throws IOException if an IO error occurs during copying
999+ * @throws IOException if the output file length is not the same as the input file length after the copy completes
9991000 * @see #copyFile(File, File, boolean)
10001001 * @since 1.3
10011002 */
@@ -1029,7 +1030,9 @@ public static void copyFileToDirectory(final File srcFile, final File destDir, f
10291030 * @throws NullPointerException if source or destination is {@code null}
10301031 * @throws IOException if source or destination is invalid
10311032 * @throws IOException if an IO error occurs during copying
1033+ * @throws IOException if the output file length is not the same as the input file length after the copy completes
10321034 * @see #copyFileToDirectory(File, File)
1035+ * @see #copyFile(File, File, boolean)
10331036 */
10341037 public static void copyFile (final File srcFile , final File destFile ) throws IOException {
10351038 copyFile (srcFile , destFile , true );
@@ -1057,7 +1060,9 @@ public static void copyFile(final File srcFile, final File destFile) throws IOEx
10571060 * @throws NullPointerException if source or destination is {@code null}
10581061 * @throws IOException if source or destination is invalid
10591062 * @throws IOException if an IO error occurs during copying
1063+ * @throws IOException if the output file length is not the same as the input file length after the copy completes
10601064 * @see #copyFileToDirectory(File, File, boolean)
1065+ * @see #doCopyFile(File, File, boolean)
10611066 */
10621067 public static void copyFile (final File srcFile , final File destFile ,
10631068 final boolean preserveFileDate ) throws IOException {
@@ -1116,11 +1121,15 @@ public static long copyFile(final File input, final OutputStream output) throws
11161121
11171122 /**
11181123 * Internal copy file method.
1124+ * This caches the original file length, and throws an IOException
1125+ * if the output file length is different from the current input file length.
1126+ * So it may fail if the file changes size.
11191127 *
11201128 * @param srcFile the validated source file, must not be {@code null}
11211129 * @param destFile the validated destination file, must not be {@code null}
11221130 * @param preserveFileDate whether to preserve the file date
11231131 * @throws IOException if an error occurs
1132+ * @throws IOException if the output file length is not the same as the input file length after the copy completes
11241133 */
11251134 private static void doCopyFile (final File srcFile , final File destFile , final boolean preserveFileDate ) throws IOException {
11261135 if (destFile .exists () && destFile .isDirectory ()) {
@@ -1150,9 +1159,11 @@ private static void doCopyFile(final File srcFile, final File destFile, final bo
11501159 IOUtils .closeQuietly (fis );
11511160 }
11521161
1153- if (srcFile .length () != destFile .length ()) {
1162+ final long srcLen = srcFile .length ();
1163+ final long dstLen = destFile .length ();
1164+ if (srcLen != dstLen ) {
11541165 throw new IOException ("Failed to copy full contents from '" +
1155- srcFile + "' to '" + destFile + "'" );
1166+ srcFile + "' to '" + destFile + "' Expected length: " + srcLen + " Actual: " + dstLen );
11561167 }
11571168 if (preserveFileDate ) {
11581169 destFile .setLastModified (srcFile .lastModified ());
0 commit comments