Skip to content

Commit

Permalink
Improve performance of FileUtils.contentEquals(File, File) by about 60%,
Browse files Browse the repository at this point in the history
see PathUtilsContentEqualsBenchmark.
  • Loading branch information
garydgregory committed Oct 8, 2023
1 parent 72cf479 commit 405c34a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="fix" due-to="Gary Gregory">
Improve performance of PathUtils.fileContentEquals(Path, Path) by about 60%, see PathUtilsContentEqualsBenchmark.
</action>
<action dev="ggregory" type="fix" due-to="Gary Gregory">
Improve performance of FileUtils.contentEquals(File, File) by about 60%, see PathUtilsContentEqualsBenchmark.
</action>
<action dev="ggregory" type="fix" due-to="Elliotte Rusty Harold">
Remove unused test code #494.
</action>
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/org/apache/commons/io/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,16 +353,13 @@ private static void cleanDirectoryOnExit(final File directory) throws IOExceptio
* This method checks to see if the two files are different lengths or if they point to the same file, before
* resorting to byte-by-byte comparison of the contents.
* </p>
* <p>
* Code origin: Avalon
* </p>
*
* @param file1 the first file
* @param file2 the second file
* @return true if the content of the files are equal or they both don't exist, false otherwise
* @throws IllegalArgumentException when an input is not a file.
* @throws IOException If an I/O error occurs.
* @see org.apache.commons.io.file.PathUtils#fileContentEquals(Path,Path,java.nio.file.LinkOption[],java.nio.file.OpenOption...)
* @see PathUtils#fileContentEquals(Path,Path)
*/
public static boolean contentEquals(final File file1, final File file2) throws IOException {
if (file1 == null && file2 == null) {
Expand Down Expand Up @@ -394,9 +391,7 @@ public static boolean contentEquals(final File file1, final File file2) throws I
return true;
}

try (InputStream input1 = Files.newInputStream(file1.toPath()); InputStream input2 = Files.newInputStream(file2.toPath())) {
return IOUtils.contentEquals(input1, input2);
}
return PathUtils.fileContentEquals(file1.toPath(), file2.toPath());
}

/**
Expand Down

0 comments on commit 405c34a

Please sign in to comment.