JDK

java.nio.file.FileSystemsクラス

FileSystemsクラスはディフォルトのファイルシステムとファクトリメソッドを用いて,FileSystemクラスのインスタンスを構築するメソッドを提供する。コンストラクタはprivateになっており,インスタンス化できない。

以下のstaticメソッドが公開されており,これらを利用する。一つ目はディフォルトのFileSystemクラスのインスタンスを取得する。2つ目は与えられたURIオブジェクトのスキームをインストール済のFileSystemProviderから検索して,対応するFileSystemクラスのインスタンスを返す。

3つ目から4つ目のメソッドはいずれも4つ目のメソッドへ移譲される。そして,URIからスキームを取り出し,2つ目のメソッドと同じようにインストールさらえているFileSystemProviderからFileSystemクラスのインスタンスを検索する。そして,みつかったFileSystemProviderのnewFileSystem(uri, env)によってFileSystemクラスのインスタンスを作成する。もしFileSystemProviderがみつからなかった場合は, ServiceLoaderクラスのloadメソッドによりFileSystemProviderをロードする。そして,ロードしたFileSystemProviderの中からスキームが一致するものを検索する。もしスキームが一致するFileSystemProviderが存在すれば,そのオブジェクトのnewFileSystem()メソッドでFileSystemオブジェクトを作成する。FileSystemProviderがみつからなかった場合は,ProviderNotFoundExceptionをスローする。

5つめのメソッドも処理内容はほぼ同じで,Pathに対応するFileSystemProviderクラスのインスタンスを検索して,FileSystemオブジェクトを取得する。FileSystemProviderクラスのインスタンスがみつからなかった場合には,ServiceLoaderクラスのload()メソッドによりFileSystemProviderをロードして,同様にFileSystemProviderクラスのインスタンスを検索し,newFileSystem()メソッドでFileSystemクラスのインスタンスを作成する。それでもみつからない場合には,ProviderNotFoundExceptionをスローしている。

  • getDefault()
  • getFileSystem(URI uri)
  • newFileSystem(URI uri, Map env)
  • newFileSystem(URI uri, Map env, ClassLoader loader)
  • newFileSystem(Path path, ClassLoader loader)

/*
 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the “Classpath” exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package java.nio.file;
import java.nio.file.spi.FileSystemProvider;
import java.net.URI;
import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import jdk.internal.misc.VM;
import sun.nio.fs.DefaultFileSystemProvider;
public final class FileSystems {
    private FileSystems() { }
    // lazy initialization of default file system
    private static class DefaultFileSystemHolder {
        static final FileSystem defaultFileSystem = defaultFileSystem();
        // returns default file system
        private static FileSystem defaultFileSystem() {
            // load default provider
            FileSystemProvider provider = AccessController
                .doPrivileged(new PrivilegedAction<>() {
                    public FileSystemProvider run() {
                        return getDefaultProvider();
                    }
                });
            // return file system
            return provider.getFileSystem(URI.create(“file:///”));
        }
        // returns default provider
        private static FileSystemProvider getDefaultProvider() {
            // start with the platform’s default file system provider
            FileSystemProvider provider = DefaultFileSystemProvider.instance();
            // if the property java.nio.file.spi.DefaultFileSystemProvider is
            // set then its value is the name of the default provider (or a list)
            String prop = “java.nio.file.spi.DefaultFileSystemProvider”;
            String propValue = System.getProperty(prop);
            if (propValue != null) {
                for (String cn: propValue.split(“,”)) {
                    try {
                        Class<?> c = Class
                            .forName(cn, true, ClassLoader.getSystemClassLoader());
                        Constructor<?> ctor = c
                            .getDeclaredConstructor(FileSystemProvider.class);
                        provider = (FileSystemProvider)ctor.newInstance(provider);
                        // must be “file”
                        if (!provider.getScheme().equals(“file”))
                            throw new Error(“Default provider must use scheme ‘file'”);
                    } catch (Exception x) {
                        throw new Error(x);
                    }
                }
            }
            return provider;
        }
    }
    public static FileSystem getDefault() {
        if (VM.isModuleSystemInited()) {
            return DefaultFileSystemHolder.defaultFileSystem;
        } else {
            // always use the platform’s default file system during startup
            return DefaultFileSystemProvider.theFileSystem();
        }
    }
    public static FileSystem getFileSystem(URI uri) {
        String scheme = uri.getScheme();
        for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
            if (scheme.equalsIgnoreCase(provider.getScheme())) {
                return provider.getFileSystem(uri);
            }
        }
        throw new ProviderNotFoundException(“Provider \”” + scheme + “\” not found”);
    }
    public static FileSystem newFileSystem(URI uri, Map<String,?> env)
        throws IOException
    {
        return newFileSystem(uri, env, null);
    }
    public static FileSystem newFileSystem(URI uri, Map<String,?> env, ClassLoader loader)
        throws IOException
    {
        String scheme = uri.getScheme();
        // check installed providers
        for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
            if (scheme.equalsIgnoreCase(provider.getScheme())) {
                try {
                    return provider.newFileSystem(uri, env);
                } catch (UnsupportedOperationException uoe) {
                }
            }
        }
        // if not found, use service-provider loading facility
        if (loader != null) {
            ServiceLoader<FileSystemProvider> sl = ServiceLoader
                .load(FileSystemProvider.class, loader);
            for (FileSystemProvider provider : sl) {
                if (scheme.equalsIgnoreCase(provider.getScheme())) {
                    try {
                        return provider.newFileSystem(uri, env);
                    } catch (UnsupportedOperationException uoe) {
                    }
                }
            }
        }
        throw new ProviderNotFoundException(“Provider \”” + scheme + “\” not found”);
    }
    public static FileSystem newFileSystem(Path path,
                                           ClassLoader loader)
        throws IOException
    {
        if (path == null)
            throw new NullPointerException();
        Map<String,?> env = Collections.emptyMap();
        // check installed providers
        for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
            try {
                return provider.newFileSystem(path, env);
            } catch (UnsupportedOperationException uoe) {
            }
        }
        // if not found, use service-provider loading facility
        if (loader != null) {
            ServiceLoader<FileSystemProvider> sl = ServiceLoader
                .load(FileSystemProvider.class, loader);
            for (FileSystemProvider provider: sl) {
                try {
                    return provider.newFileSystem(path, env);
                } catch (UnsupportedOperationException uoe) {
                }
            }
        }
        throw new ProviderNotFoundException(“Provider not found”);
    }
}