1e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera/* 2e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 3e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 5e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * This code is free software; you can redistribute it and/or modify it 6e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * under the terms of the GNU General Public License version 2 only, as 7e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * published by the Free Software Foundation. Oracle designates this 8e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * particular file as subject to the "Classpath" exception as provided 9e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * by Oracle in the LICENSE file that accompanied this code. 10e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 11e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * This code is distributed in the hope that it will be useful, but WITHOUT 12e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * version 2 for more details (a copy is included in the LICENSE file that 15e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * accompanied this code). 16e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 17e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * You should have received a copy of the GNU General Public License version 18e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 2 along with this work; if not, write to the Free Software Foundation, 19e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 21e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * or visit www.oracle.com if you need additional information or have any 23e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * questions. 24e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 25e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 26e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmerapackage java.nio.file; 27e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 28e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.spi.FileSystemProvider; 29e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.net.URI; 30e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 31e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera/** 32e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * This class consists exclusively of static methods that return a {@link Path} 33e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * by converting a path string or {@link URI}. 34e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 35e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @since 1.7 36e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 37e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 38e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmerapublic final class Paths { 39e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera private Paths() { } 40e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 41e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera /** 42e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Converts a path string, or a sequence of strings that when joined form 43e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * a path string, to a {@code Path}. If {@code more} does not specify any 44e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * elements then the value of the {@code first} parameter is the path string 45e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * to convert. If {@code more} specifies one or more elements then each 46e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * non-empty string, including {@code first}, is considered to be a sequence 47e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * of name elements (see {@link Path}) and is joined to form a path string. 48e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * The details as to how the Strings are joined is provider specific but 49e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * typically they will be joined using the {@link FileSystem#getSeparator 50e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * name-separator} as the separator. For example, if the name separator is 51e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * "{@code /}" and {@code getPath("/foo","bar","gus")} is invoked, then the 52e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * path string {@code "/foo/bar/gus"} is converted to a {@code Path}. 53e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * A {@code Path} representing an empty path is returned if {@code first} 54e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * is the empty string and {@code more} does not contain any non-empty 55e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * strings. 56e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 57e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> The {@code Path} is obtained by invoking the {@link FileSystem#getPath 58e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * getPath} method of the {@link FileSystems#getDefault default} {@link 59e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * FileSystem}. 60e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 61e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> Note that while this method is very convenient, using it will imply 62e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * an assumed reference to the default {@code FileSystem} and limit the 63e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * utility of the calling code. Hence it should not be used in library code 64e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * intended for flexible reuse. A more flexible alternative is to use an 65e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * existing {@code Path} instance as an anchor, such as: 66e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <pre> 67e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Path dir = ... 68e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Path path = dir.resolve("file"); 69e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * </pre> 70e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 71e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @param first 72e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the path string or initial part of the path string 73e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @param more 74e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * additional strings to be joined to form the path string 75e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 76e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @return the resulting {@code Path} 77e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 78e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws InvalidPathException 79e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * if the path string cannot be converted to a {@code Path} 80e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 81e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @see FileSystem#getPath 82e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 83e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera public static Path get(String first, String... more) { 84e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera return FileSystems.getDefault().getPath(first, more); 85e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera } 86e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 87e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera /** 88e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Converts the given URI to a {@link Path} object. 89e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 90e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> This method iterates over the {@link FileSystemProvider#installedProviders() 91e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * installed} providers to locate the provider that is identified by the 92e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * URI {@link URI#getScheme scheme} of the given URI. URI schemes are 93e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * compared without regard to case. If the provider is found then its {@link 94e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * FileSystemProvider#getPath getPath} method is invoked to convert the 95e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * URI. 96e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 97e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> In the case of the default provider, identified by the URI scheme 98e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * "file", the given URI has a non-empty path component, and undefined query 99e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * and fragment components. Whether the authority component may be present 100e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * is platform specific. The returned {@code Path} is associated with the 101e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * {@link FileSystems#getDefault default} file system. 102e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 103e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> The default provider provides a similar <em>round-trip</em> guarantee 104e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * to the {@link java.io.File} class. For a given {@code Path} <i>p</i> it 105e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * is guaranteed that 106e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <blockquote><tt> 107e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Paths.get(</tt><i>p</i><tt>.{@link Path#toUri() toUri}()).equals(</tt> 108e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <i>p</i><tt>.{@link Path#toAbsolutePath() toAbsolutePath}())</tt> 109e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * </blockquote> 110e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * so long as the original {@code Path}, the {@code URI}, and the new {@code 111e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Path} are all created in (possibly different invocations of) the same 112e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Java virtual machine. Whether other providers make any guarantees is 113e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * provider specific and therefore unspecified. 114e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 115e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @param uri 116e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the URI to convert 117e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 118e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @return the resulting {@code Path} 119e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * 120e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws IllegalArgumentException 121e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * if preconditions on the {@code uri} parameter do not hold. The 122e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * format of the URI is provider specific. 123e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws FileSystemNotFoundException 124e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * The file system, identified by the URI, does not exist and 125e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * cannot be created automatically, or the provider identified by 126e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * the URI's scheme component is not installed 127e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @throws SecurityException 128e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * if a security manager is installed and it denies an unspecified 129e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * permission to access the file system 130e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */ 131e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera public static Path get(URI uri) { 132e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera String scheme = uri.getScheme(); 133e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera if (scheme == null) 134e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera throw new IllegalArgumentException("Missing scheme"); 135e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 136e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera // check for default provider to avoid loading of installed providers 137e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera if (scheme.equalsIgnoreCase("file")) 138e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera return FileSystems.getDefault().provider().getPath(uri); 139e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 140e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera // try to find provider 141e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera for (FileSystemProvider provider: FileSystemProvider.installedProviders()) { 142e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera if (provider.getScheme().equalsIgnoreCase(scheme)) { 143e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera return provider.getPath(uri); 144e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera } 145e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera } 146e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera 147e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera throw new FileSystemNotFoundException("Provider \"" + scheme + "\" not installed"); 148e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera } 149e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera} 150