< prev index next >

src/java.base/share/classes/java/nio/file/Files.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -1521,10 +1521,88 @@
     public static boolean isSameFile(Path path, Path path2) throws IOException {
         return provider(path).isSameFile(path, path2);
     }
 
     /**
+     * Tests if the content of two files is identical. This method extends the
+     * {@link #isSameFile(java.nio.file.Path, java.nio.file.Path) isSameFile}
+     * method to further compare the content of the two files.
+     *
+     * <p> It always returns true when path and path2:
+     *
+     * <ul>
+     *     <li> point to the same object, even if the file does not exist;
+     *     </li>
+     *     <li> are equals as determined by
+     * {@link #isSameFile(java.nio.file.Path, java.nio.file.Path) isSameFile(path, path2)}
+     *     </li>
+     * </ul>
+     *
+     * <p> If {@code isSameFile(path, path2)} returns false, this method will proceed
+     * to read the files and compare them byte by byte to determine if they contain
+     * the same content. It is assumed that the two files remain static during
+     * the comparison process.
+     *
+     *
+     * @param   path
+     *          the path to a file
+     * @param   path2
+     *          the path to another file
+     *
+     * @return  a boolean indicating whether path and path2 locate the same file or
+     *          their contents are identical.
+     *
+     * @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 11
+     */
+    public static boolean isSameContent(Path path, Path path2) throws IOException {
+        if (isSameFile(path, path2)) {
+            return true;
+        }
+
+        final long size = Files.size(path);
+        if (size != Files.size(path2)) {
+            return false;
+        }
+
+        try (SeekableByteChannel sbc = Files.newByteChannel(path);
+            InputStream in = Channels.newInputStream(sbc);
+            SeekableByteChannel sbc2 = Files.newByteChannel(path2);
+            InputStream in2 = Channels.newInputStream(sbc2);) {
+
+            byte[] buf = new byte[BUFFER_SIZE];
+            byte[] buf2 = new byte[BUFFER_SIZE];
+            int n, n2;
+            while ((n = in.read(buf)) > 0) {
+                n2 = in2.read(buf2);
+                // unless the file is changed, n and n2 shall equal since in the previous
+                // step we've made sure the file sizes are equal
+                if (n != n2) {
+                    return false;
+                }
+
+                if (n == BUFFER_SIZE) {
+                    if (!Arrays.equals(buf, buf2)) {
+                        return false;
+                    }
+                } else {
+                    if (!Arrays.equals(buf, 0, n, buf2, 0, n2)) {
+                        return false;
+                    }
+                }
+            }
+        }
+        return true;
+    }
+
+    /**
      * Tells whether or not a file is considered <em>hidden</em>. 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
      * period character ('.'). On Windows a file is considered hidden if it
      * isn't a directory and the DOS {@link DosFileAttributes#isHidden hidden}
< prev index next >