Strange "..\" on Windows
Wang Weijun
weijun.wang at oracle.com
Sat Oct 24 14:26:30 UTC 2015
I found 2 problems in relativize() of WindowsPath.java:
401 // skip matching names
.... now "i" points to the first non match.
410 // append ..\ for remaining names in the base
411 StringBuilder result = new StringBuilder();
412 for (int j=i; j<bn; j++) {
413 result.append("..\\");
414 }
415
416 // append remaining names in child
417 for (int j=i; j<cn; j++) {
418 result.append(other.getName(j).toString());
419 result.append("\\");
420 }
421
422 // drop trailing slash in result
423 result.setLength(result.length()-1);
When calculating "x".relative("y"), line 413 above add "..\\", 414-415 add "y\\", 423 removes the trailing slash, and the final result is "..\\y". Good.
However, if it's only "x".relative(""), it becomes "..\\" + "\\" - "\\", so there is still one trailing slash.
Also, if it's "..".relative("x"), line 413 also add a "..\\". In fact, it should throw an IAE. This explains https://bugs.openjdk.java.net/browse/JDK-8066943.
Thanks
Max
> On Oct 23, 2015, at 5:38 PM, Weijun Wang <weijun.wang at oracle.com> wrote:
>
> Paths.get("x").relativize(Paths.get("")) returns ..\ on Windows, which is not the same as Paths.get("..\\") (which shows as ..). In fact, its getName(0).toString() is exactly ..\.
>
> It looks like the \ is not treated as a path separator.
>
> Also, Paths.get("x/y").relativize(Paths.get("")) returns ..\..\ on Windows, but its getName(0) is .. and getName(1) is ..\
>
> On Mac, it's all normal.
>
> Is this a bug?
>
> Thanks
> Max
>
> p.s. I run in cygwin of Windows 10.
>
>
More information about the nio-dev
mailing list