Java Code Examples for java.nio.file.spi.FileSystemProvider

The following examples show how to use java.nio.file.spi.FileSystemProvider. 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: synthea   Source File: Module.java    License: Apache License 2.0 7 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 2
Source Project: Bytecoder   Source File: JrtFileSystemProvider.java    License: Apache License 2.0 6 votes vote down vote up
private FileSystem newFileSystem(String targetHome, URI uri, Map<String, ?> env)
        throws IOException {
    Objects.requireNonNull(targetHome);
    Path jrtfs = FileSystems.getDefault().getPath(targetHome, "lib", JRT_FS_JAR);
    if (Files.notExists(jrtfs)) {
        throw new IOException(jrtfs.toString() + " not exist");
    }
    Map<String,?> newEnv = new HashMap<>(env);
    newEnv.remove("java.home");
    ClassLoader cl = newJrtFsLoader(jrtfs);
    try {
        Class<?> c = Class.forName(JrtFileSystemProvider.class.getName(), false, cl);
        @SuppressWarnings("deprecation")
        Object tmp = c.newInstance();
        return ((FileSystemProvider)tmp).newFileSystem(uri, newEnv);
    } catch (ClassNotFoundException |
             IllegalAccessException |
             InstantiationException e) {
        throw new IOException(e);
    }
}
 
Example 3
Source Project: jdk1.8-source-analysis   Source File: FileSystems.java    License: Apache License 2.0 5 votes vote down vote up
private static FileSystem defaultFileSystem() {
    // load default provider
    FileSystemProvider provider = AccessController
        .doPrivileged(new PrivilegedAction<FileSystemProvider>() {
            public FileSystemProvider run() {
                return getDefaultProvider();
            }
        });

    // return file system
    return provider.getFileSystem(URI.create("file:///"));
}
 
Example 4
Source Project: dragonwell8_jdk   Source File: FileSystems.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static FileSystem defaultFileSystem() {
    // load default provider
    FileSystemProvider provider = AccessController
        .doPrivileged(new PrivilegedAction<FileSystemProvider>() {
            public FileSystemProvider run() {
                return getDefaultProvider();
            }
        });

    // return file system
    return provider.getFileSystem(URI.create("file:///"));
}
 
Example 5
/**
 * Returns the default FileSystemProvider.
 */
public static FileSystemProvider create() {
    String osname = AccessController
        .doPrivileged(new GetPropertyAction("os.name"));
    if (osname.equals("SunOS"))
        return createProvider("sun.nio.fs.SolarisFileSystemProvider");
    if (osname.equals("Linux"))
        return createProvider("sun.nio.fs.LinuxFileSystemProvider");
    if (osname.contains("OS X"))
        return createProvider("sun.nio.fs.MacOSXFileSystemProvider");
    if (osname.equals("AIX"))
        return createProvider("sun.nio.fs.AixFileSystemProvider");
    throw new AssertionError("Platform not recognized");
}
 
Example 6
Source Project: zip4j   Source File: FileUtilsTestLinuxAndMac.java    License: Apache License 2.0 5 votes vote down vote up
private PosixFileAttributeView mockPosixFileAttributeView(Path path, boolean isDirectory) throws IOException {
  FileSystemProvider fileSystemProvider = mock(FileSystemProvider.class);
  FileSystem fileSystem = mock(FileSystem.class);
  PosixFileAttributeView posixFileAttributeView = mock(PosixFileAttributeView.class);

  when(path.getFileSystem()).thenReturn(fileSystem);
  when(fileSystemProvider.getFileAttributeView(path, PosixFileAttributeView.class)).thenReturn(posixFileAttributeView);
  when(fileSystemProvider.getFileAttributeView(path, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS))
      .thenReturn(posixFileAttributeView);
  when(path.getFileSystem().provider()).thenReturn(fileSystemProvider);

  mockRegularFileOrDirectory(fileSystemProvider, path, isDirectory);

  return posixFileAttributeView;
}
 
Example 7
Source Project: hottub   Source File: PassThroughFileSystem.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Creates a new "pass through" file system. Useful for test environments
 * where the provider might not be deployed.
 */
