in src/main/java/org/apache/commons/io/file/PathUtils.java [819:868]
public static boolean fileContentEquals(final Path path1, final Path path2, final LinkOption[] linkOptions, final OpenOption[] openOptions)
throws IOException {
if (path1 == null && path2 == null) {
return true;
}
if (path1 == null || path2 == null) {
return false;
}
final Path nPath1 = path1.normalize();
final Path nPath2 = path2.normalize();
final boolean path1Exists = exists(nPath1, linkOptions);
if (path1Exists != exists(nPath2, linkOptions)) {
return false;
}
if (!path1Exists) {
// Two not existing files are equal?
// Same as FileUtils
return true;
}
if (Files.isDirectory(nPath1, linkOptions)) {
// don't compare directory contents.
throw new IOException("Can't compare directories, only files: " + nPath1);
}
if (Files.isDirectory(nPath2, linkOptions)) {
// don't compare directory contents.
throw new IOException("Can't compare directories, only files: " + nPath2);
}
if (Files.size(nPath1) != Files.size(nPath2)) {
// lengths differ, cannot be equal
return false;
}
if (isSameFileSystem(path1, path2) && path1.equals(path2)) {
// same file
return true;
}
// Faster:
try (RandomAccessFile raf1 = RandomAccessFileMode.READ_ONLY.create(path1.toRealPath(linkOptions));
RandomAccessFile raf2 = RandomAccessFileMode.READ_ONLY.create(path2.toRealPath(linkOptions))) {
return RandomAccessFiles.contentEquals(raf1, raf2);
} catch (final UnsupportedOperationException e) {
// Slower:
// Handle
// java.lang.UnsupportedOperationException
// at com.sun.nio.zipfs.ZipPath.toFile(ZipPath.java:656)
try (InputStream inputStream1 = Files.newInputStream(nPath1, openOptions);
InputStream inputStream2 = Files.newInputStream(nPath2, openOptions)) {
return IOUtils.contentEquals(inputStream1, inputStream2);
}
}
}