下面列出了怎么用java.nio.file.FileSystemNotFoundException的API类实例代码及写法,或者点击链接到github查看源代码。
private static FileSystem getFileSystem(URI jarUri) {
FileSystem fs = fileSystems.get(jarUri);
if (fs == null) {
synchronized (fileSystems) {
fs = fileSystems.get(jarUri);
if (fs == null) {
try {
fs = FileSystems.getFileSystem(jarUri);
} catch (FileSystemNotFoundException e1) {
try {
fs = FileSystems.newFileSystem(jarUri, Collections.emptyMap());
} catch (IOException e2) {
throw new IllegalStateException("Could not create FileSystem for " + jarUri, e2);
}
}
fileSystems.put(jarUri, fs);
}
}
}
return fs;
}
static SystemImage open() throws IOException {
if (modulesImageExists) {
// open a .jimage and build directory structure
final ImageReader image = ImageReader.open(moduleImageFile);
image.getRootDirectory();
return new SystemImage() {
@Override
Node findNode(String path) throws IOException {
return image.findNode(path);
}
@Override
byte[] getResource(Node node) throws IOException {
return image.getResource(node);
}
@Override
void close() throws IOException {
image.close();
}
};
}
if (Files.notExists(explodedModulesDir))
throw new FileSystemNotFoundException(explodedModulesDir.toString());
return new ExplodedImage(explodedModulesDir);
}
public static Path create( URI uri )
{
try
{
return Paths.get( uri );
}
catch( FileSystemNotFoundException nfe )
{
try
{
Map<String, String> env = new HashMap<>();
env.put( "create", "true" ); // creates zip/jar file if not already exists
FileSystem fs = FileSystems.newFileSystem( uri, env );
return fs.provider().getPath( uri );
}
catch( IOException e )
{
throw new RuntimeException( e );
}
}
}
private static void fixPathFromJar(URI uri) throws IOException {
// this function is a hack to enable reading modules from within a JAR file
// see https://stackoverflow.com/a/48298758
if ("jar".equals(uri.getScheme())) {
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
if (provider.getScheme().equalsIgnoreCase("jar")) {
try {
provider.getFileSystem(uri);
} catch (FileSystemNotFoundException e) {
// in this case we need to initialize it first:
provider.newFileSystem(uri, Collections.emptyMap());
}
}
}
}
}
static Repository createDetectorsRepo(Class<?> clazz, String pluginName, Set<Path> paths) {
CodeSource codeSource = clazz.getProtectionDomain().getCodeSource();
if (codeSource == null) {
throw new RuntimeException(format("Initializing plugin '%s' could not get code source for class %s", pluginName, clazz.getName()));
}
URL url = codeSource.getLocation();
try {
Path path = Paths.get(url.toURI());
if(paths.add(path)) {
if(Files.isDirectory(path)) {
return new DirRepository(path);
} else {
return new JarRepository(new JarFile(path.toFile()));
}
} else {
return createNullRepository();
}
} catch (URISyntaxException | FileSystemNotFoundException | IllegalArgumentException
| IOException | UnsupportedOperationException e) {
String errorMessage = format("Error creating detector repository for plugin '%s'", pluginName);
throw new RuntimeException(errorMessage, e);
}
}
public static FileSystem getFileSystem(Path iviewFile) throws IOException {
URI uri = URI.create("jar:" + iviewFile.toUri());
try {
return FileSystems.newFileSystem(uri, Collections.emptyMap(), MCRClassTools.getClassLoader());
} catch (FileSystemAlreadyExistsException exc) {
// block until file system is closed
try {
FileSystem fileSystem = FileSystems.getFileSystem(uri);
while (fileSystem.isOpen()) {
try {
Thread.sleep(10);
} catch (InterruptedException ie) {
// get out of here
throw new IOException(ie);
}
}
} catch (FileSystemNotFoundException fsnfe) {
// seems closed now -> do nothing and try to return the file system again
LOGGER.debug("Filesystem not found", fsnfe);
}
return getFileSystem(iviewFile);
}
}
/**
* Returns any subclass that implements and handles the given scheme.
* @param scheme a valid {@link URI} scheme
* @see FileSystemProvider#getScheme()
* @throws FileSystemNotFoundException if no filesystem handles this scheme
*/
public static MCRAbstractFileSystem getInstance(String scheme) {
URI uri;
try {
uri = MCRPaths.getURI(scheme, "helper", SEPARATOR_STRING);
} catch (URISyntaxException e) {
throw new MCRException(e);
}
for (FileSystemProvider provider : Iterables.concat(MCRPaths.webAppProvider,
FileSystemProvider.installedProviders())) {
if (provider.getScheme().equals(scheme)) {
return (MCRAbstractFileSystem) provider.getFileSystem(uri);
}
}
throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not found");
}
@Override
public Path getPath(final URI uri) {
if (!FS_URI.getScheme().equals(Objects.requireNonNull(uri).getScheme())) {
throw new FileSystemNotFoundException("Unkown filesystem: " + uri);
}
String path = uri.getPath().substring(1);//URI path is absolute -> remove first slash
String owner = null;
for (int i = 0; i < path.length(); i++) {
if (path.charAt(i) == MCRAbstractFileSystem.SEPARATOR) {
break;
}
if (path.charAt(i) == ':') {
owner = path.substring(0, i);
path = path.substring(i + 1);
break;
}
}
return MCRAbstractFileSystem.getPath(owner, path, getFileSystemFromPathURI(FS_URI));
}
@Override
public Path getPath(final URI uri) {
if (!FS_URI.getScheme().equals(Objects.requireNonNull(uri).getScheme())) {
throw new FileSystemNotFoundException("Unkown filesystem: " + uri);
}
String path = uri.getPath().substring(1);//URI path is absolute -> remove first slash
String owner = null;
for (int i = 0; i < path.length(); i++) {
if (path.charAt(i) == MCRAbstractFileSystem.SEPARATOR) {
break;
}
if (path.charAt(i) == ':') {
owner = path.substring(0, i);
path = path.substring(i + 1);
break;
}
}
return MCRAbstractFileSystem.getPath(owner, path, getFileSystemFromPathURI(FS_URI));
}
private JFileSystem getLocalSystem()
{
synchronized (_localSystem) {
JFileSystem localSystem = _localSystem.getLevel();
if (localSystem == null) {
BartenderFileSystem fileSystem = BartenderFileSystem.getCurrent();
if (fileSystem == null) {
throw new FileSystemNotFoundException(L.l("cannot find local bfs file system"));
}
ServiceRef root = fileSystem.getRootServiceRef();
localSystem = new JFileSystem(this, root);
_localSystem.set(localSystem);
}
return localSystem;
}
}
@Test
public void testRemoveFileSystem() throws IOException {
addDirectory("/foo/bar");
SFTPFileSystemProvider provider = new SFTPFileSystemProvider();
URI uri;
try (SFTPFileSystem fs = newFileSystem(provider, createEnv())) {
SFTPPath path = new SFTPPath(fs, "/foo/bar");
uri = path.toUri();
assertFalse(provider.isHidden(path));
}
FileSystemNotFoundException exception = assertThrows(FileSystemNotFoundException.class, () -> provider.getPath(uri));
assertEquals(uri.toString(), exception.getMessage());
}
public static synchronized FileSystem open(URI uri) throws IOException {
FileSystem ret = null;
try {
ret = FileSystems.getFileSystem(uri);
} catch (FileSystemNotFoundException e) { }
boolean opened;
if (ret == null) {
ret = FileSystems.newFileSystem(uri, Collections.emptyMap());
opened = true;
} else {
opened = false;
}
RefData data = fsRefs.get(ret);
if (data == null) {
fsRefs.put(ret, new RefData(opened, 1));
} else {
data.refs++;
}
return ret;
}
@Override
public BundleFileSystem getFileSystem(URI uri) {
synchronized (openFilesystems) {
URI baseURI = baseURIFor(uri);
WeakReference<BundleFileSystem> ref = openFilesystems.get(baseURI);
if (ref == null) {
throw new FileSystemNotFoundException(uri.toString());
}
BundleFileSystem fs = ref.get();
if (fs == null) {
openFilesystems.remove(baseURI);
throw new FileSystemNotFoundException(uri.toString());
}
return fs;
}
}
/**
* Convert the given path {@link URI} to a {@link Path} object.
* @param uri the path to convert
* @return a {@link Path} object
*/
public static Path asPath(URI uri) {
try {
return Paths.get(uri);
} catch (FileSystemNotFoundException e) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
throw e;
}
try {
return FileSystems.newFileSystem(uri, new HashMap<>(), cl).provider().getPath(uri);
} catch (IOException ex) {
throw new RuntimeException("Cannot create filesystem for " + uri, ex);
}
}
}
@Test(expected = FileSystemNotFoundException.class)
public void getArbitraryFileFromUri_invalidSid() {
URI uri = GfsUriBuilder.fromFileSystem(gfs)
.file("/test_file.txt")
.sid("some_invalid_sid").build();
Paths.get(uri);
}
@Override
public FileSystem getFileSystem(URI uri) {
assertUri(uri);
FileSystem result = encFileSystem;
if (result == null)
throw new FileSystemNotFoundException();
return result;
}
public void scanSystemModules() throws IOException {
try (DirectoryStream<Path> moduleStream = Files.newDirectoryStream(Paths.get(URI.create("jrt:/modules")))) {
for (Path module : moduleStream) {
scanTree(module);
}
} catch (FileSystemNotFoundException e) {
// Modules are not supported
}
}
@Override
public FileSystem getFileSystem(URI uri) {
checkUri(uri);
FileSystem result = delegate;
if (result == null)
throw new FileSystemNotFoundException();
return result;
}
@Override
public Path getPath(URI uri) {
checkScheme(uri);
if (delegate == null)
throw new FileSystemNotFoundException();
// only allow absolute path
String path = uri.getSchemeSpecificPart();
if (! path.startsWith("///")) {
throw new IllegalArgumentException();
}
return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
}
@Override
public FileSystem getFileSystem(URI uri) {
checkUri(uri);
FileSystem result = delegate;
if (result == null)
throw new FileSystemNotFoundException();
return result;
}
@Override
public Path getPath(URI uri) {
checkScheme(uri);
if (delegate == null)
throw new FileSystemNotFoundException();
// only allow absolute path
String path = uri.getSchemeSpecificPart();
if (! path.startsWith("///")) {
throw new IllegalArgumentException();
}
return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
}
@Override
public FileSystem getFileSystem(URI uri) {
checkUri(uri);
FileSystem result = delegate;
if (result == null)
throw new FileSystemNotFoundException();
return result;
}
@Override
public Path getPath(URI uri) {
checkScheme(uri);
if (delegate == null)
throw new FileSystemNotFoundException();
// only allow absolute path
String path = uri.getSchemeSpecificPart();
if (! path.startsWith("///")) {
throw new IllegalArgumentException();
}
return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
}
@Test(expected = FileSystemNotFoundException.class)
public void getPathOfJarFileFailure() throws URISyntaxException {
String file = "jar:file:/path/to/jar/xxx.jar!/BOOT-INF/classes!/templates/themes";
URI uri = new URI(file);
Path path = Paths.get(uri);
log.debug("Path: " + path.toString());
}
@Override
public FileSystem getFileSystem(URI uri) {
checkUri(uri);
FileSystem result = delegate;
if (result == null)
throw new FileSystemNotFoundException();
return result;
}
@Override
public Path getPath(URI uri) {
checkScheme(uri);
if (delegate == null)
throw new FileSystemNotFoundException();
// only allow absolute path
String path = uri.getSchemeSpecificPart();
if (! path.startsWith("///")) {
throw new IllegalArgumentException();
}
return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
}
private static boolean isPosixCompliant() {
try {
return FileSystems.getDefault()
.supportedFileAttributeViews()
.contains("posix");
} catch (FileSystemNotFoundException
| ProviderNotFoundException
| SecurityException e) {
return false;
}
}
@Override
public FileSystem getFileSystem(URI uri) {
checkUri(uri);
FileSystem result = delegate;
if (result == null)
throw new FileSystemNotFoundException();
return result;
}
@Override
public Path getPath(URI uri) {
checkScheme(uri);
if (delegate == null)
throw new FileSystemNotFoundException();
// only allow absolute path
String path = uri.getSchemeSpecificPart();
if (! path.startsWith("///")) {
throw new IllegalArgumentException();
}
return new PassThroughFileSystem.PassThroughPath(delegate, delegate.root.resolve(path.substring(3)));
}
@Override
public Path getPath(URI uri) {
assertUriScheme(uri);
if (encFileSystem == null)
throw new FileSystemNotFoundException();
// avoid unterminated recursion / to be able to run Paths.get(URI)
uri = URI.create(encFileSystem.getSubFileSystem().provider()
.getScheme()
+ ":" + uri.getSchemeSpecificPart());
return new EncryptedFileSystemPath(encFileSystem, encFileSystem
.getSubFileSystem().provider().getPath(uri));
}