java.nio.file.spi.FileSystemProvider Java Examples

The following examples show how to use java.nio.file.spi.FileSystemProvider. 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   Author: synthetichealth   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   Author: mirkosertic   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   Author: raysonfang   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   Author: alibaba   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
Source Project: dragonwell8_jdk   Author: alibaba   File: DefaultFileSystemProvider.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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   Author: srikanth-lingala   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   Author: dsrg-uoft   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   Author: srikanth-lingala   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
Source Project: tds   Author: Unidata   File: RadarServerConfig.java    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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   Author: lambdalab-mirror   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
Source Project: jdk8u-jdk   Author: frohoff   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 #12
Source Project: ParallelGit   Author: beijunyi   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   Author: facebook   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
Source Project: jdk8u-jdk   Author: lambdalab-mirror   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 #15
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   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   Author: facebook   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
Source Project: jdk8u60   Author: chenghanpeng   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 #18
Source Project: gatk   Author: broadinstitute   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
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   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 #20
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: DefaultFileSystemProvider.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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   Author: boonproject   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
Source Project: jdk8u-jdk   Author: frohoff   File: DefaultFileSystemProvider.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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   Author: frohoff   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   Author: bpupadhyaya   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   Author: keerath   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   Author: stefanbirkner   File: FakeSftpServerRule.java    License: MIT License 4 votes vote down vote up
@Override
public FileSystemProvider provider() {
    return fileSystem.provider();
}
 
Example #27
Source Project: jdk8u_jdk   Author: JetBrains   File: PassThroughFileSystem.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public FileSystemProvider provider() {
    return provider;
}
 
Example #28
Source Project: jdk8u-jdk   Author: lambdalab-mirror   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
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: DefaultFileSystemProvider.java    License: GNU General Public License v2.0 4 votes vote down vote up
/**
 * Returns the default FileSystemProvider.
 */
public static FileSystemProvider create() {
    return new MacOSXFileSystemProvider();
}
 
Example #30
Source Project: jdk8u-jdk   Author: frohoff   File: TestProvider.java    License: GNU General Public License v2.0 4 votes vote down vote up
public TestProvider(FileSystemProvider defaultProvider) {
    theFileSystem = new TestFileSystem(this);
}