Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/java.base/unix/classes/java/io/UnixFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,23 @@ public int prefixLength(String pathname) {
return pathname.startsWith("/") ? 1 : 0;
}

// Remove trailing '/' if present and there are at least two characters
private static String trimSeparator(String s) {
int len = s.length();
if (len > 1 && s.charAt(len - 1) == '/')
return s.substring(0, len - 1);
return s;
}

@Override
public String resolve(String parent, String child) {
if (child.isEmpty()) return parent;
if (child.charAt(0) == '/') {
if (parent.equals("/")) return child;
return parent + child;
return trimSeparator(parent + child);
}
if (parent.equals("/")) return parent + child;
return parent + '/' + child;
if (parent.equals("/")) return trimSeparator(parent + child);
return trimSeparator(parent + '/' + child);
}

@Override
Expand Down
7 changes: 7 additions & 0 deletions src/java.base/windows/classes/java/io/WinNTFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,13 @@ public String resolve(String parent, String child) {
theChars[parentEnd] = slash;
child.getChars(childStart, cn, theChars, parentEnd + 1);
}

// if present, strip trailing name separator unless after a ':'
if (theChars.length > 1
&& theChars[theChars.length - 1] == slash
&& theChars[theChars.length - 2] != ':')
return new String(theChars, 0, theChars.length - 1);

return new String(theChars);
}

Expand Down
19 changes: 16 additions & 3 deletions test/jdk/java/io/File/Cons.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023, 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
Expand All @@ -22,7 +22,7 @@
*/

/* @test
@bug 4131169 4168988
@bug 4131169 4168988 8290499
@summary Basic File constructor tests
*/

Expand Down Expand Up @@ -251,11 +251,17 @@ static void testUnix() throws Exception {
/* Two-arg constructors cases, string parent */
if (!old) {
ck2("//foo", "bar", "/foo", "bar", "/foo/bar");
ck2("/foo", "/", "/", "foo", "/foo");
ck2("//foo", "/", "/", "foo", "/foo");
ck2("/foo", "//", "/", "foo", "/foo");
}

/* Two-arg constructor cases, File parent */
if (!old) {
ck2f("//foo", "bar", "/foo", "bar", "/foo/bar");
ck2f("/foo", "/", "/", "foo", "/foo");
ck2f("//foo", "/", "/", "foo", "/foo");
ck2f("/foo", "//", "/", "foo", "/foo");
}

File f = new File("/foo");
Expand Down Expand Up @@ -296,10 +302,17 @@ static void testWin32() throws Exception {
ck2("z:/", "//foo", "z:/", "foo", "z:/foo");
ck2("z:/", "foo/", "z:/", "foo", "z:/foo");
ck2("//foo", "bar", "//foo", "bar", "//foo/bar");
ck2("z:", "", null, "", "z:");
ck2("z:/", "/", null, "", "z:/");
ck2("z:/", "a/b/c", "z:/a/b", "c", "z:/a/b/c");
ck2("z:/", "a/b/c/", "z:/a/b", "c", "z:/a/b/c");

/* Two-arg constructor cases, File parent */
ck2f("//foo", "bar", "//foo", "bar", "//foo/bar");

ck2f("z:", "", null, "", "z:");
ck2f("z:/", "/", null, "", "z:/");
ck2f("z:/", "a/b/c", "z:/a/b", "c", "z:/a/b/c");
ck2f("z:/", "a/b/c/", "z:/a/b", "c", "z:/a/b/c");
}
}

Expand Down