static FileSystem create() throws IOException {
    FileSystemProvider provider = new PassThroughProvider();
    Map<String,?> env = Collections.emptyMap();
    URI uri = URI.create("pass:///");
    return provider.newFileSystem(uri, env);
}
 
Example 8
Source Project: zip4j   Source File: FileUtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLastModifiedFileTimeWhenIOExceptionDoesNothing() {
  Path path = mock(Path.class);
  FileSystemProvider fileSystemProvider = mockPath(path);
  when(fileSystemProvider.getFileAttributeView(path, BasicFileAttributeView.class))
      .thenThrow(new RuntimeException());

  long currentTime = System.currentTimeMillis();
  FileUtils.setFileLastModifiedTime(path, currentTime);
}
 
Example 9
private static FileSystemProvider getProvider(URI uri) throws IOException {
  if (fsproviders.containsKey(uri.getScheme())) {
    return fsproviders.get(uri.getScheme());
  } else {
    FileSystem fs;
    try {
      fs = FileSystems.newFileSystem(uri, new HashMap<String, Object>(),
          Thread.currentThread().getContextClassLoader());
    } catch (FileSystemAlreadyExistsException e) {
      fs = FileSystems.getFileSystem(uri);
    }
    fsproviders.put(uri.getScheme(), fs.provider());
    return fs.provider();
  }
}
 
Example 10
Source Project: jdk8u-jdk   Source File: FileSystems.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static FileSystem defaultFileSystem() {
    // load default provider
    FileSystemProvider provider = AccessController
        .doPrivileged(new PrivilegedAction<FileSystemProvider>() {
            public FileSystemProvider run() {
                return getDefaultProvider();
            }
        });

    // return file system
    return provider.getFileSystem(URI.create("file:///"));
}
 
Example 11
/**
 * Creates a new "pass through" file system. Useful for test environments
 * where the provider might not be deployed.
 */
static FileSystem create() throws IOException {
    FileSystemProvider provider = new PassThroughProvider();
    Map<String,?> env = Collections.emptyMap();
    URI uri = URI.create("pass:///");
    return provider.newFileSystem(uri, env);
}
 
Example 12
Source Project: ParallelGit   Source File: GitFileSystemProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
private static GitFileSystemProvider getInstalledProvider() {
  GitFileSystemProvider ret = null;
  for(FileSystemProvider provider : FileSystemProvider.installedProviders()) {
    if(provider instanceof GitFileSystemProvider) {
      ret = (GitFileSystemProvider) provider;
      break;
    }
  }
  if(ret == null)
    ret = new GitFileSystemProvider();
  return ret;
}
 
Example 13
Source Project: buck   Source File: BuckUnixPathUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static Path createJavaPath(String pathString) {
  FileSystemProvider provider = FileSystems.getDefault().provider();
  assertFalse(provider instanceof BuckFileSystemProvider);
  Path path = provider.getFileSystem(URI.create("file:///")).getPath(pathString);
  assertFalse(path instanceof BuckUnixPath);
  return path;
}
 
Example 14
/**
 * Creates a new "pass through" file system. Useful for test environments
 * where the provider might not be deployed.
 */
static FileSystem create() throws IOException {
    FileSystemProvider provider = new PassThroughProvider();
    Map<String,?> env = Collections.emptyMap();
    URI uri = URI.create("pass:///");
    return provider.newFileSystem(uri, env);
}
 
Example 15
Source Project: openjdk-jdk9   Source File: FSInfo.java    License: GNU General Public License v2.0 5 votes vote down vote up
public synchronized FileSystemProvider getJarFSProvider() {
    if (jarFSProvider != null) {
        return jarFSProvider;
    }
    for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
        if (provider.getScheme().equals("jar")) {
            return (jarFSProvider = provider);
        }
    }
    return null;
}
 
Example 16
Source Project: buck   Source File: AbstractWorkspace.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Copies a template to the Workspace regardless of what provider the template has its path
 * defined with. This is needed since using the "regular" copying mechanics with our destPath and
 * a non-default FileSystemProvider causes a ProviderMismatchException
 */
