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

This commit is contained in:
Akira Ajisaka 2019-04-02 09:16:32 +09:00
parent cf268114c9
commit aaaf856f4b
No known key found for this signature in database
GPG Key ID: C1EDBB9CA400FD50
2 changed files with 7 additions and 2 deletions

View File

@ -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 static Path mergePaths(Path path1, Path path2) {
* @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.

View File

@ -121,7 +121,9 @@ public void testNormalize() throws URISyntaxException {
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) {