jdk/src/share/classes/java/nio/file/FileSystems.java
changeset 8158 77d9c0f1c19f
parent 5506 202f599c92aa
child 9035 1255eb81cc2f
equal deleted inserted replaced
7988:d31b7cc371ef 8158:77d9c0f1c19f
   162      * by invoking it with a reference to the system-default provider.
   162      * by invoking it with a reference to the system-default provider.
   163      * The second provider class is instantiated by invoking it with a reference
   163      * The second provider class is instantiated by invoking it with a reference
   164      * to the first provider instance. The third provider class is instantiated
   164      * to the first provider instance. The third provider class is instantiated
   165      * by invoking it with a reference to the second instance, and so on. The
   165      * by invoking it with a reference to the second instance, and so on. The
   166      * last provider to be instantiated becomes the default provider; its {@code
   166      * last provider to be instantiated becomes the default provider; its {@code
   167      * getFileSystem} method is invoked with the URI {@code "file:///"} to create
   167      * getFileSystem} method is invoked with the URI {@code "file:///"} to
   168      * the default file system.
   168      * get a reference to the default file system.
   169      *
   169      *
   170      * <p> Subsequent invocations of this method return the file system that was
   170      * <p> Subsequent invocations of this method return the file system that was
   171      * returned by the first invocation.
   171      * returned by the first invocation.
   172      *
   172      *
   173      * @return  the default file system
   173      * @return  the default file system
   236      *
   236      *
   237      * <p> <b>Usage Example:</b>
   237      * <p> <b>Usage Example:</b>
   238      * Suppose there is a provider identified by the scheme {@code "memory"}
   238      * Suppose there is a provider identified by the scheme {@code "memory"}
   239      * installed:
   239      * installed:
   240      * <pre>
   240      * <pre>
   241      *   Map&lt;String,String&gt; env = new HashMap&lt;String,String&gt;();
   241      *   Map&lt;String,String&gt; env = new HashMap&lt;&gt;();
   242      *   env.put("capacity", "16G");
   242      *   env.put("capacity", "16G");
   243      *   env.put("blockSize", "4k");
   243      *   env.put("blockSize", "4k");
   244      *   FileSystem fs = FileSystems.newFileSystem(URI.create("memory:///?name=logfs"), env);
   244      *   FileSystem fs = FileSystems.newFileSystem(URI.create("memory:///?name=logfs"), env);
   245      * </pre>
   245      * </pre>
   246      *
   246      *
   341      * Constructs a new {@code FileSystem} to access the contents of a file as a
   341      * Constructs a new {@code FileSystem} to access the contents of a file as a
   342      * file system.
   342      * file system.
   343      *
   343      *
   344      * <p> This method makes use of specialized providers that create pseudo file
   344      * <p> This method makes use of specialized providers that create pseudo file
   345      * systems where the contents of one or more files is treated as a file
   345      * systems where the contents of one or more files is treated as a file
   346      * system. The {@code file} parameter is a reference to an existing file
   346      * system.
   347      * and the {@code env} parameter is a map of provider specific properties to
       
   348      * configure the file system.
       
   349      *
   347      *
   350      * <p> This method iterates over the {@link FileSystemProvider#installedProviders()
   348      * <p> This method iterates over the {@link FileSystemProvider#installedProviders()
   351      * installed} providers. It invokes, in turn, each provider's {@link
   349      * installed} providers. It invokes, in turn, each provider's {@link
   352      * FileSystemProvider#newFileSystem(FileRef,Map) newFileSystem(FileRef,Map)} method.
   350      * FileSystemProvider#newFileSystem(Path,Map) newFileSystem(Path,Map)} method
   353      * If a provider returns a file system then the iteration terminates
   351      * with an empty map. If a provider returns a file system then the iteration
   354      * and the file system is returned. If none of the installed providers return
   352      * terminates and the file system is returned. If none of the installed
   355      * a {@code FileSystem} then an attempt is made to locate the provider using
   353      * providers return a {@code FileSystem} then an attempt is made to locate
   356      * the given class loader. If a provider returns a file system then the lookup
   354      * the provider using the given class loader. If a provider returns a file
   357      * terminates and the file system is returned.
   355      * system then the lookup terminates and the file system is returned.
   358      *
   356      *
   359      * @param   file
   357      * @param   path
   360      *          a reference to a file
   358      *          the path to the file
   361      * @param   env
       
   362      *          a map of provider specific properties to configure the file system;
       
   363      *          may be empty
       
   364      * @param   loader
   359      * @param   loader
   365      *          the class loader to locate the provider or {@code null} to only
   360      *          the class loader to locate the provider or {@code null} to only
   366      *          attempt to locate an installed provider
   361      *          attempt to locate an installed provider
   367      *
   362      *
   368      * @return  a new file system
   363      * @return  a new file system
   369      *
   364      *
   370      * @throws  IllegalArgumentException
       
   371      *          if the {@code env} parameter does not contain properties required
       
   372      *          by the provider, or a property value is invalid
       
   373      * @throws  ProviderNotFoundException
   365      * @throws  ProviderNotFoundException
   374      *          if a provider supporting this file type cannot be located
   366      *          if a provider supporting this file type cannot be located
   375      * @throws  ServiceConfigurationError
   367      * @throws  ServiceConfigurationError
   376      *          when an error occurs while loading a service provider
   368      *          when an error occurs while loading a service provider
   377      * @throws  IOException
   369      * @throws  IOException
   378      *          if an I/O error occurs
   370      *          if an I/O error occurs
   379      * @throws  SecurityException
   371      * @throws  SecurityException
   380      *          if a security manager is installed and it denies an unspecified
   372      *          if a security manager is installed and it denies an unspecified
   381      *          permission
   373      *          permission
   382      */
   374      */
   383     public static FileSystem newFileSystem(FileRef file,
   375     public static FileSystem newFileSystem(Path path,
   384                                            Map<String,?> env,
       
   385                                            ClassLoader loader)
   376                                            ClassLoader loader)
   386         throws IOException
   377         throws IOException
   387     {
   378     {
   388         if (file == null)
   379         if (path == null)
   389             throw new NullPointerException();
   380             throw new NullPointerException();
       
   381         Map<String,?> env = Collections.emptyMap();
   390 
   382 
   391         // check installed providers
   383         // check installed providers
   392         for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
   384         for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
   393             try {
   385             try {
   394                 return provider.newFileSystem(file, env);
   386                 return provider.newFileSystem(path, env);
   395             } catch (UnsupportedOperationException uoe) {
   387             } catch (UnsupportedOperationException uoe) {
   396             }
   388             }
   397         }
   389         }
   398 
   390 
   399         // if not found, use service-provider loading facility
   391         // if not found, use service-provider loading facility
   400         if (loader != null) {
   392         if (loader != null) {
   401             ServiceLoader<FileSystemProvider> sl = ServiceLoader
   393             ServiceLoader<FileSystemProvider> sl = ServiceLoader
   402                 .load(FileSystemProvider.class, loader);
   394                 .load(FileSystemProvider.class, loader);
   403             for (FileSystemProvider provider: sl) {
   395             for (FileSystemProvider provider: sl) {
   404                 try {
   396                 try {
   405                     return provider.newFileSystem(file, env);
   397                     return provider.newFileSystem(path, env);
   406                 } catch (UnsupportedOperationException uoe) {
   398                 } catch (UnsupportedOperationException uoe) {
   407                 }
   399                 }
   408             }
   400             }
   409         }
   401         }
   410 
   402