Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/file/Paths.java
38918 views
/*1* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.file;2627import java.nio.file.spi.FileSystemProvider;28import java.net.URI;2930/**31* This class consists exclusively of static methods that return a {@link Path}32* by converting a path string or {@link URI}.33*34* @since 1.735*/3637public final class Paths {38private Paths() { }3940/**41* Converts a path string, or a sequence of strings that when joined form42* a path string, to a {@code Path}. If {@code more} does not specify any43* elements then the value of the {@code first} parameter is the path string44* to convert. If {@code more} specifies one or more elements then each45* non-empty string, including {@code first}, is considered to be a sequence46* of name elements (see {@link Path}) and is joined to form a path string.47* The details as to how the Strings are joined is provider specific but48* typically they will be joined using the {@link FileSystem#getSeparator49* name-separator} as the separator. For example, if the name separator is50* "{@code /}" and {@code getPath("/foo","bar","gus")} is invoked, then the51* path string {@code "/foo/bar/gus"} is converted to a {@code Path}.52* A {@code Path} representing an empty path is returned if {@code first}53* is the empty string and {@code more} does not contain any non-empty54* strings.55*56* <p> The {@code Path} is obtained by invoking the {@link FileSystem#getPath57* getPath} method of the {@link FileSystems#getDefault default} {@link58* FileSystem}.59*60* <p> Note that while this method is very convenient, using it will imply61* an assumed reference to the default {@code FileSystem} and limit the62* utility of the calling code. Hence it should not be used in library code63* intended for flexible reuse. A more flexible alternative is to use an64* existing {@code Path} instance as an anchor, such as:65* <pre>66* Path dir = ...67* Path path = dir.resolve("file");68* </pre>69*70* @param first71* the path string or initial part of the path string72* @param more73* additional strings to be joined to form the path string74*75* @return the resulting {@code Path}76*77* @throws InvalidPathException78* if the path string cannot be converted to a {@code Path}79*80* @see FileSystem#getPath81*/82public static Path get(String first, String... more) {83return FileSystems.getDefault().getPath(first, more);84}8586/**87* Converts the given URI to a {@link Path} object.88*89* <p> This method iterates over the {@link FileSystemProvider#installedProviders()90* installed} providers to locate the provider that is identified by the91* URI {@link URI#getScheme scheme} of the given URI. URI schemes are92* compared without regard to case. If the provider is found then its {@link93* FileSystemProvider#getPath getPath} method is invoked to convert the94* URI.95*96* <p> In the case of the default provider, identified by the URI scheme97* "file", the given URI has a non-empty path component, and undefined query98* and fragment components. Whether the authority component may be present99* is platform specific. The returned {@code Path} is associated with the100* {@link FileSystems#getDefault default} file system.101*102* <p> The default provider provides a similar <em>round-trip</em> guarantee103* to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it104* is guaranteed that105* <blockquote><tt>106* Paths.get(</tt><i>p</i><tt>.{@link Path#toUri() toUri}()).equals(</tt>107* <i>p</i><tt>.{@link Path#toAbsolutePath() toAbsolutePath}())</tt>108* </blockquote>109* so long as the original {@code Path}, the {@code URI}, and the new {@code110* Path} are all created in (possibly different invocations of) the same111* Java virtual machine. Whether other providers make any guarantees is112* provider specific and therefore unspecified.113*114* @param uri115* the URI to convert116*117* @return the resulting {@code Path}118*119* @throws IllegalArgumentException120* if preconditions on the {@code uri} parameter do not hold. The121* format of the URI is provider specific.122* @throws FileSystemNotFoundException123* The file system, identified by the URI, does not exist and124* cannot be created automatically, or the provider identified by125* the URI's scheme component is not installed126* @throws SecurityException127* if a security manager is installed and it denies an unspecified128* permission to access the file system129*/130public static Path get(URI uri) {131String scheme = uri.getScheme();132if (scheme == null)133throw new IllegalArgumentException("Missing scheme");134135// check for default provider to avoid loading of installed providers136if (scheme.equalsIgnoreCase("file"))137return FileSystems.getDefault().provider().getPath(uri);138139// try to find provider140for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {141if (provider.getScheme().equalsIgnoreCase(scheme)) {142return provider.getPath(uri);143}144}145146throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed");147}148}149150151