changeset 14978:b138d2995b90

8253799: Make lists of normal filenames Reviewed-by: mbalao, andrew
author yan
date Fri, 18 Dec 2020 17:58:20 +0000
parents b423d9afa01f
children 4eae74c62a51
files src/share/classes/java/io/File.java
diffstat 1 files changed, 41 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/io/File.java	Thu Feb 04 00:32:43 2021 +0300
+++ b/src/share/classes/java/io/File.java	Fri Dec 18 17:58:20 2020 +0000
@@ -500,6 +500,9 @@
     public File getParentFile() {
         String p = this.getParent();
         if (p == null) return null;
+        if (getClass() != File.class) {
+            p = fs.normalize(p);
+        }
         return new File(p, this.prefixLength);
     }
 
@@ -572,6 +575,9 @@
      */
     public File getAbsoluteFile() {
         String absPath = getAbsolutePath();
+        if (getClass() != File.class) {
+            absPath = fs.normalize(absPath);
+        }
         return new File(absPath, fs.prefixLength(absPath));
     }
 
@@ -643,6 +649,9 @@
      */
     public File getCanonicalFile() throws IOException {
         String canonPath = getCanonicalPath();
+        if (getClass() != File.class) {
+            canonPath = fs.normalize(canonPath);
+        }
         return new File(canonPath, fs.prefixLength(canonPath));
     }
 
@@ -1126,6 +1135,34 @@
 
     /**
      * Returns an array of strings naming the files and directories in the
+     * directory denoted by this abstract pathname.  The strings are
+     * ensured to represent normalized paths.
+     *
+     * @return  An array of strings naming the files and directories in the
+     *          directory denoted by this abstract pathname.  The array will be
+     *          empty if the directory is empty.  Returns {@code null} if
+     *          this abstract pathname does not denote a directory, or if an
+     *          I/O error occurs.
+     *
+     * @throws  SecurityException
+     *          If a security manager exists and its {@link
+     *          SecurityManager#checkRead(String)} method denies read access to
+     *          the directory
+     */
+    private final String[] normalizedList() {
+        String[] s = list();
+        if (getClass() != File.class) {
+            String[] normalized = new String[s.length];
+            for (int i = 0; i < s.length; i++) {
+                normalized[i] = fs.normalize(s[i]);
+            }
+            s = normalized;
+        }
+        return s;
+    }
+
+    /**
+     * Returns an array of strings naming the files and directories in the
      * directory denoted by this abstract pathname that satisfy the specified
      * filter.  The behavior of this method is the same as that of the
      * {@link #list()} method, except that the strings in the returned array
@@ -1154,7 +1191,7 @@
      * @see java.nio.file.Files#newDirectoryStream(Path,String)
      */
     public String[] list(FilenameFilter filter) {
-        String names[] = list();
+        String names[] = normalizedList();
         if ((names == null) || (filter == null)) {
             return names;
         }
@@ -1206,7 +1243,7 @@
      * @since  1.2
      */
     public File[] listFiles() {
-        String[] ss = list();
+        String[] ss = normalizedList();
         if (ss == null) return null;
         int n = ss.length;
         File[] fs = new File[n];
@@ -1247,7 +1284,7 @@
      * @see java.nio.file.Files#newDirectoryStream(Path,String)
      */
     public File[] listFiles(FilenameFilter filter) {
-        String ss[] = list();
+        String ss[] = normalizedList();
         if (ss == null) return null;
         ArrayList<File> files = new ArrayList<>();
         for (String s : ss)
@@ -1285,7 +1322,7 @@
      * @see java.nio.file.Files#newDirectoryStream(Path,java.nio.file.DirectoryStream.Filter)
      */
     public File[] listFiles(FileFilter filter) {
-        String ss[] = list();
+        String ss[] = normalizedList();
         if (ss == null) return null;
         ArrayList<File> files = new ArrayList<>();
         for (String s : ss) {