2009-02-15 12:25:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2007-2009 Sun Microsystems, Inc. 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. Sun designates this
|
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
|
|
|
* by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
|
|
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
|
|
* have any questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package java.nio.file;
|
|
|
|
|
|
|
|
import java.nio.file.spi.FileSystemProvider;
|
|
|
|
import java.net.URI;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class consists exclusively of static methods that return a {@link Path}
|
|
|
|
* by converting a path string or {@link URI}.
|
|
|
|
*
|
|
|
|
* @since 1.7
|
|
|
|
*/
|
|
|
|
|
2009-06-27 21:46:53 +01:00
|
|
|
public final class Paths {
|
2009-02-15 12:25:54 +00:00
|
|
|
private Paths() { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a {@code Path} by converting the given path string.
|
|
|
|
*
|
|
|
|
* <p> The {@code Path} is obtained by invoking the {@link FileSystem#getPath
|
|
|
|
* getPath} method of the {@link FileSystems#getDefault default} {@link
|
|
|
|
* FileSystem}.
|
|
|
|
*
|
2009-02-24 11:31:04 +00:00
|
|
|
* <p> Note that while this method is very convenient, using it will
|
|
|
|
* imply an assumed reference to the default FileSystem and limit the
|
|
|
|
* utility of the calling code. Hence it should not be used in library code
|
|
|
|
* intended for flexible reuse. A more flexible alternative is to use an
|
|
|
|
* existing {@code Path} instance as an anchor, such as:
|
|
|
|
* <pre>
|
|
|
|
* Path dir = ...
|
|
|
|
* Path path = dir.resolve("file");
|
|
|
|
* </pre>
|
|
|
|
*
|
2009-02-15 12:25:54 +00:00
|
|
|
* @param path
|
2009-02-24 11:31:04 +00:00
|
|
|
* the path string to convert
|
2009-02-15 12:25:54 +00:00
|
|
|
*
|
2009-02-24 11:31:04 +00:00
|
|
|
* @return the resulting {@code Path}
|
2009-02-15 12:25:54 +00:00
|
|
|
*
|
|
|
|
* @throws InvalidPathException
|
2009-02-24 11:31:04 +00:00
|
|
|
* if the path string cannot be converted to a {@code Path}
|
2009-02-15 12:25:54 +00:00
|
|
|
*
|
|
|
|
* @see FileSystem#getPath
|
|
|
|
*/
|
|
|
|
public static Path get(String path) {
|
|
|
|
return FileSystems.getDefault().getPath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given URI to a {@link Path} object.
|
|
|
|
*
|
|
|
|
* <p> This method iterates over the {@link FileSystemProvider#installedProviders()
|
|
|
|
* installed} providers to locate the provider that is identified by the
|
|
|
|
* URI {@link URI#getScheme scheme} of the given URI. URI schemes are
|
|
|
|
* compared without regard to case. If the provider is found then its {@link
|
|
|
|
* FileSystemProvider#getPath getPath} method is invoked to convert the
|
|
|
|
* URI.
|
|
|
|
*
|
|
|
|
* <p> In the case of the default provider, identified by the URI scheme
|
|
|
|
* "file", the given URI has a non-empty path component, and undefined query
|
|
|
|
* and fragment components. Whether the authority component may be present
|
|
|
|
* is platform specific. The returned {@code Path} is associated with the
|
|
|
|
* {@link FileSystems#getDefault default} file system.
|
|
|
|
*
|
|
|
|
* <p> The default provider provides a similar <em>round-trip</em> guarantee
|
|
|
|
* to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it
|
|
|
|
* is guaranteed that
|
|
|
|
* <blockquote><tt>
|
|
|
|
* Paths.get(</tt><i>p</i><tt>.{@link Path#toUri() toUri}()).equals(</tt>
|
|
|
|
* <i>p</i><tt>.{@link Path#toAbsolutePath() toAbsolutePath}())</tt>
|
|
|
|
* </blockquote>
|
|
|
|
* so long as the original {@code Path}, the {@code URI}, and the new {@code
|
|
|
|
* Path} are all created in (possibly different invocations of) the same
|
|
|
|
* Java virtual machine. Whether other providers make any guarantees is
|
|
|
|
* provider specific and therefore unspecified.
|
|
|
|
*
|
|
|
|
* @param uri
|
2009-02-24 11:31:04 +00:00
|
|
|
* the URI to convert
|
2009-02-15 12:25:54 +00:00
|
|
|
*
|
2009-02-24 11:31:04 +00:00
|
|
|
* @return the resulting {@code Path}
|
2009-02-15 12:25:54 +00:00
|
|
|
*
|
|
|
|
* @throws IllegalArgumentException
|
2009-02-24 11:31:04 +00:00
|
|
|
* if preconditions on the {@code uri} parameter do not hold. The
|
2009-02-15 12:25:54 +00:00
|
|
|
* format of the URI is provider specific.
|
|
|
|
* @throws FileSystemNotFoundException
|
2009-06-27 21:46:53 +01:00
|
|
|
* The file system, identified by the URI, does not exist and
|
|
|
|
* cannot be created automatically, or the provider identified by
|
|
|
|
* the URI's scheme component is not installed
|
2009-02-15 12:25:54 +00:00
|
|
|
* @throws SecurityException
|
2009-02-24 11:31:04 +00:00
|
|
|
* if a security manager is installed and it denies an unspecified
|
2009-02-15 12:25:54 +00:00
|
|
|
* permission to access the file system
|
|
|
|
*/
|
|
|
|
public static Path get(URI uri) {
|
|
|
|
String scheme = uri.getScheme();
|
|
|
|
if (scheme == null)
|
|
|
|
throw new IllegalArgumentException("Missing scheme");
|
|
|
|
|
|
|
|
// check for default provider to avoid loading of installed providers
|
|
|
|
if (scheme.equalsIgnoreCase("file"))
|
|
|
|
return FileSystems.getDefault().provider().getPath(uri);
|
|
|
|
|
|
|
|
// try to find provider
|
|
|
|
for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
|
|
|
|
if (provider.getScheme().equalsIgnoreCase(scheme)) {
|
|
|
|
return provider.getPath(uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed");
|
|
|
|
}
|
|
|
|
}
|