-
Bug
-
Resolution: Fixed
-
P3
-
7
-
b130
-
generic
-
generic
-
Verified
With zipfs, "/a/b/c/d".startsWith("/a/b") returns "true", which is correct. Similarly, "/a/b/c/d".startsWith("/a/b/") should also return "true" while it currently return "false".
With default file system, the above cases return both "true".
The root cause is that the implemenatation of getName(Index i) is different for default fs and for zipfs when handling ending slash "/".
It is hard to say which implementation is correct, but the failing zipfs.startsWith may indicate we should use that of default fs'.
Below is the testing code (also attached):
========Begin of Code=============
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import com.sun.nio.zipfs.ZipFileSystem;
public class TestStartWith {
public static void main(String[] args) throws IOException {
String thisPath = "/a/b/c/d";
String givenDir = "/a/b/"; // Ended with "/"
String givenFile = "/a/b"; // Not ended with "/"
String endDir = "c/d/";
// dafault fs
System.out.println(
"Testing with default fs: \"" + thisPath +
"\".startsWith(\"" + givenDir + "\") is " +
Paths.get(thisPath).startsWith(Paths.get(givenDir)));
System.out.println("\"" + givenDir + "\"" +
".getName(1) is " +
Paths.get(givenDir).getName(1));
System.out.println("\"" + givenFile + "\"" +
".getName(1) is " +
Paths.get(givenFile).getName(1));
System.out.println(thisPath +
"\".endsWith(\"" + endDir + "\") is " +
Paths.get(thisPath).endsWith(Paths.get(endDir)));
System.out.println();
// zipfs
Map<String, Object> env = new HashMap<String, Object>();
env.put("createNew", true);
Path file = Paths.get("place_holder.zip");
file.deleteIfExists();
ZipFileSystem fs = (ZipFileSystem) FileSystems.newFileSystem(file, env,
null);
System.out.println(
"Testing with zip fs: \"" + thisPath +
"\".startsWith(\"" + givenDir + "\") is " +
fs.getPath(thisPath).startsWith(fs.getPath(givenDir)));
System.out.println("\"" + givenDir + "\"" +
".getName(1) is " +
fs.getPath(givenDir).getName(1));
System.out.println("\"" + givenFile + "\"" +
".getName(1) is " +
fs.getPath(givenFile).getName(1));
System.out.println(thisPath +
"\".endsWith(\"" + endDir + "\") is " +
fs.getPath(thisPath).endsWith(fs.getPath(endDir)));
}
}
========End of Code=============
And the output:
==================
Testing with default fs: "/a/b/c/d".startsWith("/a/b/") is true
"/a/b/".getName(1) is b
"/a/b".getName(1) is b
/a/b/c/d".endsWith("c/d/") is true
Testing with zip fs: "/a/b/c/d".startsWith("/a/b/") is false
"/a/b/".getName(1) is b/
"/a/b".getName(1) is b
/a/b/c/d".endsWith("c/d/") is false
==================
The situation for endsWith() is the same. But for this I doubt the implementation of default fs is correct because "/a/b/c/d" does NOT look like endsWith "c/d/". (say d is a file).
With default file system, the above cases return both "true".
The root cause is that the implemenatation of getName(Index i) is different for default fs and for zipfs when handling ending slash "/".
It is hard to say which implementation is correct, but the failing zipfs.startsWith may indicate we should use that of default fs'.
Below is the testing code (also attached):
========Begin of Code=============
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import com.sun.nio.zipfs.ZipFileSystem;
public class TestStartWith {
public static void main(String[] args) throws IOException {
String thisPath = "/a/b/c/d";
String givenDir = "/a/b/"; // Ended with "/"
String givenFile = "/a/b"; // Not ended with "/"
String endDir = "c/d/";
// dafault fs
System.out.println(
"Testing with default fs: \"" + thisPath +
"\".startsWith(\"" + givenDir + "\") is " +
Paths.get(thisPath).startsWith(Paths.get(givenDir)));
System.out.println("\"" + givenDir + "\"" +
".getName(1) is " +
Paths.get(givenDir).getName(1));
System.out.println("\"" + givenFile + "\"" +
".getName(1) is " +
Paths.get(givenFile).getName(1));
System.out.println(thisPath +
"\".endsWith(\"" + endDir + "\") is " +
Paths.get(thisPath).endsWith(Paths.get(endDir)));
System.out.println();
// zipfs
Map<String, Object> env = new HashMap<String, Object>();
env.put("createNew", true);
Path file = Paths.get("place_holder.zip");
file.deleteIfExists();
ZipFileSystem fs = (ZipFileSystem) FileSystems.newFileSystem(file, env,
null);
System.out.println(
"Testing with zip fs: \"" + thisPath +
"\".startsWith(\"" + givenDir + "\") is " +
fs.getPath(thisPath).startsWith(fs.getPath(givenDir)));
System.out.println("\"" + givenDir + "\"" +
".getName(1) is " +
fs.getPath(givenDir).getName(1));
System.out.println("\"" + givenFile + "\"" +
".getName(1) is " +
fs.getPath(givenFile).getName(1));
System.out.println(thisPath +
"\".endsWith(\"" + endDir + "\") is " +
fs.getPath(thisPath).endsWith(fs.getPath(endDir)));
}
}
========End of Code=============
And the output:
==================
Testing with default fs: "/a/b/c/d".startsWith("/a/b/") is true
"/a/b/".getName(1) is b
"/a/b".getName(1) is b
/a/b/c/d".endsWith("c/d/") is true
Testing with zip fs: "/a/b/c/d".startsWith("/a/b/") is false
"/a/b/".getName(1) is b/
"/a/b".getName(1) is b
/a/b/c/d".endsWith("c/d/") is false
==================
The situation for endsWith() is the same. But for this I doubt the implementation of default fs is correct because "/a/b/c/d" does NOT look like endsWith "c/d/". (say d is a file).