--- old/src/java.base/share/classes/java/nio/file/Files.java 2018-10-16 10:45:11.883926490 -0700 +++ new/src/java.base/share/classes/java/nio/file/Files.java 2018-10-16 10:45:11.202853615 -0700 @@ -91,6 +91,9 @@ */ public final class Files { + // buffer size used for reading and writing + private static final int BUFFER_SIZE = 8192; + private Files() { } /** @@ -1531,6 +1534,90 @@ } /** + * Finds and returns the position of the first mismatched byte in the content + * of two files, or {@code -1L} if there is no mismatch. The position will be + * in the inclusive range of {@code 0L} up to the size (in bytes) of the + * smaller file. + * + *

Two files are considered to match if they satisfy one of the following + * conditions: + *

+ * + *

Otherwise there is a mismatch between the two files and the value + * returned by this method is: + *

+ * + *

This method may not be atomic with respect to other file system + * operations. This method is always reflexive (for {@code Path f}, + * {@code mismatch(f,f)} returns {@code -1L}). If the file system and files + * remain static, then this method is symmetric (for two {@code Paths f} + * and {@code g}, {@code mismatch(f,g)} will return the same value as + * {@code mismatch(g,f)}). + * + * @param path + * the path to the first file + * @param path2 + * the path to the second file + * + * @return the position of the first mismatch or {@code -1L} if no mismatch + * + * @throws IOException + * if an I/O error occurs + * @throws SecurityException + * In the case of the default provider, and a security manager is + * installed, the {@link SecurityManager#checkRead(String) checkRead} + * method is invoked to check read access to both files. + * + * @since 12 + */ + public static long mismatch(Path path, Path path2) throws IOException { + if (isSameFile(path, path2)) { + return -1; + } + byte[] buffer1 = new byte[Files.BUFFER_SIZE]; + byte[] buffer2 = new byte[Files.BUFFER_SIZE]; + try (InputStream in1 = Files.newInputStream(path); + InputStream in2 = Files.newInputStream(path2);) { + long totalRead = 0; + while (true) { + int nRead1 = in1.readNBytes(buffer1, 0, Files.BUFFER_SIZE); + int nRead2 = in2.readNBytes(buffer2, 0, Files.BUFFER_SIZE); + if (nRead1 == 0 && nRead2 == 0) { + // both files reached EOF + return -1; + } else if (nRead1 == 0 || nRead2 == 0) { + // one but not both files reached EOF + return totalRead; + } else if (nRead1 != nRead2) { + // there's always a mismatch when nRead1 != nRead2 + return totalRead + Arrays.mismatch(buffer1, 0, nRead1, buffer2, 0, nRead2); + } else { + int i = Arrays.mismatch(buffer1, 0, nRead1, buffer2, 0, nRead1); + if (i > -1) { + return totalRead + i; + } + if (nRead1 < Files.BUFFER_SIZE) { + // we've reached the end of the files, but found no mismatch + return -1; + } + totalRead += nRead1; + } + } + } + } + + /** * Tells whether or not a file is considered hidden. The exact * definition of hidden is platform or provider dependent. On UNIX for * example a file is considered to be hidden if its name begins with a @@ -2802,8 +2889,6 @@ // -- Utility methods for simple usages -- - // buffer size used for reading and writing - private static final int BUFFER_SIZE = 8192; /** * Opens a file for reading, returning a {@code BufferedReader} that may be