Skip to content

Commit 405c34a

Browse files
committed
Improve performance of FileUtils.contentEquals(File, File) by about 60%,
see PathUtilsContentEqualsBenchmark.
1 parent 72cf479 commit 405c34a

2 files changed

Lines changed: 5 additions & 7 deletions

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ The <action> type attribute can be add,update,fix,remove.
8585
<action dev="ggregory" type="fix" due-to="Gary Gregory">
8686
Improve performance of PathUtils.fileContentEquals(Path, Path) by about 60%, see PathUtilsContentEqualsBenchmark.
8787
</action>
88+
<action dev="ggregory" type="fix" due-to="Gary Gregory">
89+
Improve performance of FileUtils.contentEquals(File, File) by about 60%, see PathUtilsContentEqualsBenchmark.
90+
</action>
8891
<action dev="ggregory" type="fix" due-to="Elliotte Rusty Harold">
8992
Remove unused test code #494.
9093
</action>

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,13 @@ private static void cleanDirectoryOnExit(final File directory) throws IOExceptio
353353
* This method checks to see if the two files are different lengths or if they point to the same file, before
354354
* resorting to byte-by-byte comparison of the contents.
355355
* </p>
356-
* <p>
357-
* Code origin: Avalon
358-
* </p>
359356
*
360357
* @param file1 the first file
361358
* @param file2 the second file
362359
* @return true if the content of the files are equal or they both don't exist, false otherwise
363360
* @throws IllegalArgumentException when an input is not a file.
364361
* @throws IOException If an I/O error occurs.
365-
* @see org.apache.commons.io.file.PathUtils#fileContentEquals(Path,Path,java.nio.file.LinkOption[],java.nio.file.OpenOption...)
362+
* @see PathUtils#fileContentEquals(Path,Path)
366363
*/
367364
public static boolean contentEquals(final File file1, final File file2) throws IOException {
368365
if (file1 == null && file2 == null) {
@@ -394,9 +391,7 @@ public static boolean contentEquals(final File file1, final File file2) throws I
394391
return true;
395392
}
396393

397-
try (InputStream input1 = Files.newInputStream(file1.toPath()); InputStream input2 = Files.newInputStream(file2.toPath())) {
398-
return IOUtils.contentEquals(input1, input2);
399-
}
394+
return PathUtils.fileContentEquals(file1.toPath(), file2.toPath());
400395
}
401396

402397
/**

0 commit comments

Comments
 (0)