jdk/src/share/classes/java/nio/file/Paths.java
changeset 2057 3acf8e5e2ca0
child 2072 80dfe4469bbd
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
       
     1 /*
       
     2  * Copyright 2007-2009 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package java.nio.file;
       
    27 
       
    28 import java.nio.file.spi.FileSystemProvider;
       
    29 import java.net.URI;
       
    30 
       
    31 /**
       
    32  * This class consists exclusively of static methods that return a {@link Path}
       
    33  * by converting a path string or {@link URI}.
       
    34  *
       
    35  * @since 1.7
       
    36  */
       
    37 
       
    38 public class Paths {
       
    39     private Paths() { }
       
    40 
       
    41     /**
       
    42      * Constructs a {@code Path} by converting the given path string.
       
    43      *
       
    44      * <p> The {@code Path} is obtained by invoking the {@link FileSystem#getPath
       
    45      * getPath} method of the {@link FileSystems#getDefault default} {@link
       
    46      * FileSystem}.
       
    47      *
       
    48      * @param   path
       
    49      *          The path string to convert
       
    50      *
       
    51      * @return  The resulting {@code Path}
       
    52      *
       
    53      * @throws  InvalidPathException
       
    54      *          If the path string cannot be converted to a {@code Path}
       
    55      *
       
    56      * @see FileSystem#getPath
       
    57      */
       
    58     public static Path get(String path) {
       
    59         return FileSystems.getDefault().getPath(path);
       
    60     }
       
    61 
       
    62     /**
       
    63      * Converts the given URI to a {@link Path} object.
       
    64      *
       
    65      * <p> This method iterates over the {@link FileSystemProvider#installedProviders()
       
    66      * installed} providers to locate the provider that is identified by the
       
    67      * URI {@link URI#getScheme scheme} of the given URI. URI schemes are
       
    68      * compared without regard to case. If the provider is found then its {@link
       
    69      * FileSystemProvider#getPath getPath} method is invoked to convert the
       
    70      * URI.
       
    71      *
       
    72      * <p> In the case of the default provider, identified by the URI scheme
       
    73      * "file", the given URI has a non-empty path component, and undefined query
       
    74      * and fragment components. Whether the authority component may be present
       
    75      * is platform specific. The returned {@code Path} is associated with the
       
    76      * {@link FileSystems#getDefault default} file system.
       
    77      *
       
    78      * <p> The default provider provides a similar <em>round-trip</em> guarantee
       
    79      * to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it
       
    80      * is guaranteed that
       
    81      * <blockquote><tt>
       
    82      * Paths.get(</tt><i>p</i><tt>.{@link Path#toUri() toUri}()).equals(</tt>
       
    83      * <i>p</i><tt>.{@link Path#toAbsolutePath() toAbsolutePath}())</tt>
       
    84      * </blockquote>
       
    85      * so long as the original {@code Path}, the {@code URI}, and the new {@code
       
    86      * Path} are all created in (possibly different invocations of) the same
       
    87      * Java virtual machine. Whether other providers make any guarantees is
       
    88      * provider specific and therefore unspecified.
       
    89      *
       
    90      * @param   uri
       
    91      *          The URI to convert
       
    92      *
       
    93      * @return  The resulting {@code Path}
       
    94      *
       
    95      * @throws  IllegalArgumentException
       
    96      *          If preconditions on the {@code uri} parameter do not hold. The
       
    97      *          format of the URI is provider specific.
       
    98      * @throws  FileSystemNotFoundException
       
    99      *          If the file system identified by the URI does not exist or the
       
   100      *          provider identified by the URI's scheme component is not installed
       
   101      * @throws  SecurityException
       
   102      *          If a security manager is installed and it denies an unspecified
       
   103      *          permission to access the file system
       
   104      */
       
   105     public static Path get(URI uri) {
       
   106         String scheme =  uri.getScheme();
       
   107         if (scheme == null)
       
   108             throw new IllegalArgumentException("Missing scheme");
       
   109 
       
   110         // check for default provider to avoid loading of installed providers
       
   111         if (scheme.equalsIgnoreCase("file"))
       
   112             return FileSystems.getDefault().provider().getPath(uri);
       
   113 
       
   114         // try to find provider
       
   115         for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
       
   116             if (provider.getScheme().equalsIgnoreCase(scheme)) {
       
   117                 return provider.getPath(uri);
       
   118             }
       
   119         }
       
   120 
       
   121         throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed");
       
   122     }
       
   123 }