Skip to content

Commit 7f15b0e

Browse files
committed
IO-383 FileUtils.doCopyFile caches the file size; needs to be documented
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1488708 13f79535-47bb-0310-9956-ffa450edef68
1 parent 30c4412 commit 7f15b0e

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

src/changes/changes.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="2013-??-??" description="New features and bug fixes.">
50+
<action issue="IO-383" dev="sebb" type="fix">
51+
FileUtils.doCopyFile caches the file size; needs to be documented
52+
Added Javadoc; show file lengths in exception message
53+
</action>
5054
<action issue="IO-239" dev="sebb" type="update">
5155
Convert IOCase to a Java 1.5+ Enumeration
5256
[N.B. this is binary compatible]

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)