1e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera/*
2e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * Copyright (c) 2007, 2013, 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.io.BufferedReader;
29e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.BufferedWriter;
30e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.Closeable;
31e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.File;
32e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.IOException;
33e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.InputStream;
34e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.InputStreamReader;
35e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.OutputStream;
36e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.OutputStreamWriter;
37e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.Reader;
38e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.UncheckedIOException;
39e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.io.Writer;
40e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.channels.Channels;
41e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.channels.FileChannel;
42e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.channels.SeekableByteChannel;
43e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.charset.Charset;
44e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.charset.CharsetDecoder;
45e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.charset.CharsetEncoder;
46e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.charset.StandardCharsets;
47e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.BasicFileAttributeView;
48e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.BasicFileAttributes;
49e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.DosFileAttributes;   // javadoc
50e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.FileAttribute;
51e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.FileAttributeView;
52e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.FileOwnerAttributeView;
53e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.FileStoreAttributeView;
54e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.FileTime;
55e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.PosixFileAttributeView;
56e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.PosixFileAttributes;
57e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.PosixFilePermission;
58e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.attribute.UserPrincipal;
59e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.spi.FileSystemProvider;
60e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.nio.file.spi.FileTypeDetector;
61e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.security.AccessController;
62e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.security.PrivilegedAction;
63e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.ArrayList;
64e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.Arrays;
65e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.Collections;
66e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.EnumSet;
67e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.HashSet;
68e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.Iterator;
69e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.List;
70e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.Map;
71e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.Objects;
72e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.ServiceLoader;
73e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.Set;
74e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.Spliterator;
75e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.Spliterators;
76e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.function.BiPredicate;
77e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.stream.Stream;
78e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmeraimport java.util.stream.StreamSupport;
79e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
80e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera/**
81e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * This class consists exclusively of static methods that operate on files,
82e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * directories, or other types of files.
83e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera *
84e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * <p> In most cases, the methods defined here will delegate to the associated
85e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * file system provider to perform the file operations.
86e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera *
87e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * @since 1.7
88e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera */
89e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
90e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmerapublic final class Files {
91e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private Files() { }
92e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
93e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
94e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Returns the {@code FileSystemProvider} to delegate to.
95e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
96e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static FileSystemProvider provider(Path path) {
97e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return path.getFileSystem().provider();
98e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
99e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
100e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
101e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Convert a Closeable to a Runnable by converting checked IOException
102e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * to UncheckedIOException
103e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
104e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static Runnable asUncheckedRunnable(Closeable c) {
105e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return () -> {
106e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            try {
107e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                c.close();
108e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } catch (IOException e) {
109e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                throw new UncheckedIOException(e);
110e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
111e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        };
112e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
113e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
114e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- File contents --
115e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
116e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
117e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens a file, returning an input stream to read from the file. The stream
118e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * will not be buffered, and is not required to support the {@link
119e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * InputStream#mark mark} or {@link InputStream#reset reset} methods. The
120e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream will be safe for access by multiple concurrent threads. Reading
121e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * commences at the beginning of the file. Whether the returned stream is
122e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <i>asynchronously closeable</i> and/or <i>interruptible</i> is highly
123e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file system provider specific and therefore not specified.
124e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
125e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} parameter determines how the file is opened.
126e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * If no options are present then it is equivalent to opening the file with
127e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the {@link StandardOpenOption#READ READ} option. In addition to the {@code
128e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * READ} option, an implementation may also support additional implementation
129e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * specific options.
130e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
131e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
132e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to open
133e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
134e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the file is opened
135e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
136e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new input stream
137e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
138e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
139e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an invalid combination of options is specified
140e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
141e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an unsupported option is specified
142e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
143e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
144e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
145e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
146e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
147e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
148e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
149e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static InputStream newInputStream(Path path, OpenOption... options)
150e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
151e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
152e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).newInputStream(path, options);
153e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
154e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
155e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
156e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens or creates a file, returning an output stream that may be used to
157e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * write bytes to the file. The resulting stream will not be buffered. The
158e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream will be safe for access by multiple concurrent threads. Whether
159e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the returned stream is <i>asynchronously closeable</i> and/or
160e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <i>interruptible</i> is highly file system provider specific and
161e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * therefore not specified.
162e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
163e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method opens or creates a file in exactly the manner specified
164e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * by the {@link #newByteChannel(Path,Set,FileAttribute[]) newByteChannel}
165e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method with the exception that the {@link StandardOpenOption#READ READ}
166e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * option may not be present in the array of options. If no options are
167e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * present then this method works as if the {@link StandardOpenOption#CREATE
168e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * CREATE}, {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING},
169e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * and {@link StandardOpenOption#WRITE WRITE} options are present. In other
170e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * words, it opens the file for writing, creating the file if it doesn't
171e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * exist, or initially truncating an existing {@link #isRegularFile
172e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * regular-file} to a size of {@code 0} if it exists.
173e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
174e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Examples:</b>
175e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
176e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path path = ...
177e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
178e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     // truncate and overwrite an existing file, or create the file if
179e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     // it doesn't initially exist
180e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     OutputStream out = Files.newOutputStream(path);
181e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
182e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     // append to an existing file, fail if the file does not exist
183e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     out = Files.newOutputStream(path, APPEND);
184e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
185e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     // append to an existing file, create file if it doesn't initially exist
186e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     out = Files.newOutputStream(path, CREATE, APPEND);
187e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
188e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     // always create new file, failing if it already exists
189e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     out = Files.newOutputStream(path, CREATE_NEW);
190e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
191e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
192e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
193e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to open or create
194e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
195e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the file is opened
196e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
197e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new output stream
198e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
199e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
200e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if {@code options} contains an invalid combination of options
201e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
202e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an unsupported option is specified
203e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
204e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
205e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
206e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
207e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
208e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the file. The {@link
209e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkDelete(String) checkDelete} method is
210e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          invoked to check delete access if the file is opened with the
211e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          {@code DELETE_ON_CLOSE} option.
212e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
213e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static OutputStream newOutputStream(Path path, OpenOption... options)
214e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
215e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
216e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).newOutputStream(path, options);
217e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
218e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
219e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
220e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens or creates a file, returning a seekable byte channel to access the
221e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file.
222e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
223e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} parameter determines how the file is opened.
224e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The {@link StandardOpenOption#READ READ} and {@link
225e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#WRITE WRITE} options determine if the file should be
226e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * opened for reading and/or writing. If neither option (or the {@link
227e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#APPEND APPEND} option) is present then the file is
228e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * opened for reading. By default reading or writing commence at the
229e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * beginning of the file.
230e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
231e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> In the addition to {@code READ} and {@code WRITE}, the following
232e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * options may be present:
233e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
234e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <table border=1 cellpadding=5 summary="Options">
235e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr> <th>Option</th> <th>Description</th> </tr>
236e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
237e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link StandardOpenOption#APPEND APPEND} </td>
238e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> If this option is present then the file is opened for writing and
239e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     each invocation of the channel's {@code write} method first advances
240e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     the position to the end of the file and then writes the requested
241e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     data. Whether the advancement of the position and the writing of the
242e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     data are done in a single atomic operation is system-dependent and
243e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     therefore unspecified. This option may not be used in conjunction
244e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     with the {@code READ} or {@code TRUNCATE_EXISTING} options. </td>
245e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
246e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
247e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </td>
248e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> If this option is present then the existing file is truncated to
249e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   a size of 0 bytes. This option is ignored when the file is opened only
250e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   for reading. </td>
251e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
252e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
253e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </td>
254e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> If this option is present then a new file is created, failing if
255e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   the file already exists or is a symbolic link. When creating a file the
256e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   check for the existence of the file and the creation of the file if it
257e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   does not exist is atomic with respect to other file system operations.
258e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   This option is ignored when the file is opened only for reading. </td>
259e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
260e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
261e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td > {@link StandardOpenOption#CREATE CREATE} </td>
262e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> If this option is present then an existing file is opened if it
263e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   exists, otherwise a new file is created. This option is ignored if the
264e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   {@code CREATE_NEW} option is also present or the file is opened only
265e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   for reading. </td>
266e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
267e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
268e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </td>
269e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> When this option is present then the implementation makes a
270e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <em>best effort</em> attempt to delete the file when closed by the
271e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   {@link SeekableByteChannel#close close} method. If the {@code close}
272e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   method is not invoked then a <em>best effort</em> attempt is made to
273e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   delete the file when the Java virtual machine terminates. </td>
274e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
275e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
276e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td>{@link StandardOpenOption#SPARSE SPARSE} </td>
277e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> When creating a new file this option is a <em>hint</em> that the
278e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   new file will be sparse. This option is ignored when not creating
279e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   a new file. </td>
280e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
281e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
282e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link StandardOpenOption#SYNC SYNC} </td>
283e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> Requires that every update to the file's content or metadata be
284e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   written synchronously to the underlying storage device. (see <a
285e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   href="package-summary.html#integrity"> Synchronized I/O file
286e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   integrity</a>). </td>
287e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
288e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
289e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link StandardOpenOption#DSYNC DSYNC} </td>
290e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> Requires that every update to the file's content be written
291e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   synchronously to the underlying storage device. (see <a
292e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   href="package-summary.html#integrity"> Synchronized I/O file
293e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   integrity</a>). </td>
294e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
295e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </table>
296e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
297e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> An implementation may also support additional implementation specific
298e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * options.
299e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
300e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attrs} parameter is optional {@link FileAttribute
301e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file-attributes} to set atomically when a new file is created.
302e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
303e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> In the case of the default provider, the returned seekable byte channel
304e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is a {@link java.nio.channels.FileChannel}.
305e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
306e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Examples:</b>
307e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
308e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path path = ...
309e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
310e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     // open file for reading
311e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     ReadableByteChannel rbc = Files.newByteChannel(path, EnumSet.of(READ)));
312e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
313e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     // open file for writing to the end of an existing file, creating
314e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     // the file if it doesn't already exist
315e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     WritableByteChannel wbc = Files.newByteChannel(path, EnumSet.of(CREATE,APPEND));
316e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
317e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     // create file with initial permissions, opening it for both reading and writing
318e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     {@code FileAttribute<Set<PosixFilePermission>> perms = ...}
319e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     SeekableByteChannel sbc = Files.newByteChannel(path, EnumSet.of(CREATE_NEW,READ,WRITE), perms);
320e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
321e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
322e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
323e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to open or create
324e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
325e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the file is opened
326e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attrs
327e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an optional list of file attributes to set atomically when
328e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          creating the file
329e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
330e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new seekable byte channel
331e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
332e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
333e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the set contains an invalid combination of options
334e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
335e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an unsupported open option is specified or the array contains
336e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          attributes that cannot be set atomically when creating the file
337e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
338e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if a file of that name already exists and the {@link
339e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified
340e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          <i>(optional specific exception)</i>
341e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
342e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
343e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
344e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
345e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
346e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the path if the file is
347e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          opened for reading. The {@link SecurityManager#checkWrite(String)
348e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          checkWrite} method is invoked to check write access to the path
349e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file is opened for writing. The {@link
350e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkDelete(String) checkDelete} method is
351e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          invoked to check delete access if the file is opened with the
352e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          {@code DELETE_ON_CLOSE} option.
353e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
354e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see java.nio.channels.FileChannel#open(Path,Set,FileAttribute[])
355e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
356e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static SeekableByteChannel newByteChannel(Path path,
357e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                     Set<? extends OpenOption> options,
358e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                     FileAttribute<?>... attrs)
359e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
360e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
361e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).newByteChannel(path, options, attrs);
362e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
363e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
364e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
365e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens or creates a file, returning a seekable byte channel to access the
366e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file.
367e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
368e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method opens or creates a file in exactly the manner specified
369e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * by the {@link #newByteChannel(Path,Set,FileAttribute[]) newByteChannel}
370e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method.
371e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
372e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
373e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to open or create
374e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
375e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the file is opened
376e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
377e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new seekable byte channel
378e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
379e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
380e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the set contains an invalid combination of options
381e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
382e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an unsupported open option is specified
383e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
384e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if a file of that name already exists and the {@link
385e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified
386e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          <i>(optional specific exception)</i>
387e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
388e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
389e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
390e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
391e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
392e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the path if the file is
393e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          opened for reading. The {@link SecurityManager#checkWrite(String)
394e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          checkWrite} method is invoked to check write access to the path
395e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file is opened for writing. The {@link
396e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkDelete(String) checkDelete} method is
397e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          invoked to check delete access if the file is opened with the
398e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          {@code DELETE_ON_CLOSE} option.
399e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
400e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see java.nio.channels.FileChannel#open(Path,OpenOption[])
401e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
402e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static SeekableByteChannel newByteChannel(Path path, OpenOption... options)
403e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
404e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
405e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Set<OpenOption> set = new HashSet<OpenOption>(options.length);
406e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Collections.addAll(set, options);
407e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return newByteChannel(path, set);
408e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
409e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
410e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- Directories --
411e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
412e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static class AcceptAllFilter
413e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        implements DirectoryStream.Filter<Path>
414e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
415e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        private AcceptAllFilter() { }
416e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
417e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        @Override
418e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        public boolean accept(Path entry) { return true; }
419e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
420e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        static final AcceptAllFilter FILTER = new AcceptAllFilter();
421e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
422e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
423e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
424e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens a directory, returning a {@link DirectoryStream} to iterate over
425e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * all entries in the directory. The elements returned by the directory
426e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@link DirectoryStream#iterator iterator} are of type {@code
427e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Path}, each one representing an entry in the directory. The {@code Path}
428e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * objects are obtained as if by {@link Path#resolve(Path) resolving} the
429e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * name of the directory entry against {@code dir}.
430e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
431e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> When not using the try-with-resources construct, then directory
432e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@code close} method should be invoked after iteration is
433e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * completed so as to free any resources held for the open directory.
434e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
435e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> When an implementation supports operations on entries in the
436e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory that execute in a race-free manner then the returned directory
437e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream is a {@link SecureDirectoryStream}.
438e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
439e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   dir
440e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the directory
441e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
442e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new and open {@code DirectoryStream} object
443e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
444e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  NotDirectoryException
445e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file could not otherwise be opened because it is not
446e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          a directory <i>(optional specific exception)</i>
447e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
448e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
449e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
450e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
451e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
452e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the directory.
453e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
454e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static DirectoryStream<Path> newDirectoryStream(Path dir)
455e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
456e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
457e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(dir).newDirectoryStream(dir, AcceptAllFilter.FILTER);
458e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
459e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
460e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
461e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens a directory, returning a {@link DirectoryStream} to iterate over
462e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the entries in the directory. The elements returned by the directory
463e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@link DirectoryStream#iterator iterator} are of type {@code
464e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Path}, each one representing an entry in the directory. The {@code Path}
465e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * objects are obtained as if by {@link Path#resolve(Path) resolving} the
466e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * name of the directory entry against {@code dir}. The entries returned by
467e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the iterator are filtered by matching the {@code String} representation
468e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of their file names against the given <em>globbing</em> pattern.
469e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
470e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> For example, suppose we want to iterate over the files ending with
471e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * ".java" in a directory:
472e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
473e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path dir = ...
474e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir, "*.java")) {
475e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         :
476e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     }
477e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
478e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
479e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The globbing pattern is specified by the {@link
480e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileSystem#getPathMatcher getPathMatcher} method.
481e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
482e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> When not using the try-with-resources construct, then directory
483e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@code close} method should be invoked after iteration is
484e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * completed so as to free any resources held for the open directory.
485e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
486e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> When an implementation supports operations on entries in the
487e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory that execute in a race-free manner then the returned directory
488e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream is a {@link SecureDirectoryStream}.
489e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
490e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   dir
491e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the directory
492e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   glob
493e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the glob pattern
494e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
495e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new and open {@code DirectoryStream} object
496e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
497e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  java.util.regex.PatternSyntaxException
498e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the pattern is invalid
499e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  NotDirectoryException
500e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file could not otherwise be opened because it is not
501e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          a directory <i>(optional specific exception)</i>
502e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
503e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
504e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
505e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
506e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
507e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the directory.
508e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
509e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static DirectoryStream<Path> newDirectoryStream(Path dir, String glob)
510e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
511e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
512e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // avoid creating a matcher if all entries are required.
513e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (glob.equals("*"))
514e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return newDirectoryStream(dir);
515e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
516e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // create a matcher and return a filter that uses it.
517e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        FileSystem fs = dir.getFileSystem();
518e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        final PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
519e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
520e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            @Override
521e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            public boolean accept(Path entry)  {
522e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                return matcher.matches(entry.getFileName());
523e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
524e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        };
525e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return fs.provider().newDirectoryStream(dir, filter);
526e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
527e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
528e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
529e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens a directory, returning a {@link DirectoryStream} to iterate over
530e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the entries in the directory. The elements returned by the directory
531e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@link DirectoryStream#iterator iterator} are of type {@code
532e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Path}, each one representing an entry in the directory. The {@code Path}
533e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * objects are obtained as if by {@link Path#resolve(Path) resolving} the
534e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * name of the directory entry against {@code dir}. The entries returned by
535e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the iterator are filtered by the given {@link DirectoryStream.Filter
536e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * filter}.
537e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
538e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> When not using the try-with-resources construct, then directory
539e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@code close} method should be invoked after iteration is
540e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * completed so as to free any resources held for the open directory.
541e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
542e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Where the filter terminates due to an uncaught error or runtime
543e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * exception then it is propagated to the {@link Iterator#hasNext()
544e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * hasNext} or {@link Iterator#next() next} method. Where an {@code
545e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * IOException} is thrown, it results in the {@code hasNext} or {@code
546e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * next} method throwing a {@link DirectoryIteratorException} with the
547e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@code IOException} as the cause.
548e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
549e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> When an implementation supports operations on entries in the
550e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory that execute in a race-free manner then the returned directory
551e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream is a {@link SecureDirectoryStream}.
552e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
553e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Example:</b>
554e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Suppose we want to iterate over the files in a directory that are
555e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * larger than 8K.
556e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
557e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     DirectoryStream.Filter&lt;Path&gt; filter = new DirectoryStream.Filter&lt;Path&gt;() {
558e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         public boolean accept(Path file) throws IOException {
559e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *             return (Files.size(file) &gt; 8192L);
560e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         }
561e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     };
562e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path dir = ...
563e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir, filter)) {
564e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         :
565e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     }
566e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
567e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
568e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   dir
569e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the directory
570e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   filter
571e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the directory stream filter
572e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
573e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new and open {@code DirectoryStream} object
574e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
575e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  NotDirectoryException
576e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file could not otherwise be opened because it is not
577e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          a directory <i>(optional specific exception)</i>
578e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
579e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
580e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
581e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
582e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
583e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the directory.
584e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
585e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static DirectoryStream<Path> newDirectoryStream(Path dir,
586e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                           DirectoryStream.Filter<? super Path> filter)
587e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
588e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
589e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(dir).newDirectoryStream(dir, filter);
590e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
591e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
592e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- Creation and deletion --
593e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
594e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
595e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Creates a new and empty file, failing if the file already exists. The
596e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * check for the existence of the file and the creation of the new file if
597e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * it does not exist are a single operation that is atomic with respect to
598e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * all other filesystem activities that might affect the directory.
599e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
600e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attrs} parameter is optional {@link FileAttribute
601e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file-attributes} to set atomically when creating the file. Each attribute
602e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is identified by its {@link FileAttribute#name name}. If more than one
603e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attribute of the same name is included in the array then all but the last
604e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * occurrence is ignored.
605e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
606e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
607e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to create
608e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attrs
609e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an optional list of file attributes to set atomically when
610e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          creating the file
611e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
612e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the file
613e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
614e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
615e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the array contains an attribute that cannot be set atomically
616e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          when creating the file
617e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
618e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if a file of that name already exists
619e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          <i>(optional specific exception)</i>
620e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
621e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs or the parent directory does not exist
622e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
623e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
624e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
625e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the new file.
626e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
627e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path createFile(Path path, FileAttribute<?>... attrs)
628e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
629e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
630e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        EnumSet<StandardOpenOption> options =
631e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            EnumSet.<StandardOpenOption>of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
632e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        newByteChannel(path, options, attrs).close();
633e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return path;
634e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
635e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
636e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
637e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Creates a new directory. The check for the existence of the file and the
638e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * creation of the directory if it does not exist are a single operation
639e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that is atomic with respect to all other filesystem activities that might
640e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * affect the directory. The {@link #createDirectories createDirectories}
641e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method should be used where it is required to create all nonexistent
642e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * parent directories first.
643e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
644e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attrs} parameter is optional {@link FileAttribute
645e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file-attributes} to set atomically when creating the directory. Each
646e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attribute is identified by its {@link FileAttribute#name name}. If more
647e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * than one attribute of the same name is included in the array then all but
648e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the last occurrence is ignored.
649e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
650e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   dir
651e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the directory to create
652e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attrs
653e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an optional list of file attributes to set atomically when
654e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          creating the directory
655e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
656e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the directory
657e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
658e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
659e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the array contains an attribute that cannot be set atomically
660e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          when creating the directory
661e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
662e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if a directory could not otherwise be created because a file of
663e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          that name already exists <i>(optional specific exception)</i>
664e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
665e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs or the parent directory does not exist
666e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
667e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
668e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
669e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the new directory.
670e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
671e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path createDirectory(Path dir, FileAttribute<?>... attrs)
672e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
673e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
674e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        provider(dir).createDirectory(dir, attrs);
675e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return dir;
676e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
677e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
678e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
679e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Creates a directory by creating all nonexistent parent directories first.
680e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Unlike the {@link #createDirectory createDirectory} method, an exception
681e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is not thrown if the directory could not be created because it already
682e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * exists.
683e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
684e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attrs} parameter is optional {@link FileAttribute
685e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file-attributes} to set atomically when creating the nonexistent
686e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directories. Each file attribute is identified by its {@link
687e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileAttribute#name name}. If more than one attribute of the same name is
688e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * included in the array then all but the last occurrence is ignored.
689e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
690e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If this method fails, then it may do so after creating some, but not
691e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * all, of the parent directories.
692e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
693e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   dir
694e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the directory to create
695e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
696e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attrs
697e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an optional list of file attributes to set atomically when
698e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          creating the directory
699e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
700e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the directory
701e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
702e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
703e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the array contains an attribute that cannot be set atomically
704e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          when creating the directory
705e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
706e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if {@code dir} exists but is not a directory <i>(optional specific
707e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          exception)</i>
708e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
709e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
710e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
711e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          in the case of the default provider, and a security manager is
712e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
713e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked prior to attempting to create a directory and
714e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          its {@link SecurityManager#checkRead(String) checkRead} is
715e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          invoked for each parent directory that is checked. If {@code
716e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          dir} is not an absolute path then its {@link Path#toAbsolutePath
717e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          toAbsolutePath} may need to be invoked to get its absolute path.
718e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          This may invoke the security manager's {@link
719e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkPropertyAccess(String) checkPropertyAccess}
720e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method to check access to the system property {@code user.dir}
721e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
722e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path createDirectories(Path dir, FileAttribute<?>... attrs)
723e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
724e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
725e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // attempt to create the directory
726e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
727e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            createAndCheckIsDirectory(dir, attrs);
728e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return dir;
729e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (FileAlreadyExistsException x) {
730e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // file exists and is not a directory
731e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw x;
732e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (IOException x) {
733e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // parent may not exist or other reason
734e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
735e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        SecurityException se = null;
736e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
737e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            dir = dir.toAbsolutePath();
738e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (SecurityException x) {
739e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // don't have permission to get absolute path
740e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            se = x;
741e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
742e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // find a decendent that exists
743e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Path parent = dir.getParent();
744e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        while (parent != null) {
745e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            try {
746e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                provider(parent).checkAccess(parent);
747e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                break;
748e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } catch (NoSuchFileException x) {
749e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                // does not exist
750e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
751e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            parent = parent.getParent();
752e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
753e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (parent == null) {
754e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // unable to find existing parent
755e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (se == null) {
756e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                throw new FileSystemException(dir.toString(), null,
757e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    "Unable to determine if root directory exists");
758e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } else {
759e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                throw se;
760e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
761e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
762e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
763e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // create directories
764e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Path child = parent;
765e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        for (Path name: parent.relativize(dir)) {
766e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            child = child.resolve(name);
767e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            createAndCheckIsDirectory(child, attrs);
768e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
769e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return dir;
770e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
771e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
772e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
773e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Used by createDirectories to attempt to create a directory. A no-op
774e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * if the directory already exists.
775e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
776e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static void createAndCheckIsDirectory(Path dir,
777e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                  FileAttribute<?>... attrs)
778e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
779e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
780e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
781e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            createDirectory(dir, attrs);
782e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (FileAlreadyExistsException x) {
783e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
784e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                throw x;
785e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
786e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
787e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
788e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
789e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Creates a new empty file in the specified directory, using the given
790e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * prefix and suffix strings to generate its name. The resulting
791e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@code Path} is associated with the same {@code FileSystem} as the given
792e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory.
793e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
794e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The details as to how the name of the file is constructed is
795e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementation dependent and therefore not specified. Where possible
796e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the {@code prefix} and {@code suffix} are used to construct candidate
797e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * names in the same manner as the {@link
798e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * java.io.File#createTempFile(String,String,File)} method.
799e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
800e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> As with the {@code File.createTempFile} methods, this method is only
801e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * part of a temporary-file facility. Where used as a <em>work files</em>,
802e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the resulting file may be opened using the {@link
803e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} option so that the
804e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file is deleted when the appropriate {@code close} method is invoked.
805e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Alternatively, a {@link Runtime#addShutdownHook shutdown-hook}, or the
806e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link java.io.File#deleteOnExit} mechanism may be used to delete the
807e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file automatically.
808e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
809e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attrs} parameter is optional {@link FileAttribute
810e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file-attributes} to set atomically when creating the file. Each attribute
811e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is identified by its {@link FileAttribute#name name}. If more than one
812e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attribute of the same name is included in the array then all but the last
813e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * occurrence is ignored. When no file attributes are specified, then the
814e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * resulting file may have more restrictive access permissions to files
815e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * created by the {@link java.io.File#createTempFile(String,String,File)}
816e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method.
817e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
818e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   dir
819e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to directory in which to create the file
820e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   prefix
821e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the prefix string to be used in generating the file's name;
822e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          may be {@code null}
823e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   suffix
824e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the suffix string to be used in generating the file's name;
825e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          may be {@code null}, in which case "{@code .tmp}" is used
826e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attrs
827e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an optional list of file attributes to set atomically when
828e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          creating the file
829e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
830e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path to the newly created file that did not exist before
831e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          this method was invoked
832e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
833e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
834e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the prefix or suffix parameters cannot be used to generate
835e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          a candidate file name
836e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
837e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the array contains an attribute that cannot be set atomically
838e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          when creating the directory
839e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
840e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs or {@code dir} does not exist
841e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
842e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
843e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
844e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the file.
845e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
846e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path createTempFile(Path dir,
847e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                      String prefix,
848e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                      String suffix,
849e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                      FileAttribute<?>... attrs)
850e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
851e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
852e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return TempFileHelper.createTempFile(Objects.requireNonNull(dir),
853e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                             prefix, suffix, attrs);
854e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
855e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
856e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
857e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Creates an empty file in the default temporary-file directory, using
858e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the given prefix and suffix to generate its name. The resulting {@code
859e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Path} is associated with the default {@code FileSystem}.
860e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
861e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method works in exactly the manner specified by the
862e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link #createTempFile(Path,String,String,FileAttribute[])} method for
863e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the case that the {@code dir} parameter is the temporary-file directory.
864e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
865e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   prefix
866e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the prefix string to be used in generating the file's name;
867e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          may be {@code null}
868e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   suffix
869e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the suffix string to be used in generating the file's name;
870e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          may be {@code null}, in which case "{@code .tmp}" is used
871e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attrs
872e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an optional list of file attributes to set atomically when
873e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          creating the file
874e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
875e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path to the newly created file that did not exist before
876e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          this method was invoked
877e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
878e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
879e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the prefix or suffix parameters cannot be used to generate
880e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          a candidate file name
881e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
882e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the array contains an attribute that cannot be set atomically
883e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          when creating the directory
884e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
885e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs or the temporary-file directory does not
886e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          exist
887e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
888e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
889e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
890e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the file.
891e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
892e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path createTempFile(String prefix,
893e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                      String suffix,
894e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                      FileAttribute<?>... attrs)
895e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
896e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
897e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return TempFileHelper.createTempFile(null, prefix, suffix, attrs);
898e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
899e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
900e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
901e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Creates a new directory in the specified directory, using the given
902e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * prefix to generate its name.  The resulting {@code Path} is associated
903e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * with the same {@code FileSystem} as the given directory.
904e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
905e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The details as to how the name of the directory is constructed is
906e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementation dependent and therefore not specified. Where possible
907e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the {@code prefix} is used to construct candidate names.
908e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
909e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> As with the {@code createTempFile} methods, this method is only
910e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * part of a temporary-file facility. A {@link Runtime#addShutdownHook
911e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * shutdown-hook}, or the {@link java.io.File#deleteOnExit} mechanism may be
912e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * used to delete the directory automatically.
913e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
914e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attrs} parameter is optional {@link FileAttribute
915e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file-attributes} to set atomically when creating the directory. Each
916e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attribute is identified by its {@link FileAttribute#name name}. If more
917e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * than one attribute of the same name is included in the array then all but
918e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the last occurrence is ignored.
919e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
920e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   dir
921e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to directory in which to create the directory
922e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   prefix
923e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the prefix string to be used in generating the directory's name;
924e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          may be {@code null}
925e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attrs
926e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an optional list of file attributes to set atomically when
927e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          creating the directory
928e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
929e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path to the newly created directory that did not exist before
930e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          this method was invoked
931e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
932e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
933e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the prefix cannot be used to generate a candidate directory name
934e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
935e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the array contains an attribute that cannot be set atomically
936e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          when creating the directory
937e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
938e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs or {@code dir} does not exist
939e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
940e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
941e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
942e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access when creating the
943e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          directory.
944e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
945e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path createTempDirectory(Path dir,
946e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                           String prefix,
947e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                           FileAttribute<?>... attrs)
948e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
949e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
950e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return TempFileHelper.createTempDirectory(Objects.requireNonNull(dir),
951e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                  prefix, attrs);
952e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
953e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
954e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
955e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Creates a new directory in the default temporary-file directory, using
956e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the given prefix to generate its name. The resulting {@code Path} is
957e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * associated with the default {@code FileSystem}.
958e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
959e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method works in exactly the manner specified by {@link
960e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * #createTempDirectory(Path,String,FileAttribute[])} method for the case
961e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that the {@code dir} parameter is the temporary-file directory.
962e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
963e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   prefix
964e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the prefix string to be used in generating the directory's name;
965e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          may be {@code null}
966e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attrs
967e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an optional list of file attributes to set atomically when
968e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          creating the directory
969e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
970e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path to the newly created directory that did not exist before
971e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          this method was invoked
972e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
973e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
974e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the prefix cannot be used to generate a candidate directory name
975e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
976e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the array contains an attribute that cannot be set atomically
977e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          when creating the directory
978e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
979e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs or the temporary-file directory does not
980e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          exist
981e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
982e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
983e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
984e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access when creating the
985e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          directory.
986e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
987e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path createTempDirectory(String prefix,
988e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                           FileAttribute<?>... attrs)
989e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
990e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
991e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return TempFileHelper.createTempDirectory(null, prefix, attrs);
992e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
993e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
994e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
995e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Creates a symbolic link to a target <i>(optional operation)</i>.
996e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
997e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code target} parameter is the target of the link. It may be an
998e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link Path#isAbsolute absolute} or relative path and may not exist. When
999e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the target is a relative path then file system operations on the resulting
1000e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * link are relative to the path of the link.
1001e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1002e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attrs} parameter is optional {@link FileAttribute
1003e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attributes} to set atomically when creating the link. Each attribute is
1004e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * identified by its {@link FileAttribute#name name}. If more than one attribute
1005e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the same name is included in the array then all but the last occurrence
1006e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is ignored.
1007e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1008e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Where symbolic links are supported, but the underlying {@link FileStore}
1009e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * does not support symbolic links, then this may fail with an {@link
1010e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * IOException}. Additionally, some operating systems may require that the
1011e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Java virtual machine be started with implementation specific privileges to
1012e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * create symbolic links, in which case this method may throw {@code IOException}.
1013e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1014e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   link
1015e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path of the symbolic link to create
1016e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   target
1017e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the target of the symbolic link
1018e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attrs
1019e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the array of attributes to set atomically when creating the
1020e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          symbolic link
1021e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1022e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path to the symbolic link
1023e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1024e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1025e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the implementation does not support symbolic links or the
1026e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          array contains an attribute that cannot be set atomically when
1027e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          creating the symbolic link
1028e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
1029e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if a file with the name already exists <i>(optional specific
1030e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          exception)</i>
1031e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1032e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1033e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1034e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager
1035e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          is installed, it denies {@link LinkPermission}<tt>("symbolic")</tt>
1036e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          or its {@link SecurityManager#checkWrite(String) checkWrite}
1037e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies write access to the path of the symbolic link.
1038e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1039e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path createSymbolicLink(Path link, Path target,
1040e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                          FileAttribute<?>... attrs)
1041e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
1042e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
1043e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        provider(link).createSymbolicLink(link, target, attrs);
1044e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return link;
1045e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1046e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1047e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1048e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Creates a new link (directory entry) for an existing file <i>(optional
1049e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * operation)</i>.
1050e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1051e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code link} parameter locates the directory entry to create.
1052e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The {@code existing} parameter is the path to an existing file. This
1053e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method creates a new directory entry for the file so that it can be
1054e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * accessed using {@code link} as the path. On some file systems this is
1055e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * known as creating a "hard link". Whether the file attributes are
1056e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * maintained for the file or for each directory entry is file system
1057e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * specific and therefore not specified. Typically, a file system requires
1058e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that all links (directory entries) for a file be on the same file system.
1059e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Furthermore, on some platforms, the Java virtual machine may require to
1060e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * be started with implementation specific privileges to create hard links
1061e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * or to create links to directories.
1062e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1063e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   link
1064e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the link (directory entry) to create
1065e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   existing
1066e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          a path to an existing file
1067e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1068e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path to the link (directory entry)
1069e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1070e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1071e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the implementation does not support adding an existing file
1072e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to a directory
1073e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
1074e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the entry could not otherwise be created because a file of
1075e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          that name already exists <i>(optional specific exception)</i>
1076e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1077e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1078e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1079e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager
1080e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          is installed, it denies {@link LinkPermission}<tt>("hard")</tt>
1081e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          or its {@link SecurityManager#checkWrite(String) checkWrite}
1082e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies write access to either the link or the
1083e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          existing file.
1084e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1085e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path createLink(Path link, Path existing) throws IOException {
1086e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        provider(link).createLink(link, existing);
1087e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return link;
1088e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1089e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1090e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1091e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Deletes a file.
1092e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1093e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> An implementation may require to examine the file to determine if the
1094e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file is a directory. Consequently this method may not be atomic with respect
1095e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * to other file system operations.  If the file is a symbolic link then the
1096e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic link itself, not the final target of the link, is deleted.
1097e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1098e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If the file is a directory then the directory must be empty. In some
1099e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementations a directory has entries for special files or links that
1100e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are created when the directory is created. In such implementations a
1101e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory is considered empty when only the special entries exist.
1102e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * This method can be used with the {@link #walkFileTree walkFileTree}
1103e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method to delete a directory and all entries in the directory, or an
1104e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * entire <i>file-tree</i> where required.
1105e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1106e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> On some operating systems it may not be possible to remove a file when
1107e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * it is open and in use by this Java virtual machine or other programs.
1108e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1109e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1110e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to delete
1111e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1112e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  NoSuchFileException
1113e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file does not exist <i>(optional specific exception)</i>
1114e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  DirectoryNotEmptyException
1115e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file is a directory and could not otherwise be deleted
1116e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          because the directory is not empty <i>(optional specific
1117e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          exception)</i>
1118e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1119e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1120e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1121e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1122e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkDelete(String)} method
1123e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          is invoked to check delete access to the file
1124e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1125e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static void delete(Path path) throws IOException {
1126e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        provider(path).delete(path);
1127e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1128e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1129e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1130e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Deletes a file if it exists.
1131e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1132e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> As with the {@link #delete(Path) delete(Path)} method, an
1133e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementation may need to examine the file to determine if the file is a
1134e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory. Consequently this method may not be atomic with respect to
1135e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * other file system operations.  If the file is a symbolic link, then the
1136e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic link itself, not the final target of the link, is deleted.
1137e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1138e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If the file is a directory then the directory must be empty. In some
1139e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementations a directory has entries for special files or links that
1140e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are created when the directory is created. In such implementations a
1141e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory is considered empty when only the special entries exist.
1142e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1143e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> On some operating systems it may not be possible to remove a file when
1144e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * it is open and in use by this Java virtual machine or other programs.
1145e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1146e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1147e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to delete
1148e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1149e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file was deleted by this method; {@code
1150e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          false} if the file could not be deleted because it did not
1151e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          exist
1152e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1153e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  DirectoryNotEmptyException
1154e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file is a directory and could not otherwise be deleted
1155e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          because the directory is not empty <i>(optional specific
1156e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          exception)</i>
1157e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1158e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1159e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1160e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1161e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkDelete(String)} method
1162e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          is invoked to check delete access to the file.
1163e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1164e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean deleteIfExists(Path path) throws IOException {
1165e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).deleteIfExists(path);
1166e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1167e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1168e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- Copying and moving files --
1169e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1170e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1171e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Copy a file to a target file.
1172e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1173e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method copies a file to the target file with the {@code
1174e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * options} parameter specifying how the copy is performed. By default, the
1175e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * copy fails if the target file already exists or is a symbolic link,
1176e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * except if the source and target are the {@link #isSameFile same} file, in
1177e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * which case the method completes without copying the file. File attributes
1178e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are not required to be copied to the target file. If symbolic links are
1179e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * supported, and the file is a symbolic link, then the final target of the
1180e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * link is copied. If the file is a directory then it creates an empty
1181e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory in the target location (entries in the directory are not
1182e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * copied). This method can be used with the {@link #walkFileTree
1183e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * walkFileTree} method to copy a directory and all entries in the directory,
1184e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * or an entire <i>file-tree</i> where required.
1185e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1186e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} parameter may include any of the following:
1187e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1188e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <table border=1 cellpadding=5 summary="">
1189e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr> <th>Option</th> <th>Description</th> </tr>
1190e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
1191e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link StandardCopyOption#REPLACE_EXISTING REPLACE_EXISTING} </td>
1192e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> If the target file exists, then the target file is replaced if it
1193e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     is not a non-empty directory. If the target file exists and is a
1194e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     symbolic link, then the symbolic link itself, not the target of
1195e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     the link, is replaced. </td>
1196e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
1197e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
1198e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link StandardCopyOption#COPY_ATTRIBUTES COPY_ATTRIBUTES} </td>
1199e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> Attempts to copy the file attributes associated with this file to
1200e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     the target file. The exact file attributes that are copied is platform
1201e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     and file system dependent and therefore unspecified. Minimally, the
1202e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     {@link BasicFileAttributes#lastModifiedTime last-modified-time} is
1203e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     copied to the target file if supported by both the source and target
1204e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     file stores. Copying of file timestamps may result in precision
1205e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     loss. </td>
1206e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
1207e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
1208e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link LinkOption#NOFOLLOW_LINKS NOFOLLOW_LINKS} </td>
1209e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> Symbolic links are not followed. If the file is a symbolic link,
1210e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     then the symbolic link itself, not the target of the link, is copied.
1211e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     It is implementation specific if file attributes can be copied to the
1212e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     new link. In other words, the {@code COPY_ATTRIBUTES} option may be
1213e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     ignored when copying a symbolic link. </td>
1214e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
1215e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </table>
1216e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1217e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> An implementation of this interface may support additional
1218e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementation specific options.
1219e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1220e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Copying a file is not an atomic operation. If an {@link IOException}
1221e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is thrown, then it is possible that the target file is incomplete or some
1222e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of its file attributes have not been copied from the source file. When
1223e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the {@code REPLACE_EXISTING} option is specified and the target file
1224e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * exists, then the target file is replaced. The check for the existence of
1225e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the file and the creation of the new file may not be atomic with respect
1226e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * to other file system activities.
1227e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1228e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Example:</b>
1229e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Suppose we want to copy a file into a directory, giving it the same file
1230e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * name as the source file:
1231e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
1232e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path source = ...
1233e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path newdir = ...
1234e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Files.copy(source, newdir.resolve(source.getFileName());
1235e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
1236e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1237e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   source
1238e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to copy
1239e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   target
1240e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the target file (may be associated with a different
1241e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          provider to the source path)
1242e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
1243e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the copy should be done
1244e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1245e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path to the target file
1246e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1247e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1248e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the array contains a copy option that is not supported
1249e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
1250e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the target file exists but cannot be replaced because the
1251e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          {@code REPLACE_EXISTING} option is not specified <i>(optional
1252e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          specific exception)</i>
1253e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  DirectoryNotEmptyException
1254e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the {@code REPLACE_EXISTING} option is specified but the file
1255e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          cannot be replaced because it is a non-empty directory
1256e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          <i>(optional specific exception)</i>
1257e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1258e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1259e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1260e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1261e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1262e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the source file, the
1263e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          {@link SecurityManager#checkWrite(String) checkWrite} is invoked
1264e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to check write access to the target file. If a symbolic link is
1265e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          copied the security manager is invoked to check {@link
1266e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          LinkPermission}{@code ("symbolic")}.
1267e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1268e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path copy(Path source, Path target, CopyOption... options)
1269e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
1270e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
1271e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        FileSystemProvider provider = provider(source);
1272e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (provider(target) == provider) {
1273e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // same provider
1274e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            provider.copy(source, target, options);
1275e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } else {
1276e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // different providers
1277e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            CopyMoveHelper.copyToForeignTarget(source, target, options);
1278e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
1279e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return target;
1280e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1281e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1282e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1283e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Move or rename a file to a target file.
1284e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1285e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> By default, this method attempts to move the file to the target
1286e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file, failing if the target file exists except if the source and
1287e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * target are the {@link #isSameFile same} file, in which case this method
1288e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * has no effect. If the file is a symbolic link then the symbolic link
1289e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * itself, not the target of the link, is moved. This method may be
1290e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * invoked to move an empty directory. In some implementations a directory
1291e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * has entries for special files or links that are created when the
1292e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory is created. In such implementations a directory is considered
1293e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * empty when only the special entries exist. When invoked to move a
1294e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory that is not empty then the directory is moved if it does not
1295e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * require moving the entries in the directory.  For example, renaming a
1296e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory on the same {@link FileStore} will usually not require moving
1297e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the entries in the directory. When moving a directory requires that its
1298e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * entries be moved then this method fails (by throwing an {@code
1299e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * IOException}). To move a <i>file tree</i> may involve copying rather
1300e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * than moving directories and this can be done using the {@link
1301e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * #copy copy} method in conjunction with the {@link
1302e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * #walkFileTree Files.walkFileTree} utility method.
1303e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1304e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} parameter may include any of the following:
1305e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1306e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <table border=1 cellpadding=5 summary="">
1307e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr> <th>Option</th> <th>Description</th> </tr>
1308e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
1309e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link StandardCopyOption#REPLACE_EXISTING REPLACE_EXISTING} </td>
1310e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> If the target file exists, then the target file is replaced if it
1311e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     is not a non-empty directory. If the target file exists and is a
1312e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     symbolic link, then the symbolic link itself, not the target of
1313e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     the link, is replaced. </td>
1314e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
1315e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
1316e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@link StandardCopyOption#ATOMIC_MOVE ATOMIC_MOVE} </td>
1317e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> The move is performed as an atomic file system operation and all
1318e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     other options are ignored. If the target file exists then it is
1319e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     implementation specific if the existing file is replaced or this method
1320e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     fails by throwing an {@link IOException}. If the move cannot be
1321e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     performed as an atomic file system operation then {@link
1322e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     AtomicMoveNotSupportedException} is thrown. This can arise, for
1323e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     example, when the target location is on a different {@code FileStore}
1324e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     and would require that the file be copied, or target location is
1325e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     associated with a different provider to this object. </td>
1326e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </table>
1327e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1328e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> An implementation of this interface may support additional
1329e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementation specific options.
1330e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1331e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Moving a file will copy the {@link
1332e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * BasicFileAttributes#lastModifiedTime last-modified-time} to the target
1333e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file if supported by both source and target file stores. Copying of file
1334e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * timestamps may result in precision loss. An implementation may also
1335e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attempt to copy other file attributes but is not required to fail if the
1336e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file attributes cannot be copied. When the move is performed as
1337e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * a non-atomic operation, and an {@code IOException} is thrown, then the
1338e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * state of the files is not defined. The original file and the target file
1339e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * may both exist, the target file may be incomplete or some of its file
1340e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attributes may not been copied from the original file.
1341e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1342e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Examples:</b>
1343e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Suppose we want to rename a file to "newname", keeping the file in the
1344e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * same directory:
1345e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
1346e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path source = ...
1347e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Files.move(source, source.resolveSibling("newname"));
1348e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
1349e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Alternatively, suppose we want to move a file to new directory, keeping
1350e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the same file name, and replacing any existing file of that name in the
1351e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory:
1352e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
1353e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path source = ...
1354e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path newdir = ...
1355e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Files.move(source, newdir.resolve(source.getFileName()), REPLACE_EXISTING);
1356e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
1357e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1358e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   source
1359e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to move
1360e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   target
1361e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the target file (may be associated with a different
1362e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          provider to the source path)
1363e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
1364e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the move should be done
1365e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1366e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path to the target file
1367e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1368e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1369e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the array contains a copy option that is not supported
1370e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
1371e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the target file exists but cannot be replaced because the
1372e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          {@code REPLACE_EXISTING} option is not specified <i>(optional
1373e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          specific exception)</i>
1374e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  DirectoryNotEmptyException
1375e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the {@code REPLACE_EXISTING} option is specified but the file
1376e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          cannot be replaced because it is a non-empty directory
1377e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          <i>(optional specific exception)</i>
1378e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  AtomicMoveNotSupportedException
1379e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the options array contains the {@code ATOMIC_MOVE} option but
1380e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the file cannot be moved as an atomic file system operation.
1381e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1382e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1383e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1384e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1385e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
1386e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to both the source and
1387e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          target file.
1388e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1389e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path move(Path source, Path target, CopyOption... options)
1390e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
1391e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
1392e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        FileSystemProvider provider = provider(source);
1393e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (provider(target) == provider) {
1394e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // same provider
1395e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            provider.move(source, target, options);
1396e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } else {
1397e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // different providers
1398e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            CopyMoveHelper.moveToForeignTarget(source, target, options);
1399e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
1400e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return target;
1401e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1402e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1403e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- Miscellenous --
1404e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1405e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1406e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Reads the target of a symbolic link <i>(optional operation)</i>.
1407e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1408e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If the file system supports <a href="package-summary.html#links">symbolic
1409e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * links</a> then this method is used to read the target of the link, failing
1410e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * if the file is not a symbolic link. The target of the link need not exist.
1411e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The returned {@code Path} object will be associated with the same file
1412e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * system as {@code link}.
1413e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1414e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   link
1415e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the symbolic link
1416e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1417e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a {@code Path} object representing the target of the link
1418e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1419e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1420e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the implementation does not support symbolic links
1421e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  NotLinkException
1422e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the target could otherwise not be read because the file
1423e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          is not a symbolic link <i>(optional specific exception)</i>
1424e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1425e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1426e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1427e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager
1428e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          is installed, it checks that {@code FilePermission} has been
1429e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          granted with the "{@code readlink}" action to read the link.
1430e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1431e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path readSymbolicLink(Path link) throws IOException {
1432e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(link).readSymbolicLink(link);
1433e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1434e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1435e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1436e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Returns the {@link FileStore} representing the file store where a file
1437e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is located.
1438e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1439e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Once a reference to the {@code FileStore} is obtained it is
1440e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementation specific if operations on the returned {@code FileStore},
1441e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * or {@link FileStoreAttributeView} objects obtained from it, continue
1442e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * to depend on the existence of the file. In particular the behavior is not
1443e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * defined for the case that the file is deleted or moved to a different
1444e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file store.
1445e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1446e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1447e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
1448e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1449e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the file store where the file is stored
1450e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1451e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1452e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1453e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1454e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1455e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1456e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file, and in
1457e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          addition it checks {@link RuntimePermission}<tt>
1458e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          ("getFileStoreAttributes")</tt>
1459e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1460e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static FileStore getFileStore(Path path) throws IOException {
1461e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).getFileStore(path);
1462e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1463e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1464e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1465e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tests if two paths locate the same file.
1466e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1467e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If both {@code Path} objects are {@link Path#equals(Object) equal}
1468e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * then this method returns {@code true} without checking if the file exists.
1469e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * If the two {@code Path} objects are associated with different providers
1470e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * then this method returns {@code false}. Otherwise, this method checks if
1471e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * both {@code Path} objects locate the same file, and depending on the
1472e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementation, may require to open or access both files.
1473e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1474e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If the file system and files remain static, then this method implements
1475e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * an equivalence relation for non-null {@code Paths}.
1476e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <ul>
1477e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <li>It is <i>reflexive</i>: for {@code Path} {@code f},
1478e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     {@code isSameFile(f,f)} should return {@code true}.
1479e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <li>It is <i>symmetric</i>: for two {@code Paths} {@code f} and {@code g},
1480e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     {@code isSameFile(f,g)} will equal {@code isSameFile(g,f)}.
1481e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <li>It is <i>transitive</i>: for three {@code Paths}
1482e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     {@code f}, {@code g}, and {@code h}, if {@code isSameFile(f,g)} returns
1483e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     {@code true} and {@code isSameFile(g,h)} returns {@code true}, then
1484e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     {@code isSameFile(f,h)} will return return {@code true}.
1485e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </ul>
1486e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1487e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1488e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          one path to the file
1489e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path2
1490e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the other path
1491e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1492e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if, and only if, the two paths locate the same file
1493e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1494e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1495e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1496e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1497e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1498e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1499e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to both files.
1500e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1501e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see java.nio.file.attribute.BasicFileAttributes#fileKey
1502e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1503e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean isSameFile(Path path, Path path2) throws IOException {
1504e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).isSameFile(path, path2);
1505e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1506e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1507e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1508e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tells whether or not a file is considered <em>hidden</em>. The exact
1509e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * definition of hidden is platform or provider dependent. On UNIX for
1510e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * example a file is considered to be hidden if its name begins with a
1511e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * period character ('.'). On Windows a file is considered hidden if it
1512e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * isn't a directory and the DOS {@link DosFileAttributes#isHidden hidden}
1513e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attribute is set.
1514e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1515e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Depending on the implementation this method may require to access
1516e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the file system to determine if the file is considered hidden.
1517e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1518e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1519e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to test
1520e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1521e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file is considered hidden
1522e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1523e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1524e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1525e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1526e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1527e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
1528e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
1529e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1530e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean isHidden(Path path) throws IOException {
1531e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).isHidden(path);
1532e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1533e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1534e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // lazy loading of default and installed file type detectors
1535e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static class FileTypeDetectors{
1536e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        static final FileTypeDetector defaultFileTypeDetector =
1537e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            createDefaultFileTypeDetector();
1538e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        static final List<FileTypeDetector> installeDetectors =
1539e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            loadInstalledDetectors();
1540e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1541e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // creates the default file type detector
1542e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        private static FileTypeDetector createDefaultFileTypeDetector() {
1543e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return AccessController
1544e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                .doPrivileged(new PrivilegedAction<FileTypeDetector>() {
1545e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    @Override public FileTypeDetector run() {
1546e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        return sun.nio.fs.DefaultFileTypeDetector.create();
1547e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                }});
1548e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
1549e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1550e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // loads all installed file type detectors
1551e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        private static List<FileTypeDetector> loadInstalledDetectors() {
1552e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return AccessController
1553e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                .doPrivileged(new PrivilegedAction<List<FileTypeDetector>>() {
1554e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    @Override public List<FileTypeDetector> run() {
1555e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        List<FileTypeDetector> list = new ArrayList<>();
1556e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        ServiceLoader<FileTypeDetector> loader = ServiceLoader
1557e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            .load(FileTypeDetector.class, ClassLoader.getSystemClassLoader());
1558e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        for (FileTypeDetector detector: loader) {
1559e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            list.add(detector);
1560e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        }
1561e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        return list;
1562e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                }});
1563e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
1564e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1565e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1566e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1567e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Probes the content type of a file.
1568e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1569e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method uses the installed {@link FileTypeDetector} implementations
1570e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * to probe the given file to determine its content type. Each file type
1571e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * detector's {@link FileTypeDetector#probeContentType probeContentType} is
1572e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * invoked, in turn, to probe the file type. If the file is recognized then
1573e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the content type is returned. If the file is not recognized by any of the
1574e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * installed file type detectors then a system-default file type detector is
1575e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * invoked to guess the content type.
1576e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1577e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> A given invocation of the Java virtual machine maintains a system-wide
1578e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * list of file type detectors. Installed file type detectors are loaded
1579e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * using the service-provider loading facility defined by the {@link ServiceLoader}
1580e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * class. Installed file type detectors are loaded using the system class
1581e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * loader. If the system class loader cannot be found then the extension class
1582e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * loader is used; If the extension class loader cannot be found then the
1583e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * bootstrap class loader is used. File type detectors are typically installed
1584e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * by placing them in a JAR file on the application class path or in the
1585e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * extension directory, the JAR file contains a provider-configuration file
1586e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * named {@code java.nio.file.spi.FileTypeDetector} in the resource directory
1587e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@code META-INF/services}, and the file lists one or more fully-qualified
1588e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * names of concrete subclass of {@code FileTypeDetector } that have a zero
1589e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * argument constructor. If the process of locating or instantiating the
1590e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * installed file type detectors fails then an unspecified error is thrown.
1591e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The ordering that installed providers are located is implementation
1592e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * specific.
1593e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1594e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The return value of this method is the string form of the value of a
1595e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Multipurpose Internet Mail Extension (MIME) content type as
1596e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * defined by <a href="http://www.ietf.org/rfc/rfc2045.txt"><i>RFC&nbsp;2045:
1597e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet
1598e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Message Bodies</i></a>. The string is guaranteed to be parsable according
1599e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * to the grammar in the RFC.
1600e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1601e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1602e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to probe
1603e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1604e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  The content type of the file, or {@code null} if the content
1605e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          type cannot be determined
1606e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1607e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1608e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1609e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1610e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          If a security manager is installed and it denies an unspecified
1611e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          permission required by a file type detector implementation.
1612e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1613e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static String probeContentType(Path path)
1614e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
1615e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
1616e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // try installed file type detectors
1617e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        for (FileTypeDetector detector: FileTypeDetectors.installeDetectors) {
1618e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            String result = detector.probeContentType(path);
1619e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (result != null)
1620e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                return result;
1621e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
1622e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1623e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // fallback to default
1624e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return FileTypeDetectors.defaultFileTypeDetector.probeContentType(path);
1625e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1626e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1627e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- File Attributes --
1628e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1629e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1630e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Returns a file attribute view of a given type.
1631e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1632e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> A file attribute view provides a read-only or updatable view of a
1633e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * set of file attributes. This method is intended to be used where the file
1634e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attribute view defines type-safe methods to read or update the file
1635e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attributes. The {@code type} parameter is the type of the attribute view
1636e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * required and the method returns an instance of that type if supported.
1637e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The {@link BasicFileAttributeView} type supports access to the basic
1638e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attributes of a file. Invoking this method to select a file attribute
1639e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * view of that type will always return an instance of that class.
1640e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1641e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} array may be used to indicate how symbolic links
1642e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled by the resulting file attribute view for the case that the
1643e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file is a symbolic link. By default, symbolic links are followed. If the
1644e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * option {@link LinkOption#NOFOLLOW_LINKS NOFOLLOW_LINKS} is present then
1645e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are not followed. This option is ignored by implementations
1646e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that do not support symbolic links.
1647e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1648e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Example:</b>
1649e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Suppose we want read or set a file's ACL, if supported:
1650e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
1651e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path path = ...
1652e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
1653e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     if (view != null) {
1654e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         List&lt;AclEntry&gt; acl = view.getAcl();
1655e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         :
1656e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     }
1657e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
1658e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1659e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   <V>
1660e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          The {@code FileAttributeView} type
1661e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1662e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
1663e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   type
1664e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the {@code Class} object corresponding to the file attribute view
1665e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
1666e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
1667e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1668e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a file attribute view of the specified type, or {@code null} if
1669e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the attribute view type is not available
1670e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1671e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static <V extends FileAttributeView> V getFileAttributeView(Path path,
1672e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                                       Class<V> type,
1673e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                                       LinkOption... options)
1674e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
1675e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).getFileAttributeView(path, type, options);
1676e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1677e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1678e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1679e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Reads a file's attributes as a bulk operation.
1680e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1681e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code type} parameter is the type of the attributes required
1682e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * and this method returns an instance of that type if supported. All
1683e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementations support a basic set of file attributes and so invoking
1684e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * this method with a  {@code type} parameter of {@code
1685e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * BasicFileAttributes.class} will not throw {@code
1686e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * UnsupportedOperationException}.
1687e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1688e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} array may be used to indicate how symbolic links
1689e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
1690e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed and the file attribute of the final target
1691e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
1692e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
1693e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1694e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> It is implementation specific if all file attributes are read as an
1695e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * atomic operation with respect to other file system operations.
1696e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1697e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Example:</b>
1698e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Suppose we want to read a file's attributes in bulk:
1699e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
1700e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    Path path = ...
1701e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
1702e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
1703e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Alternatively, suppose we want to read file's POSIX attributes without
1704e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * following symbolic links:
1705e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
1706e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    PosixFileAttributes attrs = Files.readAttributes(path, PosixFileAttributes.class, NOFOLLOW_LINKS);
1707e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
1708e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1709e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   <A>
1710e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          The {@code BasicFileAttributes} type
1711e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1712e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
1713e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   type
1714e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the {@code Class} of the file attributes required
1715e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to read
1716e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
1717e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
1718e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1719e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the file attributes
1720e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1721e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1722e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an attributes of the given type are not supported
1723e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1724e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1725e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1726e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, a security manager is
1727e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
1728e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file. If this
1729e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to read security sensitive attributes then the
1730e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          security manager may be invoke to check for additional permissions.
1731e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1732e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static <A extends BasicFileAttributes> A readAttributes(Path path,
1733e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                                   Class<A> type,
1734e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                                   LinkOption... options)
1735e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
1736e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
1737e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).readAttributes(path, type, options);
1738e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1739e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1740e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1741e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Sets the value of a file attribute.
1742e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1743e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attribute} parameter identifies the attribute to be set
1744e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * and takes the form:
1745e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <blockquote>
1746e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * [<i>view-name</i><b>:</b>]<i>attribute-name</i>
1747e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </blockquote>
1748e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * where square brackets [...] delineate an optional component and the
1749e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * character {@code ':'} stands for itself.
1750e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1751e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link
1752e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileAttributeView} that identifies a set of file attributes. If not
1753e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * specified then it defaults to {@code "basic"}, the name of the file
1754e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attribute view that identifies the basic set of file attributes common to
1755e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * many file systems. <i>attribute-name</i> is the name of the attribute
1756e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * within the set.
1757e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1758e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} array may be used to indicate how symbolic links
1759e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
1760e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed and the file attribute of the final target
1761e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link is set. If the option {@link LinkOption#NOFOLLOW_LINKS
1762e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
1763e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1764e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Example:</b>
1765e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Suppose we want to set the DOS "hidden" attribute:
1766e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
1767e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    Path path = ...
1768e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    Files.setAttribute(path, "dos:hidden", true);
1769e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
1770e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1771e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1772e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
1773e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attribute
1774e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the attribute to set
1775e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   value
1776e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the attribute value
1777e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
1778e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
1779e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1780e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the {@code path} parameter
1781e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1782e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1783e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the attribute view is not available
1784e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
1785e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the attribute name is not specified, or is not recognized, or
1786e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the attribute value is of the correct type but has an
1787e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          inappropriate value
1788e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  ClassCastException
1789e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the attribute value is not of the expected type or is a
1790e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          collection containing elements that are not of the expected
1791e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          type
1792e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1793e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1794e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1795e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1796e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, its {@link SecurityManager#checkWrite(String) checkWrite}
1797e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies write access to the file. If this method is invoked
1798e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to set security sensitive attributes then the security manager
1799e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          may be invoked to check for additional permissions.
1800e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1801e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path setAttribute(Path path, String attribute, Object value,
1802e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                    LinkOption... options)
1803e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
1804e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
1805e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        provider(path).setAttribute(path, attribute, value, options);
1806e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return path;
1807e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1808e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1809e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1810e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Reads the value of a file attribute.
1811e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1812e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attribute} parameter identifies the attribute to be read
1813e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * and takes the form:
1814e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <blockquote>
1815e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * [<i>view-name</i><b>:</b>]<i>attribute-name</i>
1816e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </blockquote>
1817e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * where square brackets [...] delineate an optional component and the
1818e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * character {@code ':'} stands for itself.
1819e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1820e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link
1821e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileAttributeView} that identifies a set of file attributes. If not
1822e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * specified then it defaults to {@code "basic"}, the name of the file
1823e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attribute view that identifies the basic set of file attributes common to
1824e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * many file systems. <i>attribute-name</i> is the name of the attribute.
1825e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1826e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} array may be used to indicate how symbolic links
1827e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
1828e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed and the file attribute of the final target
1829e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
1830e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
1831e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1832e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Example:</b>
1833e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Suppose we require the user ID of the file owner on a system that
1834e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * supports a "{@code unix}" view:
1835e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
1836e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    Path path = ...
1837e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    int uid = (Integer)Files.getAttribute(path, "unix:uid");
1838e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
1839e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1840e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1841e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
1842e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attribute
1843e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the attribute to read
1844e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
1845e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
1846e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1847e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the attribute value
1848e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1849e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1850e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the attribute view is not available
1851e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
1852e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the attribute name is not specified or is not recognized
1853e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1854e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1855e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1856e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1857e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
1858e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies read access to the file. If this method is invoked
1859e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to read security sensitive attributes then the security manager
1860e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          may be invoked to check for additional permissions.
1861e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1862e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Object getAttribute(Path path, String attribute,
1863e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                      LinkOption... options)
1864e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
1865e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
1866e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // only one attribute should be read
1867e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (attribute.indexOf('*') >= 0 || attribute.indexOf(',') >= 0)
1868e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw new IllegalArgumentException(attribute);
1869e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Map<String,Object> map = readAttributes(path, attribute, options);
1870e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        assert map.size() == 1;
1871e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        String name;
1872e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        int pos = attribute.indexOf(':');
1873e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (pos == -1) {
1874e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            name = attribute;
1875e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } else {
1876e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            name = (pos == attribute.length()) ? "" : attribute.substring(pos+1);
1877e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
1878e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return map.get(name);
1879e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1880e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1881e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1882e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Reads a set of file attributes as a bulk operation.
1883e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1884e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code attributes} parameter identifies the attributes to be read
1885e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * and takes the form:
1886e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <blockquote>
1887e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * [<i>view-name</i><b>:</b>]<i>attribute-list</i>
1888e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </blockquote>
1889e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * where square brackets [...] delineate an optional component and the
1890e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * character {@code ':'} stands for itself.
1891e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1892e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link
1893e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileAttributeView} that identifies a set of file attributes. If not
1894e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * specified then it defaults to {@code "basic"}, the name of the file
1895e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attribute view that identifies the basic set of file attributes common to
1896e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * many file systems.
1897e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1898e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The <i>attribute-list</i> component is a comma separated list of
1899e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * zero or more names of attributes to read. If the list contains the value
1900e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@code "*"} then all attributes are read. Attributes that are not supported
1901e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are ignored and will not be present in the returned map. It is
1902e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * implementation specific if all attributes are read as an atomic operation
1903e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * with respect to other file system operations.
1904e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1905e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The following examples demonstrate possible values for the {@code
1906e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attributes} parameter:
1907e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1908e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <blockquote>
1909e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <table border="0" summary="Possible values">
1910e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
1911e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@code "*"} </td>
1912e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> Read all {@link BasicFileAttributes basic-file-attributes}. </td>
1913e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
1914e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
1915e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@code "size,lastModifiedTime,lastAccessTime"} </td>
1916e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> Reads the file size, last modified time, and last access time
1917e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     attributes. </td>
1918e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
1919e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
1920e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@code "posix:*"} </td>
1921e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> Read all {@link PosixFileAttributes POSIX-file-attributes}. </td>
1922e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
1923e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <tr>
1924e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> {@code "posix:permissions,owner,size"} </td>
1925e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <td> Reads the POSX file permissions, owner, and file size. </td>
1926e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </tr>
1927e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </table>
1928e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </blockquote>
1929e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1930e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} array may be used to indicate how symbolic links
1931e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
1932e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed and the file attribute of the final target
1933e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
1934e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
1935e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1936e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1937e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
1938e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   attributes
1939e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the attributes to read
1940e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
1941e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
1942e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1943e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a map of the attributes returned; The map's keys are the
1944e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          attribute names, its values are the attribute values
1945e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1946e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1947e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the attribute view is not available
1948e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
1949e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if no attributes are specified or an unrecognized attributes is
1950e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          specified
1951e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1952e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1953e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1954e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
1955e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
1956e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies read access to the file. If this method is invoked
1957e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to read security sensitive attributes then the security manager
1958e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          may be invoke to check for additional permissions.
1959e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
1960e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Map<String,Object> readAttributes(Path path, String attributes,
1961e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                    LinkOption... options)
1962e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
1963e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
1964e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return provider(path).readAttributes(path, attributes, options);
1965e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
1966e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
1967e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
1968e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Returns a file's POSIX file permissions.
1969e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1970e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code path} parameter is associated with a {@code FileSystem}
1971e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that supports the {@link PosixFileAttributeView}. This attribute view
1972e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * provides access to file attributes commonly associated with files on file
1973e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * systems used by operating systems that implement the Portable Operating
1974e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * System Interface (POSIX) family of standards.
1975e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1976e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} array may be used to indicate how symbolic links
1977e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
1978e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed and the file attribute of the final target
1979e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
1980e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
1981e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1982e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
1983e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
1984e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
1985e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
1986e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1987e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the file permissions
1988e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
1989e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
1990e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the associated file system does not support the {@code
1991e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          PosixFileAttributeView}
1992e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
1993e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
1994e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
1995e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, a security manager is
1996e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, and it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
1997e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          or its {@link SecurityManager#checkRead(String) checkRead} method
1998e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          denies read access to the file.
1999e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2000e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Set<PosixFilePermission> getPosixFilePermissions(Path path,
2001e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                                   LinkOption... options)
2002e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2003e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2004e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return readAttributes(path, PosixFileAttributes.class, options).permissions();
2005e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2006e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2007e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2008e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Sets a file's POSIX permissions.
2009e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2010e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code path} parameter is associated with a {@code FileSystem}
2011e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that supports the {@link PosixFileAttributeView}. This attribute view
2012e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * provides access to file attributes commonly associated with files on file
2013e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * systems used by operating systems that implement the Portable Operating
2014e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * System Interface (POSIX) family of standards.
2015e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2016e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2017e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          The path to the file
2018e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   perms
2019e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          The new set of permissions
2020e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2021e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  The path
2022e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2023e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
2024e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the associated file system does not support the {@code
2025e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          PosixFileAttributeView}
2026e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  ClassCastException
2027e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the sets contains elements that are not of type {@code
2028e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          PosixFilePermission}
2029e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2030e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
2031e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2032e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2033e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
2034e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          or its {@link SecurityManager#checkWrite(String) checkWrite}
2035e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies write access to the file.
2036e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2037e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path setPosixFilePermissions(Path path,
2038e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                               Set<PosixFilePermission> perms)
2039e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2040e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2041e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        PosixFileAttributeView view =
2042e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            getFileAttributeView(path, PosixFileAttributeView.class);
2043e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (view == null)
2044e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw new UnsupportedOperationException();
2045e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        view.setPermissions(perms);
2046e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return path;
2047e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2048e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2049e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2050e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Returns the owner of a file.
2051e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2052e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code path} parameter is associated with a file system that
2053e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * supports {@link FileOwnerAttributeView}. This file attribute view provides
2054e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * access to a file attribute that is the owner of the file.
2055e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2056e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2057e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          The path to the file
2058e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2059e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
2060e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2061e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  A user principal representing the owner of the file
2062e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2063e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
2064e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the associated file system does not support the {@code
2065e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          FileOwnerAttributeView}
2066e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2067e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
2068e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2069e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2070e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
2071e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          or its {@link SecurityManager#checkRead(String) checkRead} method
2072e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          denies read access to the file.
2073e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2074e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException {
2075e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        FileOwnerAttributeView view =
2076e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            getFileAttributeView(path, FileOwnerAttributeView.class, options);
2077e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (view == null)
2078e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw new UnsupportedOperationException();
2079e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return view.getOwner();
2080e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2081e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2082e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2083e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Updates the file owner.
2084e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2085e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code path} parameter is associated with a file system that
2086e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * supports {@link FileOwnerAttributeView}. This file attribute view provides
2087e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * access to a file attribute that is the owner of the file.
2088e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2089e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Example:</b>
2090e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Suppose we want to make "joe" the owner of a file:
2091e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
2092e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path path = ...
2093e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     UserPrincipalLookupService lookupService =
2094e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         provider(path).getUserPrincipalLookupService();
2095e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
2096e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Files.setOwner(path, joe);
2097e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
2098e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2099e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2100e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          The path to the file
2101e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   owner
2102e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          The new file owner
2103e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2104e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  The path
2105e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2106e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
2107e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the associated file system does not support the {@code
2108e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          FileOwnerAttributeView}
2109e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2110e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
2111e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2112e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2113e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt>
2114e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          or its {@link SecurityManager#checkWrite(String) checkWrite}
2115e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies write access to the file.
2116e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2117e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see FileSystem#getUserPrincipalLookupService
2118e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see java.nio.file.attribute.UserPrincipalLookupService
2119e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2120e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path setOwner(Path path, UserPrincipal owner)
2121e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2122e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2123e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        FileOwnerAttributeView view =
2124e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            getFileAttributeView(path, FileOwnerAttributeView.class);
2125e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (view == null)
2126e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw new UnsupportedOperationException();
2127e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        view.setOwner(owner);
2128e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return path;
2129e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2130e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2131e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2132e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tests whether a file is a symbolic link.
2133e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2134e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Where it is required to distinguish an I/O exception from the case
2135e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that the file is not a symbolic link then the file attributes can be
2136e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * read with the {@link #readAttributes(Path,Class,LinkOption[])
2137e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * readAttributes} method and the file type tested with the {@link
2138e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * BasicFileAttributes#isSymbolicLink} method.
2139e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2140e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path  The path to the file
2141e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2142e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file is a symbolic link; {@code false} if
2143e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the file does not exist, is not a symbolic link, or it cannot
2144e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          be determined if the file is a symbolic link or not.
2145e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2146e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2147e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2148e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
2149e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies read access to the file.
2150e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2151e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean isSymbolicLink(Path path) {
2152e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
2153e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return readAttributes(path,
2154e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                  BasicFileAttributes.class,
2155e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                  LinkOption.NOFOLLOW_LINKS).isSymbolicLink();
2156e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (IOException ioe) {
2157e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return false;
2158e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2159e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2160e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2161e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2162e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tests whether a file is a directory.
2163e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2164e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} array may be used to indicate how symbolic links
2165e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
2166e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed and the file attribute of the final target
2167e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
2168e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
2169e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2170e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Where it is required to distinguish an I/O exception from the case
2171e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that the file is not a directory then the file attributes can be
2172e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * read with the {@link #readAttributes(Path,Class,LinkOption[])
2173e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * readAttributes} method and the file type tested with the {@link
2174e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * BasicFileAttributes#isDirectory} method.
2175e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2176e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2177e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to test
2178e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2179e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
2180e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2181e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file is a directory; {@code false} if
2182e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the file does not exist, is not a directory, or it cannot
2183e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          be determined if the file is a directory or not.
2184e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2185e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2186e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2187e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
2188e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies read access to the file.
2189e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2190e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean isDirectory(Path path, LinkOption... options) {
2191e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
2192e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return readAttributes(path, BasicFileAttributes.class, options).isDirectory();
2193e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (IOException ioe) {
2194e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return false;
2195e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2196e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2197e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2198e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2199e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tests whether a file is a regular file with opaque content.
2200e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2201e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} array may be used to indicate how symbolic links
2202e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
2203e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed and the file attribute of the final target
2204e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
2205e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
2206e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2207e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Where it is required to distinguish an I/O exception from the case
2208e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that the file is not a regular file then the file attributes can be
2209e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * read with the {@link #readAttributes(Path,Class,LinkOption[])
2210e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * readAttributes} method and the file type tested with the {@link
2211e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * BasicFileAttributes#isRegularFile} method.
2212e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2213e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2214e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
2215e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2216e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
2217e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2218e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file is a regular file; {@code false} if
2219e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the file does not exist, is not a regular file, or it
2220e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          cannot be determined if the file is a regular file or not.
2221e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2222e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2223e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2224e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
2225e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies read access to the file.
2226e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2227e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean isRegularFile(Path path, LinkOption... options) {
2228e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
2229e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return readAttributes(path, BasicFileAttributes.class, options).isRegularFile();
2230e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (IOException ioe) {
2231e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return false;
2232e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2233e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2234e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2235e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2236e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Returns a file's last modified time.
2237e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2238e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} array may be used to indicate how symbolic links
2239e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
2240e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed and the file attribute of the final target
2241e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
2242e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
2243e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2244e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2245e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
2246e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2247e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
2248e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2249e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a {@code FileTime} representing the time the file was last
2250e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          modified, or an implementation specific default when a time
2251e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          stamp to indicate the time of last modification is not supported
2252e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          by the file system
2253e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2254e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2255e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
2256e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2257e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2258e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
2259e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies read access to the file.
2260e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2261e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see BasicFileAttributes#lastModifiedTime
2262e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2263e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static FileTime getLastModifiedTime(Path path, LinkOption... options)
2264e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2265e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2266e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return readAttributes(path, BasicFileAttributes.class, options).lastModifiedTime();
2267e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2268e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2269e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2270e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Updates a file's last modified time attribute. The file time is converted
2271e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * to the epoch and precision supported by the file system. Converting from
2272e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * finer to coarser granularities result in precision loss. The behavior of
2273e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * this method when attempting to set the last modified time when it is not
2274e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * supported by the file system or is outside the range supported by the
2275e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * underlying file store is not defined. It may or not fail by throwing an
2276e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@code IOException}.
2277e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2278e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage Example:</b>
2279e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Suppose we want to set the last modified time to the current time:
2280e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
2281e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    Path path = ...
2282e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    FileTime now = FileTime.fromMillis(System.currentTimeMillis());
2283e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *    Files.setLastModifiedTime(path, now);
2284e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
2285e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2286e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2287e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
2288e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   time
2289e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the new last modified time
2290e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2291e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path
2292e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2293e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2294e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
2295e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2296e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, the security manager's {@link
2297e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkWrite(String) checkWrite} method is invoked
2298e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to check write access to file
2299e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2300e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see BasicFileAttributeView#setTimes
2301e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2302e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path setLastModifiedTime(Path path, FileTime time)
2303e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2304e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2305e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        getFileAttributeView(path, BasicFileAttributeView.class)
2306e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            .setTimes(time, null, null);
2307e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return path;
2308e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2309e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2310e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2311e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Returns the size of a file (in bytes). The size may differ from the
2312e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * actual size on the file system due to compression, support for sparse
2313e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * files, or other reasons. The size of files that are not {@link
2314e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * #isRegularFile regular} files is implementation specific and
2315e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * therefore unspecified.
2316e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2317e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2318e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
2319e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2320e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the file size, in bytes
2321e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2322e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2323e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs
2324e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2325e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2326e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
2327e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method denies read access to the file.
2328e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2329e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see BasicFileAttributes#size
2330e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2331e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static long size(Path path) throws IOException {
2332e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return readAttributes(path, BasicFileAttributes.class).size();
2333e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2334e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2335e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- Accessibility --
2336e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2337e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2338e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Returns {@code false} if NOFOLLOW_LINKS is present.
2339e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2340e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static boolean followLinks(LinkOption... options) {
2341e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        boolean followLinks = true;
2342e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        for (LinkOption opt: options) {
2343e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (opt == LinkOption.NOFOLLOW_LINKS) {
2344e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                followLinks = false;
2345e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                continue;
2346e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
2347e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (opt == null)
2348e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                throw new NullPointerException();
2349e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw new AssertionError("Should not get here");
2350e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2351e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return followLinks;
2352e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2353e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2354e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2355e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tests whether a file exists.
2356e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2357e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} parameter may be used to indicate how symbolic links
2358e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
2359e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed. If the option {@link LinkOption#NOFOLLOW_LINKS
2360e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
2361e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2362e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Note that the result of this method is immediately outdated. If this
2363e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method indicates the file exists then there is no guarantee that a
2364e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * subsequence access will succeed. Care should be taken when using this
2365e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method in security sensitive applications.
2366e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2367e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2368e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to test
2369e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2370e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
2371e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * .
2372e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file exists; {@code false} if the file does
2373e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          not exist or its existence cannot be determined.
2374e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2375e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2376e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, the {@link
2377e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkRead(String)} is invoked to check
2378e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          read access to the file.
2379e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2380e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see #notExists
2381e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2382e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean exists(Path path, LinkOption... options) {
2383e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
2384e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (followLinks(options)) {
2385e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                provider(path).checkAccess(path);
2386e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } else {
2387e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                // attempt to read attributes without following links
2388e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                readAttributes(path, BasicFileAttributes.class,
2389e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                               LinkOption.NOFOLLOW_LINKS);
2390e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
2391e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // file exists
2392e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return true;
2393e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (IOException x) {
2394e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // does not exist or unable to determine if file exists
2395e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return false;
2396e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2397e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2398e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2399e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2400e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2401e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tests whether the file located by this path does not exist. This method
2402e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is intended for cases where it is required to take action when it can be
2403e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * confirmed that a file does not exist.
2404e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2405e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} parameter may be used to indicate how symbolic links
2406e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are handled for the case that the file is a symbolic link. By default,
2407e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic links are followed. If the option {@link LinkOption#NOFOLLOW_LINKS
2408e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
2409e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2410e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Note that this method is not the complement of the {@link #exists
2411e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * exists} method. Where it is not possible to determine if a file exists
2412e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * or not then both methods return {@code false}. As with the {@code exists}
2413e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method, the result of this method is immediately outdated. If this
2414e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method indicates the file does exist then there is no guarantee that a
2415e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * subsequence attempt to create the file will succeed. Care should be taken
2416e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * when using this method in security sensitive applications.
2417e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2418e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2419e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to test
2420e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2421e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options indicating how symbolic links are handled
2422e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2423e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file does not exist; {@code false} if the
2424e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          file exists or its existence cannot be determined
2425e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2426e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2427e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, the {@link
2428e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkRead(String)} is invoked to check
2429e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          read access to the file.
2430e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2431e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean notExists(Path path, LinkOption... options) {
2432e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
2433e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (followLinks(options)) {
2434e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                provider(path).checkAccess(path);
2435e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } else {
2436e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                // attempt to read attributes without following links
2437e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                readAttributes(path, BasicFileAttributes.class,
2438e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                               LinkOption.NOFOLLOW_LINKS);
2439e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
2440e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // file exists
2441e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return false;
2442e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (NoSuchFileException x) {
2443e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // file confirmed not to exist
2444e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return true;
2445e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (IOException x) {
2446e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return false;
2447e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2448e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2449e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2450e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2451e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Used by isReadbale, isWritable, isExecutable to test access to a file.
2452e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2453e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static boolean isAccessible(Path path, AccessMode... modes) {
2454e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
2455e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            provider(path).checkAccess(path, modes);
2456e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return true;
2457e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (IOException x) {
2458e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return false;
2459e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2460e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2461e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2462e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2463e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tests whether a file is readable. This method checks that a file exists
2464e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * and that this Java virtual machine has appropriate privileges that would
2465e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * allow it open the file for reading. Depending on the implementation, this
2466e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method may require to read file permissions, access control lists, or
2467e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * other file attributes in order to check the effective access to the file.
2468e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Consequently, this method may not be atomic with respect to other file
2469e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * system operations.
2470e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2471e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Note that the result of this method is immediately outdated, there is
2472e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * no guarantee that a subsequent attempt to open the file for reading will
2473e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * succeed (or even that it will access the same file). Care should be taken
2474e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * when using this method in security sensitive applications.
2475e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2476e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2477e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to check
2478e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2479e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file exists and is readable; {@code false}
2480e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file does not exist, read access would be denied because
2481e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the Java virtual machine has insufficient privileges, or access
2482e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          cannot be determined
2483e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2484e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2485e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2486e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
2487e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          is invoked to check read access to the file.
2488e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2489e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean isReadable(Path path) {
2490e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return isAccessible(path, AccessMode.READ);
2491e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2492e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2493e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2494e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tests whether a file is writable. This method checks that a file exists
2495e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * and that this Java virtual machine has appropriate privileges that would
2496e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * allow it open the file for writing. Depending on the implementation, this
2497e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method may require to read file permissions, access control lists, or
2498e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * other file attributes in order to check the effective access to the file.
2499e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Consequently, this method may not be atomic with respect to other file
2500e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * system operations.
2501e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2502e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Note that result of this method is immediately outdated, there is no
2503e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * guarantee that a subsequent attempt to open the file for writing will
2504e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * succeed (or even that it will access the same file). Care should be taken
2505e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * when using this method in security sensitive applications.
2506e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2507e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2508e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to check
2509e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2510e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file exists and is writable; {@code false}
2511e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file does not exist, write access would be denied because
2512e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the Java virtual machine has insufficient privileges, or access
2513e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          cannot be determined
2514e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2515e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2516e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2517e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
2518e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          is invoked to check write access to the file.
2519e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2520e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean isWritable(Path path) {
2521e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return isAccessible(path, AccessMode.WRITE);
2522e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2523e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2524e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2525e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Tests whether a file is executable. This method checks that a file exists
2526e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * and that this Java virtual machine has appropriate privileges to {@link
2527e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Runtime#exec execute} the file. The semantics may differ when checking
2528e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * access to a directory. For example, on UNIX systems, checking for
2529e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * execute access checks that the Java virtual machine has permission to
2530e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * search the directory in order to access file or subdirectories.
2531e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2532e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Depending on the implementation, this method may require to read file
2533e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * permissions, access control lists, or other file attributes in order to
2534e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * check the effective access to the file. Consequently, this method may not
2535e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * be atomic with respect to other file system operations.
2536e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2537e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Note that the result of this method is immediately outdated, there is
2538e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * no guarantee that a subsequent attempt to execute the file will succeed
2539e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * (or even that it will access the same file). Care should be taken when
2540e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * using this method in security sensitive applications.
2541e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2542e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2543e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file to check
2544e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2545e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  {@code true} if the file exists and is executable; {@code false}
2546e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file does not exist, execute access would be denied because
2547e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the Java virtual machine has insufficient privileges, or access
2548e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          cannot be determined
2549e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2550e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2551e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2552e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkExec(String)
2553e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          checkExec} is invoked to check execute access to the file.
2554e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2555e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static boolean isExecutable(Path path) {
2556e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return isAccessible(path, AccessMode.EXECUTE);
2557e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2558e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2559e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- Recursive operations --
2560e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2561e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2562e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Walks a file tree.
2563e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2564e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method walks a file tree rooted at a given starting file. The
2565e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file tree traversal is <em>depth-first</em> with the given {@link
2566e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileVisitor} invoked for each file encountered. File tree traversal
2567e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * completes when all accessible files in the tree have been visited, or a
2568e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * visit method returns a result of {@link FileVisitResult#TERMINATE
2569e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * TERMINATE}. Where a visit method terminates due an {@code IOException},
2570e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * an uncaught error, or runtime exception, then the traversal is terminated
2571e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * and the error or exception is propagated to the caller of this method.
2572e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2573e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> For each file encountered this method attempts to read its {@link
2574e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * java.nio.file.attribute.BasicFileAttributes}. If the file is not a
2575e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory then the {@link FileVisitor#visitFile visitFile} method is
2576e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * invoked with the file attributes. If the file attributes cannot be read,
2577e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * due to an I/O exception, then the {@link FileVisitor#visitFileFailed
2578e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * visitFileFailed} method is invoked with the I/O exception.
2579e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2580e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Where the file is a directory, and the directory could not be opened,
2581e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * then the {@code visitFileFailed} method is invoked with the I/O exception,
2582e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * after which, the file tree walk continues, by default, at the next
2583e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <em>sibling</em> of the directory.
2584e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2585e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Where the directory is opened successfully, then the entries in the
2586e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory, and their <em>descendants</em> are visited. When all entries
2587e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * have been visited, or an I/O error occurs during iteration of the
2588e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory, then the directory is closed and the visitor's {@link
2589e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileVisitor#postVisitDirectory postVisitDirectory} method is invoked.
2590e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The file tree walk then continues, by default, at the next <em>sibling</em>
2591e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the directory.
2592e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2593e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> By default, symbolic links are not automatically followed by this
2594e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method. If the {@code options} parameter contains the {@link
2595e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then symbolic links are
2596e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * followed. When following links, and the attributes of the target cannot
2597e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * be read, then this method attempts to get the {@code BasicFileAttributes}
2598e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link. If they can be read then the {@code visitFile} method is
2599e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * invoked with the attributes of the link (otherwise the {@code visitFileFailed}
2600e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method is invoked as specified above).
2601e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2602e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If the {@code options} parameter contains the {@link
2603e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then this method keeps
2604e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * track of directories visited so that cycles can be detected. A cycle
2605e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * arises when there is an entry in a directory that is an ancestor of the
2606e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory. Cycle detection is done by recording the {@link
2607e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * java.nio.file.attribute.BasicFileAttributes#fileKey file-key} of directories,
2608e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * or if file keys are not available, by invoking the {@link #isSameFile
2609e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * isSameFile} method to test if a directory is the same file as an
2610e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * ancestor. When a cycle is detected it is treated as an I/O error, and the
2611e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link FileVisitor#visitFileFailed visitFileFailed} method is invoked with
2612e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * an instance of {@link FileSystemLoopException}.
2613e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2614e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code maxDepth} parameter is the maximum number of levels of
2615e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directories to visit. A value of {@code 0} means that only the starting
2616e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file is visited, unless denied by the security manager. A value of
2617e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link Integer#MAX_VALUE MAX_VALUE} may be used to indicate that all
2618e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * levels should be visited. The {@code visitFile} method is invoked for all
2619e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * files, including directories, encountered at {@code maxDepth}, unless the
2620e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * basic file attributes cannot be read, in which case the {@code
2621e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * visitFileFailed} method is invoked.
2622e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2623e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If a visitor returns a result of {@code null} then {@code
2624e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * NullPointerException} is thrown.
2625e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2626e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> When a security manager is installed and it denies access to a file
2627e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * (or directory), then it is ignored and the visitor is not invoked for
2628e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that file (or directory).
2629e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2630e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   start
2631e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the starting file
2632e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2633e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options to configure the traversal
2634e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   maxDepth
2635e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the maximum number of directory levels to visit
2636e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   visitor
2637e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the file visitor to invoke for each file
2638e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2639e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the starting file
2640e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2641e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
2642e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the {@code maxDepth} parameter is negative
2643e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2644e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          If the security manager denies access to the starting file.
2645e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, the {@link
2646e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkRead(String) checkRead} method is invoked
2647e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to check read access to the directory.
2648e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2649e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error is thrown by a visitor method
2650e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2651e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path walkFileTree(Path start,
2652e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                    Set<FileVisitOption> options,
2653e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                    int maxDepth,
2654e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                    FileVisitor<? super Path> visitor)
2655e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2656e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2657e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        /**
2658e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera         * Create a FileTreeWalker to walk the file tree, invoking the visitor
2659e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera         * for each event.
2660e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera         */
2661e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try (FileTreeWalker walker = new FileTreeWalker(options, maxDepth)) {
2662e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            FileTreeWalker.Event ev = walker.walk(start);
2663e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            do {
2664e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                FileVisitResult result;
2665e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                switch (ev.type()) {
2666e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    case ENTRY :
2667e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        IOException ioe = ev.ioeException();
2668e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        if (ioe == null) {
2669e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            assert ev.attributes() != null;
2670e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            result = visitor.visitFile(ev.file(), ev.attributes());
2671e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        } else {
2672e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            result = visitor.visitFileFailed(ev.file(), ioe);
2673e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        }
2674e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        break;
2675e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2676e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    case START_DIRECTORY :
2677e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        result = visitor.preVisitDirectory(ev.file(), ev.attributes());
2678e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2679e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        // if SKIP_SIBLINGS and SKIP_SUBTREE is returned then
2680e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        // there shouldn't be any more events for the current
2681e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        // directory.
2682e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        if (result == FileVisitResult.SKIP_SUBTREE ||
2683e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            result == FileVisitResult.SKIP_SIBLINGS)
2684e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            walker.pop();
2685e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        break;
2686e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2687e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    case END_DIRECTORY :
2688e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        result = visitor.postVisitDirectory(ev.file(), ev.ioeException());
2689e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2690e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        // SKIP_SIBLINGS is a no-op for postVisitDirectory
2691e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        if (result == FileVisitResult.SKIP_SIBLINGS)
2692e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            result = FileVisitResult.CONTINUE;
2693e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        break;
2694e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2695e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    default :
2696e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        throw new AssertionError("Should not get here");
2697e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                }
2698e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2699e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                if (Objects.requireNonNull(result) != FileVisitResult.CONTINUE) {
2700e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    if (result == FileVisitResult.TERMINATE) {
2701e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        break;
2702e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    } else if (result == FileVisitResult.SKIP_SIBLINGS) {
2703e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        walker.skipRemainingSiblings();
2704e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    }
2705e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                }
2706e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                ev = walker.next();
2707e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } while (ev != null);
2708e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2709e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2710e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return start;
2711e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2712e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2713e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2714e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Walks a file tree.
2715e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2716e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method works as if invoking it were equivalent to evaluating the
2717e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * expression:
2718e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <blockquote><pre>
2719e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor)
2720e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre></blockquote>
2721e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * In other words, it does not follow symbolic links, and visits all levels
2722e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the file tree.
2723e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2724e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   start
2725e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the starting file
2726e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   visitor
2727e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the file visitor to invoke for each file
2728e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2729e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the starting file
2730e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2731e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2732e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          If the security manager denies access to the starting file.
2733e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, the {@link
2734e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkRead(String) checkRead} method is invoked
2735e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to check read access to the directory.
2736e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2737e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error is thrown by a visitor method
2738e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2739e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor)
2740e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2741e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2742e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return walkFileTree(start,
2743e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            EnumSet.noneOf(FileVisitOption.class),
2744e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            Integer.MAX_VALUE,
2745e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                            visitor);
2746e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2747e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2748e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2749e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- Utility methods for simple usages --
2750e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2751e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // buffer size used for reading and writing
2752e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static final int BUFFER_SIZE = 8192;
2753e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2754e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2755e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens a file for reading, returning a {@code BufferedReader} that may be
2756e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * used to read text from the file in an efficient manner. Bytes from the
2757e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file are decoded into characters using the specified charset. Reading
2758e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * commences at the beginning of the file.
2759e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2760e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code Reader} methods that read from the file throw {@code
2761e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * IOException} if a malformed or unmappable byte sequence is read.
2762e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2763e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2764e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
2765e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   cs
2766e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the charset to use for decoding
2767e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2768e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new buffered reader, with default buffer size, to read text
2769e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          from the file
2770e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2771e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2772e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs opening the file
2773e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2774e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2775e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
2776e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
2777e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2778e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see #readAllLines
2779e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2780e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static BufferedReader newBufferedReader(Path path, Charset cs)
2781e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2782e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2783e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        CharsetDecoder decoder = cs.newDecoder();
2784e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Reader reader = new InputStreamReader(newInputStream(path), decoder);
2785e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return new BufferedReader(reader);
2786e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2787e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2788e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2789e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens a file for reading, returning a {@code BufferedReader} to read text
2790e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * from the file in an efficient manner. Bytes from the file are decoded into
2791e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * characters using the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset
2792e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * charset}.
2793e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2794e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method works as if invoking it were equivalent to evaluating the
2795e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * expression:
2796e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>{@code
2797e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Files.newBufferedReader(path, StandardCharsets.UTF_8)
2798e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * }</pre>
2799e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2800e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2801e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
2802e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2803e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new buffered reader, with default buffer size, to read text
2804e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          from the file
2805e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2806e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2807e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs opening the file
2808e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2809e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2810e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
2811e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
2812e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2813e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since 1.8
2814e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2815e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static BufferedReader newBufferedReader(Path path) throws IOException {
2816e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return newBufferedReader(path, StandardCharsets.UTF_8);
2817e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2818e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2819e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2820e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens or creates a file for writing, returning a {@code BufferedWriter}
2821e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * that may be used to write text to the file in an efficient manner.
2822e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The {@code options} parameter specifies how the the file is created or
2823e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * opened. If no options are present then this method works as if the {@link
2824e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#CREATE CREATE}, {@link
2825e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
2826e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#WRITE WRITE} options are present. In other words, it
2827e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * opens the file for writing, creating the file if it doesn't exist, or
2828e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * initially truncating an existing {@link #isRegularFile regular-file} to
2829e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * a size of {@code 0} if it exists.
2830e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2831e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code Writer} methods to write text throw {@code IOException}
2832e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * if the text cannot be encoded using the specified charset.
2833e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2834e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2835e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
2836e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   cs
2837e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the charset to use for encoding
2838e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2839e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the file is opened
2840e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2841e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new buffered writer, with default buffer size, to write text
2842e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to the file
2843e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2844e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2845e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs opening or creating the file
2846e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
2847e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an unsupported option is specified
2848e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2849e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2850e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
2851e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the file.
2852e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2853e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see #write(Path,Iterable,Charset,OpenOption[])
2854e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2855e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static BufferedWriter newBufferedWriter(Path path, Charset cs,
2856e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                                   OpenOption... options)
2857e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2858e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2859e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        CharsetEncoder encoder = cs.newEncoder();
2860e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Writer writer = new OutputStreamWriter(newOutputStream(path, options), encoder);
2861e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return new BufferedWriter(writer);
2862e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2863e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2864e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2865e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Opens or creates a file for writing, returning a {@code BufferedWriter}
2866e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * to write text to the file in an efficient manner. The text is encoded
2867e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * into bytes for writing using the {@link StandardCharsets#UTF_8 UTF-8}
2868e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link Charset charset}.
2869e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2870e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method works as if invoking it were equivalent to evaluating the
2871e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * expression:
2872e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>{@code
2873e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Files.newBufferedWriter(path, StandardCharsets.UTF_8, options)
2874e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * }</pre>
2875e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2876e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
2877e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
2878e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2879e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the file is opened
2880e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2881e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a new buffered writer, with default buffer size, to write text
2882e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to the file
2883e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2884e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2885e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs opening or creating the file
2886e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
2887e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an unsupported option is specified
2888e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2889e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2890e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
2891e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the file.
2892e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2893e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since 1.8
2894e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2895e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static BufferedWriter newBufferedWriter(Path path, OpenOption... options) throws IOException {
2896e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return newBufferedWriter(path, StandardCharsets.UTF_8, options);
2897e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2898e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2899e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2900e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Reads all bytes from an input stream and writes them to an output stream.
2901e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2902e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static long copy(InputStream source, OutputStream sink)
2903e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2904e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2905e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        long nread = 0L;
2906e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        byte[] buf = new byte[BUFFER_SIZE];
2907e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        int n;
2908e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        while ((n = source.read(buf)) > 0) {
2909e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            sink.write(buf, 0, n);
2910e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            nread += n;
2911e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2912e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return nread;
2913e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
2914e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2915e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
2916e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Copies all bytes from an input stream to a file. On return, the input
2917e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream will be at end of stream.
2918e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2919e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> By default, the copy fails if the target file already exists or is a
2920e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * symbolic link. If the {@link StandardCopyOption#REPLACE_EXISTING
2921e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * REPLACE_EXISTING} option is specified, and the target file already exists,
2922e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * then it is replaced if it is not a non-empty directory. If the target
2923e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file exists and is a symbolic link, then the symbolic link is replaced.
2924e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * In this release, the {@code REPLACE_EXISTING} option is the only option
2925e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * required to be supported by this method. Additional options may be
2926e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * supported in future releases.
2927e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2928e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p>  If an I/O error occurs reading from the input stream or writing to
2929e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the file, then it may do so after the target file has been created and
2930e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * after some bytes have been read or written. Consequently the input
2931e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream may not be at end of stream and may be in an inconsistent state.
2932e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * It is strongly recommended that the input stream be promptly closed if an
2933e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * I/O error occurs.
2934e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2935e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method may block indefinitely reading from the input stream (or
2936e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * writing to the file). The behavior for the case that the input stream is
2937e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <i>asynchronously closed</i> or the thread interrupted during the copy is
2938e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * highly input stream and file system provider specific and therefore not
2939e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * specified.
2940e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2941e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage example</b>: Suppose we want to capture a web page and save
2942e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * it to a file:
2943e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
2944e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path path = ...
2945e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     URI u = URI.create("http://java.sun.com/");
2946e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     try (InputStream in = u.toURL().openStream()) {
2947e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         Files.copy(in, path);
2948e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     }
2949e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
2950e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2951e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   in
2952e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the input stream to read from
2953e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   target
2954e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
2955e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
2956e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the copy should be done
2957e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2958e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the number of bytes read or written
2959e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
2960e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
2961e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs when reading or writing
2962e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  FileAlreadyExistsException
2963e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the target file exists but cannot be replaced because the
2964e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          {@code REPLACE_EXISTING} option is not specified <i>(optional
2965e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          specific exception)</i>
2966e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  DirectoryNotEmptyException
2967e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the {@code REPLACE_EXISTING} option is specified but the file
2968e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          cannot be replaced because it is a non-empty directory
2969e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          <i>(optional specific exception)</i>     *
2970e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
2971e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if {@code options} contains a copy option that is not supported
2972e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
2973e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
2974e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
2975e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the file. Where the
2976e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          {@code REPLACE_EXISTING} option is specified, the security
2977e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          manager's {@link SecurityManager#checkDelete(String) checkDelete}
2978e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check that an existing file can be deleted.
2979e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
2980e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static long copy(InputStream in, Path target, CopyOption... options)
2981e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
2982e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
2983e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // ensure not null before opening file
2984e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Objects.requireNonNull(in);
2985e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
2986e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // check for REPLACE_EXISTING
2987e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        boolean replaceExisting = false;
2988e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        for (CopyOption opt: options) {
2989e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (opt == StandardCopyOption.REPLACE_EXISTING) {
2990e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                replaceExisting = true;
2991e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } else {
2992e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                if (opt == null) {
2993e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    throw new NullPointerException("options contains 'null'");
2994e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                }  else {
2995e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    throw new UnsupportedOperationException(opt + " not supported");
2996e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                }
2997e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
2998e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
2999e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3000e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // attempt to delete an existing file
3001e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        SecurityException se = null;
3002e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (replaceExisting) {
3003e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            try {
3004e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                deleteIfExists(target);
3005e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } catch (SecurityException x) {
3006e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                se = x;
3007e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
3008e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3009e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3010e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // attempt to create target file. If it fails with
3011e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // FileAlreadyExistsException then it may be because the security
3012e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // manager prevented us from deleting the file, in which case we just
3013e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // throw the SecurityException.
3014e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        OutputStream ostream;
3015e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
3016e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            ostream = newOutputStream(target, StandardOpenOption.CREATE_NEW,
3017e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                              StandardOpenOption.WRITE);
3018e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (FileAlreadyExistsException x) {
3019e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (se != null)
3020e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                throw se;
3021e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // someone else won the race and created the file
3022e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw x;
3023e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3024e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3025e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // do the copy
3026e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try (OutputStream out = ostream) {
3027e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return copy(in, out);
3028e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3029e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3030e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3031e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3032e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Copies all bytes from a file to an output stream.
3033e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3034e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If an I/O error occurs reading from the file or writing to the output
3035e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream, then it may do so after some bytes have been read or written.
3036e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Consequently the output stream may be in an inconsistent state. It is
3037e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * strongly recommended that the output stream be promptly closed if an I/O
3038e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * error occurs.
3039e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3040e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method may block indefinitely writing to the output stream (or
3041e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * reading from the file). The behavior for the case that the output stream
3042e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is <i>asynchronously closed</i> or the thread interrupted during the copy
3043e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is highly output stream and file system provider specific and therefore
3044e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * not specified.
3045e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3046e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Note that if the given output stream is {@link java.io.Flushable}
3047e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * then its {@link java.io.Flushable#flush flush} method may need to invoked
3048e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * after this method completes so as to flush any buffered output.
3049e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3050e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   source
3051e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the  path to the file
3052e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   out
3053e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the output stream to write to
3054e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3055e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the number of bytes read or written
3056e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3057e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3058e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs when reading or writing
3059e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3060e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3061e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3062e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
3063e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3064e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static long copy(Path source, OutputStream out) throws IOException {
3065e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // ensure not null before opening file
3066e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Objects.requireNonNull(out);
3067e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3068e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try (InputStream in = newInputStream(source)) {
3069e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return copy(in, out);
3070e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3071e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3072e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3073e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3074e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The maximum size of array to allocate.
3075e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Some VMs reserve some header words in an array.
3076e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Attempts to allocate larger arrays may result in
3077e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * OutOfMemoryError: Requested array size exceeds VM limit
3078e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3079e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
3080e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3081e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3082e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Reads all the bytes from an input stream. Uses {@code initialSize} as a hint
3083e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * about how many bytes the stream will have.
3084e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3085e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   source
3086e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the input stream to read from
3087e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   initialSize
3088e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the initial size of the byte array to allocate
3089e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3090e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a byte array containing the bytes read from the file
3091e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3092e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3093e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs reading from the stream
3094e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  OutOfMemoryError
3095e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an array of the required size cannot be allocated
3096e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3097e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    private static byte[] read(InputStream source, int initialSize) throws IOException {
3098e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        int capacity = initialSize;
3099e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        byte[] buf = new byte[capacity];
3100e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        int nread = 0;
3101e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        int n;
3102e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        for (;;) {
3103e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // read to EOF which may read more or less than initialSize (eg: file
3104e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // is truncated while we are reading)
3105e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            while ((n = source.read(buf, nread, capacity - nread)) > 0)
3106e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                nread += n;
3107e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3108e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // if last call to source.read() returned -1, we are done
3109e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // otherwise, try to read one more byte; if that failed we're done too
3110e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (n < 0 || (n = source.read()) < 0)
3111e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                break;
3112e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3113e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // one more byte was read; need to allocate a larger buffer
3114e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (capacity <= MAX_BUFFER_SIZE - capacity) {
3115e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                capacity = Math.max(capacity << 1, BUFFER_SIZE);
3116e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } else {
3117e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                if (capacity == MAX_BUFFER_SIZE)
3118e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    throw new OutOfMemoryError("Required array size too large");
3119e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                capacity = MAX_BUFFER_SIZE;
3120e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
3121e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            buf = Arrays.copyOf(buf, capacity);
3122e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            buf[nread++] = (byte)n;
3123e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3124e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
3125e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3126e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3127e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3128e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Reads all the bytes from a file. The method ensures that the file is
3129e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * closed when all bytes have been read or an I/O error, or other runtime
3130e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * exception, is thrown.
3131e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3132e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Note that this method is intended for simple cases where it is
3133e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * convenient to read all bytes into a byte array. It is not intended for
3134e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * reading in large files.
3135e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3136e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
3137e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
3138e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3139e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  a byte array containing the bytes read from the file
3140e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3141e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3142e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs reading from the stream
3143e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  OutOfMemoryError
3144e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an array of the required size cannot be allocated, for
3145e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          example the file is larger that {@code 2GB}
3146e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3147e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3148e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3149e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
3150e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3151e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static byte[] readAllBytes(Path path) throws IOException {
3152e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try (SeekableByteChannel sbc = Files.newByteChannel(path);
3153e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera             InputStream in = Channels.newInputStream(sbc)) {
3154e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            long size = sbc.size();
3155e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            if (size > (long)MAX_BUFFER_SIZE)
3156e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                throw new OutOfMemoryError("Required array size too large");
3157e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3158e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return read(in, (int)size);
3159e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3160e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3161e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3162e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3163e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Read all lines from a file. This method ensures that the file is
3164e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * closed when all bytes have been read or an I/O error, or other runtime
3165e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * exception, is thrown. Bytes from the file are decoded into characters
3166e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * using the specified charset.
3167e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3168e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method recognizes the following as line terminators:
3169e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <ul>
3170e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <li> <code>&#92;u000D</code> followed by <code>&#92;u000A</code>,
3171e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     CARRIAGE RETURN followed by LINE FEED </li>
3172e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <li> <code>&#92;u000A</code>, LINE FEED </li>
3173e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *   <li> <code>&#92;u000D</code>, CARRIAGE RETURN </li>
3174e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </ul>
3175e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Additional Unicode line terminators may be recognized in future
3176e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * releases.
3177e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3178e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Note that this method is intended for simple cases where it is
3179e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * convenient to read all lines in a single operation. It is not intended
3180e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * for reading in large files.
3181e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3182e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
3183e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
3184e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   cs
3185e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the charset to use for decoding
3186e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3187e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the lines from the file as a {@code List}; whether the {@code
3188e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          List} is modifiable or not is implementation dependent and
3189e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          therefore not specified
3190e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3191e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3192e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs reading from the file or a malformed or
3193e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          unmappable byte sequence is read
3194e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3195e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3196e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3197e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
3198e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3199e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see #newBufferedReader
3200e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3201e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static List<String> readAllLines(Path path, Charset cs) throws IOException {
3202e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try (BufferedReader reader = newBufferedReader(path, cs)) {
3203e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            List<String> result = new ArrayList<>();
3204e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            for (;;) {
3205e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                String line = reader.readLine();
3206e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                if (line == null)
3207e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    break;
3208e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                result.add(line);
3209e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
3210e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return result;
3211e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3212e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3213e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3214e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3215e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Read all lines from a file. Bytes from the file are decoded into characters
3216e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * using the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset charset}.
3217e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3218e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method works as if invoking it were equivalent to evaluating the
3219e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * expression:
3220e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>{@code
3221e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Files.readAllLines(path, StandardCharsets.UTF_8)
3222e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * }</pre>
3223e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3224e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
3225e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
3226e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3227e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the lines from the file as a {@code List}; whether the {@code
3228e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          List} is modifiable or not is implementation dependent and
3229e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          therefore not specified
3230e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3231e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3232e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs reading from the file or a malformed or
3233e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          unmappable byte sequence is read
3234e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3235e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3236e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3237e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
3238e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3239e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since 1.8
3240e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3241e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static List<String> readAllLines(Path path) throws IOException {
3242e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return readAllLines(path, StandardCharsets.UTF_8);
3243e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3244e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3245e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3246e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Writes bytes to a file. The {@code options} parameter specifies how the
3247e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the file is created or opened. If no options are present then this method
3248e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * works as if the {@link StandardOpenOption#CREATE CREATE}, {@link
3249e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
3250e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#WRITE WRITE} options are present. In other words, it
3251e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * opens the file for writing, creating the file if it doesn't exist, or
3252e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * initially truncating an existing {@link #isRegularFile regular-file} to
3253e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * a size of {@code 0}. All bytes in the byte array are written to the file.
3254e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The method ensures that the file is closed when all bytes have been
3255e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * written (or an I/O error or other runtime exception is thrown). If an I/O
3256e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * error occurs then it may do so after the file has created or truncated,
3257e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * or after some bytes have been written to the file.
3258e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3259e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> <b>Usage example</b>: By default the method creates a new file or
3260e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * overwrites an existing file. Suppose you instead want to append bytes
3261e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * to an existing file:
3262e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>
3263e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Path path = ...
3264e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     byte[] bytes = ...
3265e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *     Files.write(path, bytes, StandardOpenOption.APPEND);
3266e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre>
3267e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3268e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
3269e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
3270e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   bytes
3271e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the byte array with the bytes to write
3272e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
3273e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the file is opened
3274e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3275e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path
3276e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3277e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3278e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs writing to or creating the file
3279e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
3280e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an unsupported option is specified
3281e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3282e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3283e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
3284e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the file.
3285e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3286e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path write(Path path, byte[] bytes, OpenOption... options)
3287e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
3288e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
3289e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // ensure bytes is not null before opening file
3290e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Objects.requireNonNull(bytes);
3291e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3292e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try (OutputStream out = Files.newOutputStream(path, options)) {
3293e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            int len = bytes.length;
3294e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            int rem = len;
3295e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            while (rem > 0) {
3296e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                int n = Math.min(rem, BUFFER_SIZE);
3297e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                out.write(bytes, (len-rem), n);
3298e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                rem -= n;
3299e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
3300e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3301e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return path;
3302e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3303e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3304e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3305e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Write lines of text to a file. Each line is a char sequence and is
3306e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * written to the file in sequence with each line terminated by the
3307e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * platform's line separator, as defined by the system property {@code
3308e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * line.separator}. Characters are encoded into bytes using the specified
3309e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * charset.
3310e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3311e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code options} parameter specifies how the the file is created
3312e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * or opened. If no options are present then this method works as if the
3313e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link StandardOpenOption#CREATE CREATE}, {@link
3314e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
3315e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * StandardOpenOption#WRITE WRITE} options are present. In other words, it
3316e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * opens the file for writing, creating the file if it doesn't exist, or
3317e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * initially truncating an existing {@link #isRegularFile regular-file} to
3318e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * a size of {@code 0}. The method ensures that the file is closed when all
3319e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * lines have been written (or an I/O error or other runtime exception is
3320e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * thrown). If an I/O error occurs then it may do so after the file has
3321e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * created or truncated, or after some bytes have been written to the file.
3322e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3323e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
3324e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
3325e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   lines
3326e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an object to iterate over the char sequences
3327e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   cs
3328e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the charset to use for encoding
3329e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
3330e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the file is opened
3331e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3332e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path
3333e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3334e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3335e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs writing to or creating the file, or the
3336e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          text cannot be encoded using the specified charset
3337e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
3338e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an unsupported option is specified
3339e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3340e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3341e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
3342e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the file.
3343e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3344e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path write(Path path, Iterable<? extends CharSequence> lines,
3345e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                             Charset cs, OpenOption... options)
3346e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
3347e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
3348e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        // ensure lines is not null before opening file
3349e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        Objects.requireNonNull(lines);
3350e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        CharsetEncoder encoder = cs.newEncoder();
3351e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        OutputStream out = newOutputStream(path, options);
3352e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
3353e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            for (CharSequence line: lines) {
3354e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                writer.append(line);
3355e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                writer.newLine();
3356e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
3357e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3358e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return path;
3359e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3360e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3361e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3362e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Write lines of text to a file. Characters are encoded into bytes using
3363e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset charset}.
3364e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3365e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method works as if invoking it were equivalent to evaluating the
3366e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * expression:
3367e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>{@code
3368e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Files.write(path, lines, StandardCharsets.UTF_8, options);
3369e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * }</pre>
3370e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3371e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
3372e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
3373e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   lines
3374e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          an object to iterate over the char sequences
3375e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
3376e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options specifying how the file is opened
3377e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3378e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the path
3379e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3380e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3381e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs writing to or creating the file, or the
3382e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          text cannot be encoded as {@code UTF-8}
3383e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  UnsupportedOperationException
3384e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an unsupported option is specified
3385e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3386e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3387e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
3388e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check write access to the file.
3389e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3390e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since 1.8
3391e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3392e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Path write(Path path,
3393e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                             Iterable<? extends CharSequence> lines,
3394e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                             OpenOption... options)
3395e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
3396e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
3397e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return write(path, lines, StandardCharsets.UTF_8, options);
3398e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3399e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3400e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    // -- Stream APIs --
3401e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3402e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3403e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Return a lazily populated {@code Stream}, the elements of
3404e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * which are the entries in the directory.  The listing is not recursive.
3405e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3406e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The elements of the stream are {@link Path} objects that are
3407e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * obtained as if by {@link Path#resolve(Path) resolving} the name of the
3408e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory entry against {@code dir}. Some file systems maintain special
3409e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * links to the directory itself and the directory's parent directory.
3410e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Entries representing these links are not included.
3411e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3412e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The stream is <i>weakly consistent</i>. It is thread safe but does
3413e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * not freeze the directory while iterating, so it may (or may not)
3414e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * reflect updates to the directory that occur after returning from this
3415e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method.
3416e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3417e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The returned stream encapsulates a {@link DirectoryStream}.
3418e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * If timely disposal of file system resources is required, the
3419e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@code try}-with-resources construct should be used to ensure that the
3420e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@link Stream#close close} method is invoked after the stream
3421e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * operations are completed.
3422e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3423e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Operating on a closed stream behaves as if the end of stream
3424e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * has been reached. Due to read-ahead, one or more elements may be
3425e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * returned after the stream has been closed.
3426e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3427e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If an {@link IOException} is thrown when accessing the directory
3428e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * after this method has returned, it is wrapped in an {@link
3429e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * UncheckedIOException} which will be thrown from the method that caused
3430e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the access to take place.
3431e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3432e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   dir  The path to the directory
3433e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3434e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  The {@code Stream} describing the content of the
3435e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          directory
3436e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3437e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  NotDirectoryException
3438e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the file could not otherwise be opened because it is not
3439e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          a directory <i>(optional specific exception)</i>
3440e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3441e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs when opening the directory
3442e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3443e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3444e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3445e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the directory.
3446e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3447e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see     #newDirectoryStream(Path)
3448e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since   1.8
3449e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3450e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Stream<Path> list(Path dir) throws IOException {
3451e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        DirectoryStream<Path> ds = Files.newDirectoryStream(dir);
3452e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
3453e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            final Iterator<Path> delegate = ds.iterator();
3454e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3455e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            // Re-wrap DirectoryIteratorException to UncheckedIOException
3456e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            Iterator<Path> it = new Iterator<Path>() {
3457e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                @Override
3458e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                public boolean hasNext() {
3459e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    try {
3460e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        return delegate.hasNext();
3461e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    } catch (DirectoryIteratorException e) {
3462e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        throw new UncheckedIOException(e.getCause());
3463e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    }
3464e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                }
3465e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                @Override
3466e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                public Path next() {
3467e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    try {
3468e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        return delegate.next();
3469e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    } catch (DirectoryIteratorException e) {
3470e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                        throw new UncheckedIOException(e.getCause());
3471e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    }
3472e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                }
3473e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            };
3474e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3475e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.DISTINCT), false)
3476e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                .onClose(asUncheckedRunnable(ds));
3477e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (Error|RuntimeException e) {
3478e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            try {
3479e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                ds.close();
3480e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } catch (IOException ex) {
3481e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                try {
3482e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    e.addSuppressed(ex);
3483e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                } catch (Throwable ignore) {}
3484e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
3485e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw e;
3486e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3487e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3488e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3489e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3490e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Return a {@code Stream} that is lazily populated with {@code
3491e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Path} by walking the file tree rooted at a given starting file.  The
3492e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file tree is traversed <em>depth-first</em>, the elements in the stream
3493e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are {@link Path} objects that are obtained as if by {@link
3494e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Path#resolve(Path) resolving} the relative path against {@code start}.
3495e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3496e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code stream} walks the file tree as elements are consumed.
3497e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * The {@code Stream} returned is guaranteed to have at least one
3498e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * element, the starting file itself. For each file visited, the stream
3499e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * attempts to read its {@link BasicFileAttributes}. If the file is a
3500e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory and can be opened successfully, entries in the directory, and
3501e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * their <em>descendants</em> will follow the directory in the stream as
3502e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * they are encountered. When all entries have been visited, then the
3503e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory is closed. The file tree walk then continues at the next
3504e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <em>sibling</em> of the directory.
3505e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3506e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The stream is <i>weakly consistent</i>. It does not freeze the
3507e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file tree while iterating, so it may (or may not) reflect updates to
3508e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the file tree that occur after returned from this method.
3509e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3510e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> By default, symbolic links are not automatically followed by this
3511e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * method. If the {@code options} parameter contains the {@link
3512e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then symbolic links are
3513e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * followed. When following links, and the attributes of the target cannot
3514e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * be read, then this method attempts to get the {@code BasicFileAttributes}
3515e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * of the link.
3516e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3517e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If the {@code options} parameter contains the {@link
3518e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then the stream keeps
3519e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * track of directories visited so that cycles can be detected. A cycle
3520e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * arises when there is an entry in a directory that is an ancestor of the
3521e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directory. Cycle detection is done by recording the {@link
3522e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * java.nio.file.attribute.BasicFileAttributes#fileKey file-key} of directories,
3523e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * or if file keys are not available, by invoking the {@link #isSameFile
3524e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * isSameFile} method to test if a directory is the same file as an
3525e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * ancestor. When a cycle is detected it is treated as an I/O error with
3526e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * an instance of {@link FileSystemLoopException}.
3527e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3528e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The {@code maxDepth} parameter is the maximum number of levels of
3529e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * directories to visit. A value of {@code 0} means that only the starting
3530e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file is visited, unless denied by the security manager. A value of
3531e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link Integer#MAX_VALUE MAX_VALUE} may be used to indicate that all
3532e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * levels should be visited.
3533e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3534e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> When a security manager is installed and it denies access to a file
3535e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * (or directory), then it is ignored and not included in the stream.
3536e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3537e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The returned stream encapsulates one or more {@link DirectoryStream}s.
3538e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * If timely disposal of file system resources is required, the
3539e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@code try}-with-resources construct should be used to ensure that the
3540e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@link Stream#close close} method is invoked after the stream
3541e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * operations are completed.  Operating on a closed stream will result in an
3542e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link java.lang.IllegalStateException}.
3543e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3544e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If an {@link IOException} is thrown when accessing the directory
3545e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * after this method has returned, it is wrapped in an {@link
3546e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * UncheckedIOException} which will be thrown from the method that caused
3547e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the access to take place.
3548e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3549e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   start
3550e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the starting file
3551e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   maxDepth
3552e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the maximum number of directory levels to visit
3553e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
3554e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options to configure the traversal
3555e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3556e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the {@link Stream} of {@link Path}
3557e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3558e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
3559e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the {@code maxDepth} parameter is negative
3560e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3561e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          If the security manager denies access to the starting file.
3562e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, the {@link
3563e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkRead(String) checkRead} method is invoked
3564e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to check read access to the directory.
3565e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3566e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error is thrown when accessing the starting file.
3567e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since   1.8
3568e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3569e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Stream<Path> walk(Path start,
3570e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                    int maxDepth,
3571e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                    FileVisitOption... options)
3572e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
3573e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
3574e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
3575e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
3576e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT), false)
3577e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                .onClose(iterator::close)
3578e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                .map(entry -> entry.file());
3579e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (Error|RuntimeException e) {
3580e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            iterator.close();
3581e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw e;
3582e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3583e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3584e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3585e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3586e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Return a {@code Stream} that is lazily populated with {@code
3587e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Path} by walking the file tree rooted at a given starting file.  The
3588e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file tree is traversed <em>depth-first</em>, the elements in the stream
3589e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are {@link Path} objects that are obtained as if by {@link
3590e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Path#resolve(Path) resolving} the relative path against {@code start}.
3591e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3592e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method works as if invoking it were equivalent to evaluating the
3593e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * expression:
3594e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <blockquote><pre>
3595e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * walk(start, Integer.MAX_VALUE, options)
3596e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * </pre></blockquote>
3597e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * In other words, it visits all levels of the file tree.
3598e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3599e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The returned stream encapsulates one or more {@link DirectoryStream}s.
3600e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * If timely disposal of file system resources is required, the
3601e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@code try}-with-resources construct should be used to ensure that the
3602e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@link Stream#close close} method is invoked after the stream
3603e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * operations are completed.  Operating on a closed stream will result in an
3604e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link java.lang.IllegalStateException}.
3605e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3606e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   start
3607e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the starting file
3608e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
3609e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options to configure the traversal
3610e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3611e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the {@link Stream} of {@link Path}
3612e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3613e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3614e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          If the security manager denies access to the starting file.
3615e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, the {@link
3616e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkRead(String) checkRead} method is invoked
3617e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to check read access to the directory.
3618e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3619e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error is thrown when accessing the starting file.
3620e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3621e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see     #walk(Path, int, FileVisitOption...)
3622e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since   1.8
3623e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3624e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Stream<Path> walk(Path start, FileVisitOption... options) throws IOException {
3625e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return walk(start, Integer.MAX_VALUE, options);
3626e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3627e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3628e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3629e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Return a {@code Stream} that is lazily populated with {@code
3630e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Path} by searching for files in a file tree rooted at a given starting
3631e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * file.
3632e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3633e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method walks the file tree in exactly the manner specified by
3634e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the {@link #walk walk} method. For each file encountered, the given
3635e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link BiPredicate} is invoked with its {@link Path} and {@link
3636e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * BasicFileAttributes}. The {@code Path} object is obtained as if by
3637e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link Path#resolve(Path) resolving} the relative path against {@code
3638e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * start} and is only included in the returned {@link Stream} if
3639e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the {@code BiPredicate} returns true. Compare to calling {@link
3640e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * java.util.stream.Stream#filter filter} on the {@code Stream}
3641e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * returned by {@code walk} method, this method may be more efficient by
3642e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * avoiding redundant retrieval of the {@code BasicFileAttributes}.
3643e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3644e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The returned stream encapsulates one or more {@link DirectoryStream}s.
3645e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * If timely disposal of file system resources is required, the
3646e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@code try}-with-resources construct should be used to ensure that the
3647e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * stream's {@link Stream#close close} method is invoked after the stream
3648e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * operations are completed.  Operating on a closed stream will result in an
3649e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link java.lang.IllegalStateException}.
3650e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3651e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> If an {@link IOException} is thrown when accessing the directory
3652e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * after returned from this method, it is wrapped in an {@link
3653e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * UncheckedIOException} which will be thrown from the method that caused
3654e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * the access to take place.
3655e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3656e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   start
3657e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the starting file
3658e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   maxDepth
3659e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the maximum number of directory levels to search
3660e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   matcher
3661e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the function used to decide whether a file should be included
3662e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          in the returned stream
3663e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   options
3664e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          options to configure the traversal
3665e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3666e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the {@link Stream} of {@link Path}
3667e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3668e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IllegalArgumentException
3669e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if the {@code maxDepth} parameter is negative
3670e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3671e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          If the security manager denies access to the starting file.
3672e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, the {@link
3673e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          SecurityManager#checkRead(String) checkRead} method is invoked
3674e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          to check read access to the directory.
3675e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3676e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error is thrown when accessing the starting file.
3677e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3678e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see     #walk(Path, int, FileVisitOption...)
3679e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since   1.8
3680e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3681e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Stream<Path> find(Path start,
3682e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                    int maxDepth,
3683e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                    BiPredicate<Path, BasicFileAttributes> matcher,
3684e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                    FileVisitOption... options)
3685e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        throws IOException
3686e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
3687e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
3688e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
3689e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT), false)
3690e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                .onClose(iterator::close)
3691e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                .filter(entry -> matcher.test(entry.file(), entry.attributes()))
3692e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                                .map(entry -> entry.file());
3693e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (Error|RuntimeException e) {
3694e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            iterator.close();
3695e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw e;
3696e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3697e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3698e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3699e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3700e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Read all lines from a file as a {@code Stream}. Unlike {@link
3701e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * #readAllLines(Path, Charset) readAllLines}, this method does not read
3702e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * all lines into a {@code List}, but instead populates lazily as the stream
3703e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * is consumed.
3704e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3705e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> Bytes from the file are decoded into characters using the specified
3706e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * charset and the same line terminators as specified by {@code
3707e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * readAllLines} are supported.
3708e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3709e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> After this method returns, then any subsequent I/O exception that
3710e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * occurs while reading from the file or when a malformed or unmappable byte
3711e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * sequence is read, is wrapped in an {@link UncheckedIOException} that will
3712e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * be thrown from the
3713e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link java.util.stream.Stream} method that caused the read to take
3714e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * place. In case an {@code IOException} is thrown when closing the file,
3715e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * it is also wrapped as an {@code UncheckedIOException}.
3716e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3717e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> The returned stream encapsulates a {@link Reader}.  If timely
3718e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * disposal of file system resources is required, the try-with-resources
3719e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * construct should be used to ensure that the stream's
3720e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link Stream#close close} method is invoked after the stream operations
3721e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * are completed.
3722e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3723e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3724e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
3725e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
3726e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   cs
3727e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the charset to use for decoding
3728e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3729e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the lines from the file as a {@code Stream}
3730e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3731e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3732e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs opening the file
3733e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3734e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3735e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3736e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
3737e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3738e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see     #readAllLines(Path, Charset)
3739e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see     #newBufferedReader(Path, Charset)
3740e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @see     java.io.BufferedReader#lines()
3741e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since   1.8
3742e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3743e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Stream<String> lines(Path path, Charset cs) throws IOException {
3744e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        BufferedReader br = Files.newBufferedReader(path, cs);
3745e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        try {
3746e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            return br.lines().onClose(asUncheckedRunnable(br));
3747e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        } catch (Error|RuntimeException e) {
3748e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            try {
3749e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                br.close();
3750e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            } catch (IOException ex) {
3751e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                try {
3752e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                    e.addSuppressed(ex);
3753e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                } catch (Throwable ignore) {}
3754e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            }
3755e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw e;
3756e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        }
3757e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3758e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
3759e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
3760e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Read all lines from a file as a {@code Stream}. Bytes from the file are
3761e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * decoded into characters using the {@link StandardCharsets#UTF_8 UTF-8}
3762e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * {@link Charset charset}.
3763e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3764e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <p> This method works as if invoking it were equivalent to evaluating the
3765e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * expression:
3766e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * <pre>{@code
3767e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Files.lines(path, StandardCharsets.UTF_8)
3768e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * }</pre>
3769e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3770e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param   path
3771e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          the path to the file
3772e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3773e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @return  the lines from the file as a {@code Stream}
3774e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3775e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  IOException
3776e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          if an I/O error occurs opening the file
3777e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws  SecurityException
3778e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          In the case of the default provider, and a security manager is
3779e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
3780e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *          method is invoked to check read access to the file.
3781e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
3782e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since 1.8
3783e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
3784e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    public static Stream<String> lines(Path path) throws IOException {
3785e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        return lines(path, StandardCharsets.UTF_8);
3786e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
3787e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera}
3788