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 File: Module.java    From synthea with 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 File: JrtFileSystemProvider.java    From Bytecoder with 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 File: FileSystems.java    From openjdk-jdk8u-backup with 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 #4
Source File: DefaultFileSystemProvider.java    From dragonwell8_jdk with 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 #5
Source File: FileSystems.java    From jdk8u-dev-jdk with 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 #6
Source File: IO.java    From boon with 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 #7
Source File: DefaultFileSystemProvider.java    From openjdk-jdk8u with 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 #8
Source File: FileSystems.java    From jdk1.8-source-analysis with 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 #9
Source File: DefaultFileSystemProvider.java    From jdk8u-jdk with 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 #10
Source File: FileSystems.java    From dragonwell8_jdk with 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 File: PassThroughFileSystem.java    From jdk8u-jdk with 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 File: PathSpecifier.java    From gatk with 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 #13
Source File: FileUtilsTestLinuxAndMac.java    From zip4j with 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 #14
Source File: FileUtilsTest.java    From zip4j with 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 #15
Source File: RadarServerConfig.java    From tds with 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 #16
Source File: PassThroughFileSystem.java    From hottub with 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 #17
Source File: PassThroughFileSystem.java    From jdk8u60 with 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 File: FileSystems.java    From jdk8u-jdk with 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 #19
Source File: PassThroughFileSystem.java    From jdk8u-jdk with 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 #20
Source File: AbstractWorkspace.java    From buck with 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 #21
Source File: FSInfo.java    From openjdk-jdk9 with 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 #22
Source File: BuckUnixPathUtils.java    From buck with 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 #23
Source File: GitFileSystemProvider.java    From ParallelGit with 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 #24
Source File: TestProvider.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
public FileSystemProvider provider() {
    return provider;
}
 
Example #25
Source File: PassThroughFileSystem.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
@Override
public FileSystemProvider provider() {
    return provider;
}
 
Example #26
Source File: Basic.java    From jdk8u-jdk with 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 #27
Source File: PassThroughFileSystem.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public FileSystemProvider provider() {
    return provider;
}
 
Example #28
Source File: DefaultFileTypeDetector.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static FileTypeDetector create() {
    FileSystemProvider provider = FileSystems.getDefault().provider();
    return ((UnixFileSystemProvider)provider).getFileTypeDetector();
}
 
Example #29
Source File: JrtFileSystem.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
public FileSystemProvider provider() {
    return provider;
}
 
Example #30
Source File: DefaultFileSystemProvider.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static FileSystemProvider create() {
    return new WindowsFileSystemProvider();
}