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 FileSystemProviders INSTALLED = new FileSystemProviders(FileSystemProvider.installedProviders()); 37 38 /** 39 * Gets the {@link FileSystemProvider} for the given Path. 40 * 41 * @param path The Path to query 42 * @return the {@link FileSystemProvider} for the given Path. 43 */ 44 @SuppressWarnings("resource") // FileSystem is not allocated here. 45 public static FileSystemProvider getFileSystemProvider(final Path path) { 46 return Objects.requireNonNull(path, "path").getFileSystem().provider(); 47 } 48 49 /** 50 * Returns the instance for the installed providers. 51 * 52 * @return the instance for the installed providers. 53 * @see FileSystemProvider#installedProviders() 54 */ 55 public static FileSystemProviders installed() { 56 return INSTALLED; 57 } 58 59 private final List<FileSystemProvider> providers; 60 61 /* 62 * Might make public later. 63 */ 64 private FileSystemProviders(final List<FileSystemProvider> providers) { 65 this.providers = providers != null ? providers : Collections.emptyList(); 66 } 67 68 /** 69 * Gets the {@link FileSystemProvider} for the given scheme. 70 * 71 * @param scheme The scheme to query. 72 * @return the {@link FileSystemProvider} for the given URI or null. 73 */ 74 @SuppressWarnings("resource") // FileSystems.getDefault() returns a constant. 75 public FileSystemProvider getFileSystemProvider(final String scheme) { 76 Objects.requireNonNull(scheme, "scheme"); 77 // Check default provider first to avoid loading of installed providers. 78 if (scheme.equalsIgnoreCase("file")) { 79 return FileSystems.getDefault().provider(); 80 } 81 // Find provider. 82 return providers.stream().filter(provider -> provider.getScheme().equalsIgnoreCase(scheme)).findFirst().orElse(null); 83 } 84 85 /** 86 * Gets the {@link FileSystemProvider} for the given URI. 87 * 88 * @param uri The URI to query 89 * @return the {@link FileSystemProvider} for the given URI or null. 90 */ 91 public FileSystemProvider getFileSystemProvider(final URI uri) { 92 return getFileSystemProvider(Objects.requireNonNull(uri, "uri").getScheme()); 93 } 94 95 /** 96 * Gets the {@link FileSystemProvider} for the given URL. 97 * 98 * @param url The URL to query 99 * @return the {@link FileSystemProvider} for the given URI or null. 100 */ 101 public FileSystemProvider getFileSystemProvider(final URL url) { 102 return getFileSystemProvider(Objects.requireNonNull(url, "url").getProtocol()); 103 } 104 105 }