private void copyTemplateToWorkspace(FileSystemProvider provider, Path templatePath)
    throws IOException {
  Queue<Path> contentQueue = new ArrayDeque<>();
  addDirectoryContentToQueue(provider, templatePath, contentQueue);
  while (!contentQueue.isEmpty()) {
    Path contentPath = contentQueue.remove();
    if (Files.isDirectory(contentPath)) {
      Files.createDirectories(destPath.resolve(templatePath.relativize(contentPath).toString()));
      addDirectoryContentToQueue(provider, contentPath, contentQueue);
    } else {
      copyTemplateContentsToDestPath(provider, templatePath, contentPath);
    }
  }
}
 
Example 17
/**
 * Creates a new "pass through" file system. Useful for test environments
 * where the provider might not be deployed.
 */
static FileSystem create() throws IOException {
    FileSystemProvider provider = new PassThroughProvider();
    Map<String,?> env = Collections.emptyMap();
    URI uri = URI.create("pass:///");
    return provider.newFileSystem(uri, env);
}
 
Example 18
Source Project: gatk   Source File: PathSpecifier.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public boolean hasFileSystemProvider() {
    // try to find a provider; assume that our URI always has a scheme
    for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
        if (provider.getScheme().equalsIgnoreCase(uri.getScheme())) {
            return true;
        }
    }
    return false;
}
 
Example 19
private static FileSystem defaultFileSystem() {
    // load default provider
    FileSystemProvider provider = AccessController
        .doPrivileged(new PrivilegedAction<FileSystemProvider>() {
            public FileSystemProvider run() {
                return getDefaultProvider();
            }
        });

    // return file system
    return provider.getFileSystem(URI.create("file:///"));
}
 
Example 20
/**
 * Returns the default FileSystemProvider.
 */
public static FileSystemProvider create() {
    String osname = AccessController
        .doPrivileged(new GetPropertyAction("os.name"));
    if (osname.equals("SunOS"))
        return createProvider("sun.nio.fs.SolarisFileSystemProvider");
    if (osname.equals("Linux"))
        return createProvider("sun.nio.fs.LinuxFileSystemProvider");
    if (osname.contains("OS X"))
        return createProvider("sun.nio.fs.MacOSXFileSystemProvider");
    if (osname.equals("AIX"))
        return createProvider("sun.nio.fs.AixFileSystemProvider");
    throw new AssertionError("Platform not recognized");
}
 
Example 21
Source Project: boon   Source File: IO.java    License: Apache License 2.0 5 votes vote down vote up
public static FileSystem zipFileSystem( URI fileJarURI ) {



        final Map<String, Object> env = Maps.map( "create", ( Object ) "true" );

        FileSystemProvider provider = loadFileSystemProvider("jar");

        requireNonNull( provider, "Zip file provider not found" );

        FileSystem fs = null;

        try {
            fs = provider.getFileSystem( fileJarURI );
        } catch ( Exception ex ) {
            if ( provider != null ) {
                try {
                    fs = provider.newFileSystem( fileJarURI, env );
                } catch ( IOException ex2 ) {
                    Exceptions.handle( FileSystem.class,
                            sputs( "unable to load", fileJarURI, "as zip file system" ),
                            ex2 );
                }
            }
        }

        requireNonNull( provider, "Zip file system was not found" );

        return fs;
    }
 
Example 22
/**
 * Returns the default FileSystemProvider.
 */
public static FileSystemProvider create() {
    String osname = AccessController
        .doPrivileged(new GetPropertyAction("os.name"));
    if (osname.equals("SunOS"))
        return createProvider("sun.nio.fs.SolarisFileSystemProvider");
    if (osname.equals("Linux"))
        return createProvider("sun.nio.fs.LinuxFileSystemProvider");
    if (osname.contains("OS X"))
        return createProvider("sun.nio.fs.MacOSXFileSystemProvider");
    if (osname.equals("AIX"))
        return createProvider("sun.nio.fs.AixFileSystemProvider");
    throw new AssertionError("Platform not recognized");
}
 
