|
| 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.List; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +/** |
| 29 | + * Helps working with {@link FileSystemProvider}. |
| 30 | + * |
| 31 | + * @since 2.9.0 |
| 32 | + */ |
| 33 | +public class FileSystemProviders { |
| 34 | + |
| 35 | + private static FileSystemProviders INSTALLED = new FileSystemProviders(FileSystemProvider.installedProviders()); |
| 36 | + |
| 37 | + /** |
| 38 | + * Gets the {@link FileSystemProvider} for the given Path. |
| 39 | + * |
| 40 | + * @param path The Path to query |
| 41 | + * @return the {@link FileSystemProvider} for the given Path. |
| 42 | + */ |
| 43 | + @SuppressWarnings("resource") // FileSystem is not allocated here. |
| 44 | + public static FileSystemProvider getFileSystemProvider(final Path path) { |
| 45 | + return Objects.requireNonNull(path, "path").getFileSystem().provider(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Returns the instance for the installed providers. |
| 50 | + * |
| 51 | + * @return the instance for the installed providers. |
| 52 | + * @see FileSystemProvider#installedProviders() |
| 53 | + */ |
| 54 | + public static FileSystemProviders installed() { |
| 55 | + return INSTALLED; |
| 56 | + } |
| 57 | + |
| 58 | + private final List<FileSystemProvider> providers; |
| 59 | + |
| 60 | + /* |
| 61 | + * Might make public later. |
| 62 | + */ |
| 63 | + private FileSystemProviders(List<FileSystemProvider> providers) { |
| 64 | + super(); |
| 65 | + this.providers = providers; |
| 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 | + if (providers != null) { |
| 83 | + for (final FileSystemProvider provider : providers) { |
| 84 | + if (provider.getScheme().equalsIgnoreCase(scheme)) { |
| 85 | + return provider; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Gets the {@link FileSystemProvider} for the given URI. |
| 94 | + * |
| 95 | + * @param uri The URI to query |
| 96 | + * @return the {@link FileSystemProvider} for the given URI or null. |
| 97 | + */ |
| 98 | + public FileSystemProvider getFileSystemProvider(final URI uri) { |
| 99 | + return getFileSystemProvider(Objects.requireNonNull(uri, "uri").getScheme()); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Gets the {@link FileSystemProvider} for the given URL. |
| 104 | + * |
| 105 | + * @param url The URL to query |
| 106 | + * @return the {@link FileSystemProvider} for the given URI or null. |
| 107 | + */ |
| 108 | + public FileSystemProvider getFileSystemProvider(final URL url) { |
| 109 | + return getFileSystemProvider(Objects.requireNonNull(url, "url").getProtocol()); |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments