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 18 package org.apache.commons.io.file.spi; 19 20 import java.net.URI; 21 import java.net.URL; 22 import java.nio.file.FileSystems; 23 import java.nio.file.Path; 24 import java.nio.file.spi.FileSystemProvider; 25 import java.util.Collections; 26 import java.util.List; 27 import java.util.Objects; 28 29 /** 30 * Helps to work with {@link FileSystemProvider}. 31 * 32 * @since 2.9.0 33 */ 34 public class FileSystemProviders { // NOPMD Class will be final in 3.0. 35 36 private static final String SCHEME_FILE = "file"; 37 private static final FileSystemProviders INSTALLED = new FileSystemProviders(FileSystemProvider.installedProviders()); 38 39 /** 40 * Gets the {@link FileSystemProvider} for the given Path. 41 * 42 * @param path The Path to query 43 * @return the {@link FileSystemProvider} for the given Path. 44 */ 45 @SuppressWarnings("resource") // FileSystem is not allocated here. 46 public static FileSystemProvider getFileSystemProvider(final Path path) { 47 return Objects.requireNonNull(path, "path").getFileSystem().provider(); 48 } 49 50 /** 51 * Returns the instance for the installed providers. 52 * 53 * @return the instance for the installed providers. 54 * @see FileSystemProvider#installedProviders() 55 */ 56 public static FileSystemProviders installed() { 57 return INSTALLED; 58 } 59 60 private final List<FileSystemProvider> providers; 61 62 /* 63 * Might make public later. 64 */ 65 private FileSystemProviders(final List<FileSystemProvider> providers) { 66 this.providers = providers != null ? providers : Collections.emptyList(); 67 } 68 69 /** 70 * Gets the {@link FileSystemProvider} for the given scheme. 71 * 72 * @param scheme The scheme to query. 73 * @return the {@link FileSystemProvider} for the given URI or null. 74 */ 75 @SuppressWarnings("resource") // FileSystems.getDefault() returns a constant. 76 public FileSystemProvider getFileSystemProvider(final String scheme) { 77 Objects.requireNonNull(scheme, "scheme"); 78 // Check default provider first to avoid loading of installed providers. 79 if (scheme.equalsIgnoreCase(SCHEME_FILE)) { 80 return FileSystems.getDefault().provider(); 81 } 82 // Find provider. 83 return providers.stream().filter(provider -> provider.getScheme().equalsIgnoreCase(scheme)).findFirst().orElse(null); 84 } 85 86 /** 87 * Gets the {@link FileSystemProvider} for the given URI. 88 * 89 * @param uri The URI to query 90 * @return the {@link FileSystemProvider} for the given URI or null. 91 */ 92 public FileSystemProvider getFileSystemProvider(final URI uri) { 93 return getFileSystemProvider(Objects.requireNonNull(uri, "uri").getScheme()); 94 } 95 96 /** 97 * Gets the {@link FileSystemProvider} for the given URL. 98 * 99 * @param url The URL to query 100 * @return the {@link FileSystemProvider} for the given URI or null. 101 */ 102 public FileSystemProvider getFileSystemProvider(final URL url) { 103 return getFileSystemProvider(Objects.requireNonNull(url, "url").getProtocol()); 104 } 105 106 }