Example 23
Source Project: jdk8u-dev-jdk   Source File: FileSystems.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static FileSystem defaultFileSystem() {
    // load default provider
    FileSystemProvider provider = AccessController
        .doPrivileged(new PrivilegedAction<FileSystemProvider>() {
            public FileSystemProvider run() {
                return getDefaultProvider();
            }
        });

    // return file system
    return provider.getFileSystem(URI.create("file:///"));
}
 
Example 24
Source Project: openjdk-8   Source File: Files.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the {@code FileSystemProvider} to delegate to.
 */
private static FileSystemProvider provider(Path path) {
    return path.getFileSystem().provider();
}
 
Example 25
Source Project: openjdk-8-source   Source File: Basic.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    Path zipfile = Paths.get(args[0]);

    // Test: zip should should be returned in provider list
    boolean found = false;

    for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
        if (provider.getScheme().equalsIgnoreCase("jar")) {
            found = true;
            break;
        }
    }
    if (!found)
        throw new RuntimeException("'jar' provider not installed");

    // Test: FileSystems#newFileSystem(Path)
    Map<String,?> env = new HashMap<String,Object>();
    FileSystems.newFileSystem(zipfile, null).close();

    // Test: FileSystems#newFileSystem(URI)
    URI uri = new URI("jar", zipfile.toUri().toString(), null);
    FileSystem fs = FileSystems.newFileSystem(uri, env, null);

    // Test: exercise toUri method
    String expected = uri.toString() + "!/foo";
    String actual = fs.getPath("/foo").toUri().toString();
    if (!actual.equals(expected)) {
        throw new RuntimeException("toUri returned '" + actual +
            "', expected '" + expected + "'");
    }

    // Test: exercise directory iterator and retrieval of basic attributes
    Files.walkFileTree(fs.getPath("/"), new FileTreePrinter());

    // Test: DirectoryStream
    found = false;
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(fs.getPath("/"))) {
        for (Path entry: stream) {
            found = entry.toString().equals("/META-INF/");
            if (found) break;
        }
    }

    if (!found)
        throw new RuntimeException("Expected file not found");

    // Test: copy file from zip file to current (scratch) directory
    Path source = fs.getPath("/META-INF/services/java.nio.file.spi.FileSystemProvider");
    if (Files.exists(source)) {
        Path target = Paths.get(source.getFileName().toString());
        Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
        try {
            long s1 = Files.readAttributes(source, BasicFileAttributes.class).size();
            long s2 = Files.readAttributes(target, BasicFileAttributes.class).size();
            if (s2 != s1)
                throw new RuntimeException("target size != source size");
        } finally {
            Files.delete(target);
        }
    }

    // Test: FileStore
    FileStore store = Files.getFileStore(fs.getPath("/"));
    if (!store.supportsFileAttributeView("basic"))
        throw new RuntimeException("BasicFileAttributeView should be supported");

    // Test: ClosedFileSystemException
    fs.close();
    if (fs.isOpen())
        throw new RuntimeException("FileSystem should be closed");
    try {
        fs.provider().checkAccess(fs.getPath("/missing"), AccessMode.READ);
    } catch (ClosedFileSystemException x) { }
}
 
Example 26
Source Project: fake-sftp-server-rule   Source File: FakeSftpServerRule.java    License: MIT License 4 votes vote down vote up
@Override
public FileSystemProvider provider() {
    return fileSystem.provider();
}
 
Example 27
@Override
public FileSystemProvider provider() {
    return provider;
}
 
Example 28
Source Project: jdk8u-jdk   Source File: FaultyFileSystem.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public FileSystemProvider provider() {
    return FaultyFSProvider.getInstance();
}
 
Example 29
/**
 * Returns the default FileSystemProvider.
 */
public static FileSystemProvider create() {
    return new MacOSXFileSystemProvider();
}
 
Example 30
Source Project: jdk8u-jdk   Source File: TestProvider.java    License: GNU General Public License v2.0 4 votes vote down vote up
public TestProvider(FileSystemProvider defaultProvider) {
    theFileSystem = new TestFileSystem(this);
}