FileSystemProviders.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.io.file.spi;

  18. import java.net.URI;
  19. import java.net.URL;
  20. import java.nio.file.FileSystems;
  21. import java.nio.file.Path;
  22. import java.nio.file.spi.FileSystemProvider;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.Objects;

  26. /**
  27.  * Helps to work with {@link FileSystemProvider}.
  28.  *
  29.  * @since 2.9.0
  30.  */
  31. public class FileSystemProviders { // NOPMD Class will be final in 3.0.

  32.     private static final String SCHEME_FILE = "file";
  33.     private static final FileSystemProviders INSTALLED = new FileSystemProviders(FileSystemProvider.installedProviders());

  34.     /**
  35.      * Gets the {@link FileSystemProvider} for the given Path.
  36.      *
  37.      * @param path The Path to query
  38.      * @return the {@link FileSystemProvider} for the given Path.
  39.      */
  40.     @SuppressWarnings("resource") // FileSystem is not allocated here.
  41.     public static FileSystemProvider getFileSystemProvider(final Path path) {
  42.         return Objects.requireNonNull(path, "path").getFileSystem().provider();
  43.     }

  44.     /**
  45.      * Returns the instance for the installed providers.
  46.      *
  47.      * @return the instance for the installed providers.
  48.      * @see FileSystemProvider#installedProviders()
  49.      */
  50.     public static FileSystemProviders installed() {
  51.         return INSTALLED;
  52.     }

  53.     private final List<FileSystemProvider> providers;

  54.     /*
  55.      * Might make public later.
  56.      */
  57.     private FileSystemProviders(final List<FileSystemProvider> providers) {
  58.         this.providers = providers != null ? providers : Collections.emptyList();
  59.     }

  60.     /**
  61.      * Gets the {@link FileSystemProvider} for the given scheme.
  62.      *
  63.      * @param scheme The scheme to query.
  64.      * @return the {@link FileSystemProvider} for the given URI or null.
  65.      */
  66.     @SuppressWarnings("resource") // FileSystems.getDefault() returns a constant.
  67.     public FileSystemProvider getFileSystemProvider(final String scheme) {
  68.         Objects.requireNonNull(scheme, "scheme");
  69.         // Check default provider first to avoid loading of installed providers.
  70.         if (scheme.equalsIgnoreCase(SCHEME_FILE)) {
  71.             return FileSystems.getDefault().provider();
  72.         }
  73.         // Find provider.
  74.         return providers.stream().filter(provider -> provider.getScheme().equalsIgnoreCase(scheme)).findFirst().orElse(null);
  75.     }

  76.     /**
  77.      * Gets the {@link FileSystemProvider} for the given URI.
  78.      *
  79.      * @param uri The URI to query
  80.      * @return the {@link FileSystemProvider} for the given URI or null.
  81.      */
  82.     public FileSystemProvider getFileSystemProvider(final URI uri) {
  83.         return getFileSystemProvider(Objects.requireNonNull(uri, "uri").getScheme());
  84.     }

  85.     /**
  86.      * Gets the {@link FileSystemProvider} for the given URL.
  87.      *
  88.      * @param url The URL to query
  89.      * @return the {@link FileSystemProvider} for the given URI or null.
  90.      */
  91.     public FileSystemProvider getFileSystemProvider(final URL url) {
  92.         return getFileSystemProvider(Objects.requireNonNull(url, "url").getProtocol());
  93.     }

  94. }