Parcourir la source

HADOOP-16226. new Path(String str) does not remove all the trailing slashes of str

Akira Ajisaka il y a 6 ans
Parent
commit
aaaf856f4b

+ 5 - 2
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/Path.java

@@ -70,6 +70,9 @@ public class Path
   private static final Pattern HAS_DRIVE_LETTER_SPECIFIER =
       Pattern.compile("^/?[a-zA-Z]:");
 
+  /** Pre-compiled regular expressions to detect duplicated slashes. */
+  private static final Pattern SLASHES = Pattern.compile("/+");
+
   private static final long serialVersionUID = 0xad00f;
 
   private URI uri; // a hierarchical uri
@@ -291,8 +294,8 @@ public class Path
    * @return the normalized path string
    */
   private static String normalizePath(String scheme, String path) {
-    // Remove double forward slashes.
-    path = StringUtils.replace(path, "//", "/");
+    // Remove duplicated slashes.
+    path = SLASHES.matcher(path).replaceAll("/");
 
     // Remove backslashes if this looks like a Windows path. Avoid
     // the substitution if it looks like a non-local URI.

+ 2 - 0
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestPath.java

@@ -121,7 +121,9 @@ public class TestPath {
     assertEquals("/foo", new Path("/foo/").toString());
     assertEquals("foo", new Path("foo/").toString());
     assertEquals("foo", new Path("foo//").toString());
+    assertEquals("foo", new Path("foo///").toString());
     assertEquals("foo/bar", new Path("foo//bar").toString());
+    assertEquals("foo/bar", new Path("foo///bar").toString());
     assertEquals("hdfs://foo/foo2/bar/baz/",
         new Path(new URI("hdfs://foo//foo2///bar/baz///")).toString());
     if (Path.WINDOWS) {