src/java.base/share/classes/java/nio/file/Path.java
changeset 49285 4d2e3f5abb48
parent 47216 71c04702a3d5
equal deleted inserted replaced
49284:a51ca91c2cde 49285:4d2e3f5abb48
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    26 package java.nio.file;
    26 package java.nio.file;
    27 
    27 
    28 import java.io.File;
    28 import java.io.File;
    29 import java.io.IOException;
    29 import java.io.IOException;
    30 import java.net.URI;
    30 import java.net.URI;
       
    31 import java.nio.file.spi.FileSystemProvider;
    31 import java.util.Iterator;
    32 import java.util.Iterator;
    32 import java.util.NoSuchElementException;
    33 import java.util.NoSuchElementException;
    33 
    34 
    34 /**
    35 /**
    35  * An object that may be used to locate a file in a file system. It will
    36  * An object that may be used to locate a file in a file system. It will
    91  * <h2>Concurrency</h2>
    92  * <h2>Concurrency</h2>
    92  * <p> Implementations of this interface are immutable and safe for use by
    93  * <p> Implementations of this interface are immutable and safe for use by
    93  * multiple concurrent threads.
    94  * multiple concurrent threads.
    94  *
    95  *
    95  * @since 1.7
    96  * @since 1.7
    96  * @see Paths
       
    97  */
    97  */
    98 
    98 
    99 public interface Path
    99 public interface Path
   100     extends Comparable<Path>, Iterable<Path>, Watchable
   100     extends Comparable<Path>, Iterable<Path>, Watchable
   101 {
   101 {
       
   102     /**
       
   103      * Returns a {@code Path} by converting a path string, or a sequence of
       
   104      * strings that when joined form a path string. If {@code more} does not
       
   105      * specify any elements then the value of the {@code first} parameter is
       
   106      * the path string to convert. If {@code more} specifies one or more
       
   107      * elements then each non-empty string, including {@code first}, is
       
   108      * considered to be a sequence of name elements and is joined to form a
       
   109      * path string. The details as to how the Strings are joined is provider
       
   110      * specific but typically they will be joined using the
       
   111      * {@link FileSystem#getSeparator name-separator} as the separator.
       
   112      * For example, if the name separator is "{@code /}" and
       
   113      * {@code getPath("/foo","bar","gus")} is invoked, then the path string
       
   114      * {@code "/foo/bar/gus"} is converted to a {@code Path}. A {@code Path}
       
   115      * representing an empty path is returned if {@code first} is the empty
       
   116      * string and {@code more} does not contain any non-empty strings.
       
   117      *
       
   118      * <p> The {@code Path} is obtained by invoking the {@link FileSystem#getPath
       
   119      * getPath} method of the {@link FileSystems#getDefault default} {@link
       
   120      * FileSystem}.
       
   121      *
       
   122      * <p> Note that while this method is very convenient, using it will imply
       
   123      * an assumed reference to the default {@code FileSystem} and limit the
       
   124      * utility of the calling code. Hence it should not be used in library code
       
   125      * intended for flexible reuse. A more flexible alternative is to use an
       
   126      * existing {@code Path} instance as an anchor, such as:
       
   127      * <pre>{@code
       
   128      *     Path dir = ...
       
   129      *     Path path = dir.resolve("file");
       
   130      * }</pre>
       
   131      *
       
   132      * @param   first
       
   133      *          the path string or initial part of the path string
       
   134      * @param   more
       
   135      *          additional strings to be joined to form the path string
       
   136      *
       
   137      * @return  the resulting {@code Path}
       
   138      *
       
   139      * @throws  InvalidPathException
       
   140      *          if the path string cannot be converted to a {@code Path}
       
   141      *
       
   142      * @see FileSystem#getPath
       
   143      *
       
   144      * @since 11
       
   145      */
       
   146     public static Path of(String first, String... more) {
       
   147         return FileSystems.getDefault().getPath(first, more);
       
   148     }
       
   149 
       
   150     /**
       
   151      * Returns a {@code Path} by converting a URI.
       
   152      *
       
   153      * <p> This method iterates over the {@link FileSystemProvider#installedProviders()
       
   154      * installed} providers to locate the provider that is identified by the
       
   155      * URI {@link URI#getScheme scheme} of the given URI. URI schemes are
       
   156      * compared without regard to case. If the provider is found then its {@link
       
   157      * FileSystemProvider#getPath getPath} method is invoked to convert the
       
   158      * URI.
       
   159      *
       
   160      * <p> In the case of the default provider, identified by the URI scheme
       
   161      * "file", the given URI has a non-empty path component, and undefined query
       
   162      * and fragment components. Whether the authority component may be present
       
   163      * is platform specific. The returned {@code Path} is associated with the
       
   164      * {@link FileSystems#getDefault default} file system.
       
   165      *
       
   166      * <p> The default provider provides a similar <em>round-trip</em> guarantee
       
   167      * to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it
       
   168      * is guaranteed that
       
   169      * <blockquote>{@code
       
   170      * Path.of(}<i>p</i>{@code .}{@link Path#toUri() toUri}{@code ()).equals(}
       
   171      * <i>p</i>{@code .}{@link Path#toAbsolutePath() toAbsolutePath}{@code ())}
       
   172      * </blockquote>
       
   173      * so long as the original {@code Path}, the {@code URI}, and the new {@code
       
   174      * Path} are all created in (possibly different invocations of) the same
       
   175      * Java virtual machine. Whether other providers make any guarantees is
       
   176      * provider specific and therefore unspecified.
       
   177      *
       
   178      * @param   uri
       
   179      *          the URI to convert
       
   180      *
       
   181      * @return  the resulting {@code Path}
       
   182      *
       
   183      * @throws  IllegalArgumentException
       
   184      *          if preconditions on the {@code uri} parameter do not hold. The
       
   185      *          format of the URI is provider specific.
       
   186      * @throws  FileSystemNotFoundException
       
   187      *          The file system, identified by the URI, does not exist and
       
   188      *          cannot be created automatically, or the provider identified by
       
   189      *          the URI's scheme component is not installed
       
   190      * @throws  SecurityException
       
   191      *          if a security manager is installed and it denies an unspecified
       
   192      *          permission to access the file system
       
   193      *
       
   194      * @since 11
       
   195      */
       
   196     public static Path of(URI uri) {
       
   197         String scheme =  uri.getScheme();
       
   198         if (scheme == null)
       
   199             throw new IllegalArgumentException("Missing scheme");
       
   200 
       
   201         // check for default provider to avoid loading of installed providers
       
   202         if (scheme.equalsIgnoreCase("file"))
       
   203             return FileSystems.getDefault().provider().getPath(uri);
       
   204 
       
   205         // try to find provider
       
   206         for (FileSystemProvider provider: FileSystemProvider.installedProviders()) {
       
   207             if (provider.getScheme().equalsIgnoreCase(scheme)) {
       
   208                 return provider.getPath(uri);
       
   209             }
       
   210         }
       
   211 
       
   212         throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed");
       
   213     }
       
   214 
   102     /**
   215     /**
   103      * Returns the file system that created this object.
   216      * Returns the file system that created this object.
   104      *
   217      *
   105      * @return  the file system that created this object
   218      * @return  the file system that created this object
   106      */
   219      */
   525      *
   638      *
   526      * <p> The default provider provides a similar <em>round-trip</em> guarantee
   639      * <p> The default provider provides a similar <em>round-trip</em> guarantee
   527      * to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it
   640      * to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it
   528      * is guaranteed that
   641      * is guaranteed that
   529      * <blockquote>
   642      * <blockquote>
   530      * {@link Paths#get(URI) Paths.get}{@code (}<i>p</i>{@code .toUri()).equals(}<i>p</i>
   643      * {@link Path#of(URI) Path.of}{@code (}<i>p</i>{@code .toUri()).equals(}<i>p</i>
   531      * {@code .}{@link #toAbsolutePath() toAbsolutePath}{@code ())}
   644      * {@code .}{@link #toAbsolutePath() toAbsolutePath}{@code ())}
   532      * </blockquote>
   645      * </blockquote>
   533      * so long as the original {@code Path}, the {@code URI}, and the new {@code
   646      * so long as the original {@code Path}, the {@code URI}, and the new {@code
   534      * Path} are all created in (possibly different invocations of) the same
   647      * Path} are all created in (possibly different invocations of) the same
   535      * Java virtual machine. Whether other providers make any guarantees is
   648      * Java virtual machine. Whether other providers make any guarantees is