JDK-8219793 : (fs) Add FileSystems.newFileSystem(Path, Map) method
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.nio
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 13
  • Submitted: 2019-02-26
  • Updated: 2019-05-18
  • Resolved: 2019-05-01
Related Reports
CSR :  
Relates :  
Description
Summary
-------

Update java.nio.file.FileSystems with new factory methods to make it easier for users to work with file system providers that treat the contents of a file as a file system.

Problem
-------

Currently the API doesn't make it easy to provide configuration settings when opening a file as a file system. Users are forced to use the newFileSystem method that takes a URI to the zip or JAR file as these are the only methods that also take a Map with configuration settings (such as the "create" property to create a zip file).


Solution
--------

Add the following methods to java.nio.file.FileSystems:

    newFileSystem(Path) 
    newFileSystem(Path, Map<String, ?>) 
    newFileSystem(Path, Map<String, ?>, ClassLoader)

 



Specification
-------------

    /**
     * Constructs a new {@code FileSystem} to access the contents of a file as a
     * file system.
     *
     * <p> This method makes use of specialized providers that create pseudo file
     * systems where the contents of one or more files is treated as a file
     * system.
     *
     * <p> This method first attempts to locate an installed provider in exactly
     * the same manner as the {@link #newFileSystem(Path,Map,ClassLoader)
     * newFileSystem(Path, Map, ClassLoader)}. If found, the provider's
     * {@link FileSystemProvider#newFileSystem(Path, Map) newFileSystem(Path, Map)}
     * method is invoked to construct the new file system.
     *
     * @param   path
     *          the path to the file
     * @param   env
     *          a map of provider specific properties to configure the file system;
     *          may be empty
     *
     * @return  a new file system
     *
     * @throws  ProviderNotFoundException
     *          if a provider supporting this file type cannot be located
     * @throws  ServiceConfigurationError
     *          when an error occurs while loading a service provider
     * @throws  IOException
     *          if an I/O error occurs
     * @throws  SecurityException
     *          if a security manager is installed and it denies an unspecified
     *          permission
     *
     * @since 13
     */
    public static FileSystem newFileSystem(Path path, Map<String,?> env)
        throws IOException


    /**
     * Constructs a new {@code FileSystem} to access the contents of a file as a
     * file system.
     *
     * <p> This method makes use of specialized providers that create pseudo file
     * systems where the contents of one or more files is treated as a file
     * system.
     *
     * <p> This method first attempts to locate an installed provider in exactly
     * the same manner as the {@link #newFileSystem(Path,Map,ClassLoader)
     * newFileSystem(Path, Map, ClassLoader)}. If found, the provider's
     * {@link FileSystemProvider#newFileSystem(Path, Map) newFileSystem(Path, Map)}
     * method is invoked with an empty map to construct the new file system.
     *
     * @param   path
     *          the path to the file
     *
     * @return  a new file system
     *
     * @throws  ProviderNotFoundException
     *          if a provider supporting this file type cannot be located
     * @throws  ServiceConfigurationError
     *          when an error occurs while loading a service provider
     * @throws  IOException
     *          if an I/O error occurs
     * @throws  SecurityException
     *          if a security manager is installed and it denies an unspecified
     *          permission
     *
     * @since 13
     */
    public static FileSystem newFileSystem(Path path)
            throws IOException


    /**
     * Constructs a new {@code FileSystem} to access the contents of a file as a
     * file system.
     *
     * <p> This method makes use of specialized providers that create pseudo file
     * systems where the contents of one or more files is treated as a file
     * system.
     *
     * <p> This method iterates over the {@link FileSystemProvider#installedProviders()
     * installed} providers. It invokes, in turn, each provider's {@link
     * FileSystemProvider#newFileSystem(Path,Map) newFileSystem(Path,Map)}
     * method. If a provider returns a file system then the iteration
     * terminates and the file system is returned.
     * If none of the installed providers return a {@code FileSystem} then
     * an attempt is made to locate the provider using the given class loader.
     * If a provider returns a file
     * system, then the lookup terminates and the file system is returned.
     *
     * @param   path
     *          the path to the file
     * @param   env
     *          a map of provider specific properties to configure the file system;
     *          may be empty
     * @param   loader
     *          the class loader to locate the provider or {@code null} to only
     *          attempt to locate an installed provider
     *
     * @return  a new file system
     *
     * @throws  ProviderNotFoundException
     *          if a provider supporting this file type cannot be located
     * @throws  ServiceConfigurationError
     *          when an error occurs while loading a service provider
     * @throws  IOException
     *          if an I/O error occurs
     * @throws  SecurityException
     *          if a security manager is installed and it denies an unspecified
     *          permission
     *
     * @since 13
     */
    public static FileSystem newFileSystem(Path path, Map<String,?> env,
                                           ClassLoader loader)
        throws IOException

Comments
I believe that you have overlook the fact the code newFileSystem(path, null) is common in the test codes of a lot of applications. I think you should reconsider using the name newFileSystemWithEnv instead of newFileSystem for the two methods that take an environment as parameter.
18-05-2019

I don't think anything has been overlooked. In the case of the two file system implementations cited then they expose their own API to create the file system. The source compatibility issue that we discussed here (and on nio-dev) is should be limited to code calling FileSystems.newFileSystem(Path, ClassLoader) where the ClassLoader is specified as null without a cast. No impact on the newFileSystems that specify a URI.
18-05-2019

I believe you have overlook the fact that the ZipFileSystem is no the only one FileSystem used, you can create your own FileSystem since Java 7. From my own experience, at least the HadoopFileSystem [1] and the GitFileSystem [2] are used widely. Doing a Github search shows a lot of other usages too (like Android) https://github.com/search?q=newFileSystem+null&type=Code so i believe that the you should reconsider the decision to add this method given that it will break a lot of codes. regards, R��mi [1] https://hadoop.apache.org/docs/stable/hadoop-project-dist/hadoop-common/filesystem/filesystem.html [2] https://github.com/beijunyi/ParallelGit
18-05-2019

It's important not to confuse the newFileSystem methods that take a URI with the methods that take a Path. The former can work with provider that maintain a registry of file systems and work with the getFileSystem method. There are several highly provider specific scenarios here, none of these are testable via conformance tests.
10-05-2019

Existing newFileSystem methods do declare FileSystemNotFoundException/FileSystemAlreadyExistsException throwing. So the same should be with the new methods if there really are conditions when user should expect these exceptions. The new methods should explicitly declare those FS exceptions, otherwise it would be a spec inconsistency and a confusion for users.
10-05-2019

These methods do not throw FileSystemNotFoundException or FileSystemAlreadyExistsException. There may be a bug in the zipfs implementation where it throws FileSystemNotFoundException when the zip or JAR file does not exist, that is something to fix via another issue.
09-05-2019

Moving to Approved.
01-05-2019

I created a release note subtask: https://bugs.openjdk.java.net/browse/JDK-8223197
01-05-2019

Moving to Provisional for JDK 13. Please plan for a release note before the request is finalized.
01-05-2019