You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to create a virtual FileSystem similar to Configuration.unix() that is able to handle both / and \ as separators, like the default Java filesystem.
I regularly read relative file paths from default FileSystem or configuration and need to convert them to relative JimFs paths. Those paths happily mix / and \ if the program is executed on windows, and i do not want to manually replace the separators at every occation since actually JimFs has this functionality built in, but I don't see a way to use it.
A PathType for this would be nice. Or the possibility to configure this somehow. The only way I see to do this is to create my own PathType adapter class that forwards all methods to UnixPathType but has a different constructor that specified additional separators.
Edit:
Implementing adapter is somewhat cumbersome because toUriPath(...) and parseUriPath(...) are protected, so i cannot call them unless i put my adapter into the same package as UnixPathType, but thats only nitpicking.
Edit2:
I need to implement parsePath(String path) as well so it uses the custom Splitter. This solution is not great.
code
packagecom.google.common.jimfs;
importstaticcom.google.common.jimfs.Feature.FILE_CHANNEL;
importstaticcom.google.common.jimfs.Feature.LINKS;
importstaticcom.google.common.jimfs.Feature.SECURE_DIRECTORY_STREAM;
importstaticcom.google.common.jimfs.Feature.SYMBOLIC_LINKS;
importjava.nio.file.InvalidPathException;
/** * Very similar to normal Configuration.unix(), but is able to parse windows separators in * addition to unix separator. * */publicclassUnixCompatible {
publicstaticConfigurationconfig() {
returnConfiguration.builder(newCompatiblePathType())
.setRoots("/")
.setWorkingDirectory("/work")
.setAttributeViews("basic")
.setSupportedFeatures(LINKS, SYMBOLIC_LINKS, SECURE_DIRECTORY_STREAM, FILE_CHANNEL)
.build();
}
staticclassCompatiblePathTypeextendsPathType {
finalPathTypeunix;
publicCompatiblePathType() {
super(false, '/', '\\');
unix = PathType.unix();
}
/** Copied from UnixPathType, custom splitter */@OverridepublicParseResultparsePath(Stringpath) {
if (path.isEmpty()) {
returnemptyPath();
}
checkValid(path);
finalStringroot = path.startsWith("/") ? "/" : null;
returnnewParseResult(root, splitter().split(path));
}
/** Copied from UnixPathType */privatestaticvoidcheckValid(Stringpath) {
finalintnulIndex = path.indexOf('\0');
if (nulIndex != -1) {
thrownewInvalidPathException(path, "nul character not allowed", nulIndex);
}
}
@OverridepublicStringtoString(Stringroot, Iterable<String> names) {
returnunix.toString(root, names);
}
@OverridepublicStringtoString() {
returnunix.toString();
}
@OverrideprotectedStringtoUriPath(Stringroot, Iterable<String> names, booleandirectory) {
returnunix.toUriPath(root, names, directory);
}
@OverrideprotectedParseResultparseUriPath(StringuriPath) {
returnunix.parseUriPath(uriPath);
}
}
}
The text was updated successfully, but these errors were encountered:
I want to create a virtual FileSystem similar to
Configuration.unix()
that is able to handle both/
and\
as separators, like the default Java filesystem.I regularly read relative file paths from default FileSystem or configuration and need to convert them to relative JimFs paths. Those paths happily mix
/
and\
if the program is executed on windows, and i do not want to manually replace the separators at every occation since actually JimFs has this functionality built in, but I don't see a way to use it.A PathType for this would be nice. Or the possibility to configure this somehow. The only way I see to do this is to create my own PathType adapter class that forwards all methods to UnixPathType but has a different constructor that specified additional separators.
Edit:
Implementing adapter is somewhat cumbersome because
toUriPath(...)
andparseUriPath(...)
are protected, so i cannot call them unless i put my adapter into the same package asUnixPathType
, but thats only nitpicking.Edit2:
I need to implement
parsePath(String path)
as well so it uses the custom Splitter. This solution is not great.code
The text was updated successfully, but these errors were encountered: