001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.io.file.spi; 019 020import java.net.URI; 021import java.net.URL; 022import java.nio.file.FileSystems; 023import java.nio.file.Path; 024import java.nio.file.spi.FileSystemProvider; 025import java.util.Collections; 026import java.util.List; 027import java.util.Objects; 028 029/** 030 * Helps to work with {@link FileSystemProvider}. 031 * 032 * @since 2.9.0 033 */ 034public class FileSystemProviders { // NOPMD Class will be final in 3.0. 035 036 private static final FileSystemProviders INSTALLED = new FileSystemProviders(FileSystemProvider.installedProviders()); 037 038 /** 039 * Gets the {@link FileSystemProvider} for the given Path. 040 * 041 * @param path The Path to query 042 * @return the {@link FileSystemProvider} for the given Path. 043 */ 044 @SuppressWarnings("resource") // FileSystem is not allocated here. 045 public static FileSystemProvider getFileSystemProvider(final Path path) { 046 return Objects.requireNonNull(path, "path").getFileSystem().provider(); 047 } 048 049 /** 050 * Returns the instance for the installed providers. 051 * 052 * @return the instance for the installed providers. 053 * @see FileSystemProvider#installedProviders() 054 */ 055 public static FileSystemProviders installed() { 056 return INSTALLED; 057 } 058 059 private final List<FileSystemProvider> providers; 060 061 /* 062 * Might make public later. 063 */ 064 private FileSystemProviders(final List<FileSystemProvider> providers) { 065 this.providers = providers != null ? providers : Collections.emptyList(); 066 } 067 068 /** 069 * Gets the {@link FileSystemProvider} for the given scheme. 070 * 071 * @param scheme The scheme to query. 072 * @return the {@link FileSystemProvider} for the given URI or null. 073 */ 074 @SuppressWarnings("resource") // FileSystems.getDefault() returns a constant. 075 public FileSystemProvider getFileSystemProvider(final String scheme) { 076 Objects.requireNonNull(scheme, "scheme"); 077 // Check default provider first to avoid loading of installed providers. 078 if (scheme.equalsIgnoreCase("file")) { 079 return FileSystems.getDefault().provider(); 080 } 081 // Find provider. 082 return providers.stream().filter(provider -> provider.getScheme().equalsIgnoreCase(scheme)).findFirst().orElse(null); 083 } 084 085 /** 086 * Gets the {@link FileSystemProvider} for the given URI. 087 * 088 * @param uri The URI to query 089 * @return the {@link FileSystemProvider} for the given URI or null. 090 */ 091 public FileSystemProvider getFileSystemProvider(final URI uri) { 092 return getFileSystemProvider(Objects.requireNonNull(uri, "uri").getScheme()); 093 } 094 095 /** 096 * Gets the {@link FileSystemProvider} for the given URL. 097 * 098 * @param url The URL to query 099 * @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}