Java Code Examples for java.nio.file.FileSystemNotFoundException

The following examples show how to use java.nio.file.FileSystemNotFoundException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: vind   Source File: ElasticMappingUtils.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source Project: openjdk-jdk9   Source File: SystemImage.java    License: GNU General Public License v2.0 6 votes vote down vote up
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);
}
 
Example 3
Source Project: manifold   Source File: PathUtil.java    License: Apache License 2.0 6 votes vote down vote up
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 );
    }
  }
}
 
Example 4
Source Project: synthea   Source File: Module.java    License: Apache License 2.0 6 votes vote down vote up
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());
        }
      }
    }
  }
}
 
Example 5
Source Project: huntbugs   Source File: Repository.java    License: Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 6
Source Project: mycore   Source File: MCRIView2Tools.java    License: GNU General Public License v3.0 6 votes vote down vote up
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);
    }
}
 
Example 7
Source Project: mycore   Source File: MCRAbstractFileSystem.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * 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");
}
 
Example 8
Source Project: mycore   Source File: MCRFileSystemProvider.java    License: GNU General Public License v3.0 6 votes vote down vote up
@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));
}
 
Example 9
Source Project: mycore   Source File: MCRFileSystemProvider.java    License: GNU General Public License v3.0 6 votes vote down vote up
@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));
}
 
Example 10
Source Project: baratine   Source File: JFileSystemProvider.java    License: GNU General Public License v2.0 6 votes vote down vote up
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;
  }
}
 
Example 11
Source Project: sftp-fs   Source File: SFTPFileSystemProviderTest.java    License: Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example 12
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;
}
 
Example 13
@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;
	}
}
 
Example 14
Source Project: Hadoop-BAM   Source File: NIOFileUtil.java    License: MIT License 6 votes vote down vote up
/**
 * 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);
    }
  }
}
 
Example 15
Source Project: ParallelGit   Source File: PathsTest.java    License: Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 16
Source Project: encfs4j   Source File: EncryptedFileSystemProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FileSystem getFileSystem(URI uri) {
	assertUri(uri);
	FileSystem result = encFileSystem;
	if (result == null)
		throw new FileSystemNotFoundException();
	return result;
}
 
Example 17
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
    }
}
 
Example 18
@Override
public FileSystem getFileSystem(URI uri) {
    checkUri(uri);
    FileSystem result = delegate;
    if (result == null)
        throw new FileSystemNotFoundException();
    return result;
}
 
Example 19
@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)));
}
 
Example 20
@Override
public FileSystem getFileSystem(URI uri) {
    checkUri(uri);
    FileSystem result = delegate;
    if (result == null)
        throw new FileSystemNotFoundException();
    return result;
}
 
Example 21
@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)));
}
 
Example 22
Source Project: jdk8u60   Source File: FaultyFileSystem.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public FileSystem getFileSystem(URI uri) {
    checkUri(uri);
    FileSystem result = delegate;
    if (result == null)
        throw new FileSystemNotFoundException();
    return result;
}
 
Example 23
Source Project: jdk8u60   Source File: FaultyFileSystem.java    License: GNU General Public License v2.0 5 votes vote down vote up
@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)));
}
 
Example 24
Source Project: halo   Source File: PathTest.java    License: GNU General Public License v3.0 5 votes vote down vote up
@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());
}
 
Example 25
@Override
public FileSystem getFileSystem(URI uri) {
    checkUri(uri);
    FileSystem result = delegate;
    if (result == null)
        throw new FileSystemNotFoundException();
    return result;
}
 
Example 26
@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)));
}
 
Example 27
Source Project: ocr-neuralnet   Source File: NativeUtils.java    License: GNU General Public License v3.0 5 votes vote down vote up
private static boolean isPosixCompliant() {
    try {
        return FileSystems.getDefault()
                .supportedFileAttributeViews()
                .contains("posix");
    } catch (FileSystemNotFoundException
            | ProviderNotFoundException
            | SecurityException e) {
        return false;
    }
}
 
Example 28
@Override
public FileSystem getFileSystem(URI uri) {
    checkUri(uri);
    FileSystem result = delegate;
    if (result == null)
        throw new FileSystemNotFoundException();
    return result;
}
 
Example 29
@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)));
}
 
Example 30
Source Project: encfs4j   Source File: EncryptedFileSystemProvider.java    License: Apache License 2.0 5 votes vote down vote up
@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));
}