src/java.base/share/classes/java/nio/file/Files.java
author sgehwolf
Tue, 08 Oct 2019 15:30:46 +0200
changeset 58515 8f849d3ec1e5
parent 54971 4285b4d13471
permissions -rw-r--r--
8232003: (fs) Files.write can leak file descriptor in the exception case Summary: Be sure to close the leaked OutputStream in all cases Reviewed-by: alanb, bpb, clanger
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     1
/*
53388
8c08552a1fbd 8215467: Files.isHidden should return true for hidden directories on Windows
bpb
parents: 52700
diff changeset
     2
 * Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     4
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    10
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    15
 * accompanied this code).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    16
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3847
diff changeset
    23
 * questions.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    24
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    25
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    26
package java.nio.file;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    27
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    28
import java.io.BufferedReader;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    29
import java.io.BufferedWriter;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    30
import java.io.Closeable;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    31
import java.io.File;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    32
import java.io.IOException;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    33
import java.io.InputStream;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    34
import java.io.InputStreamReader;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    35
import java.io.OutputStream;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    36
import java.io.OutputStreamWriter;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    37
import java.io.Reader;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    38
import java.io.UncheckedIOException;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    39
import java.io.Writer;
19185
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
    40
import java.nio.channels.Channels;
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
    41
import java.nio.channels.FileChannel;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    42
import java.nio.channels.SeekableByteChannel;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    43
import java.nio.charset.Charset;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    44
import java.nio.charset.CharsetDecoder;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    45
import java.nio.charset.CharsetEncoder;
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
    46
import java.nio.charset.StandardCharsets;
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    47
import java.nio.file.attribute.BasicFileAttributeView;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    48
import java.nio.file.attribute.BasicFileAttributes;
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
    49
import java.nio.file.attribute.DosFileAttributes;   // javadoc
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    50
import java.nio.file.attribute.FileAttribute;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    51
import java.nio.file.attribute.FileAttributeView;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    52
import java.nio.file.attribute.FileOwnerAttributeView;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    53
import java.nio.file.attribute.FileStoreAttributeView;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    54
import java.nio.file.attribute.FileTime;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    55
import java.nio.file.attribute.PosixFileAttributeView;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    56
import java.nio.file.attribute.PosixFileAttributes;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    57
import java.nio.file.attribute.PosixFilePermission;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    58
import java.nio.file.attribute.UserPrincipal;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    59
import java.nio.file.spi.FileSystemProvider;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    60
import java.nio.file.spi.FileTypeDetector;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    61
import java.security.AccessController;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    62
import java.security.PrivilegedAction;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    63
import java.util.ArrayList;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    64
import java.util.Arrays;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    65
import java.util.Collections;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    66
import java.util.EnumSet;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    67
import java.util.HashSet;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    68
import java.util.Iterator;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    69
import java.util.List;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    70
import java.util.Map;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    71
import java.util.Objects;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    72
import java.util.ServiceLoader;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    73
import java.util.Set;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    74
import java.util.Spliterator;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    75
import java.util.Spliterators;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    76
import java.util.function.BiPredicate;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    77
import java.util.stream.Stream;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
    78
import java.util.stream.StreamSupport;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    79
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 53388
diff changeset
    80
import jdk.internal.util.ArraysSupport;
50784
57f5cba78093 8205612: (fc) Files.readAllBytes fails with ClosedByInterruptException when interrupt status set
alanb
parents: 50701
diff changeset
    81
import sun.nio.ch.FileChannelImpl;
37346
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
    82
import sun.nio.fs.AbstractFileSystemProvider;
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
    83
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    84
/**
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    85
 * This class consists exclusively of static methods that operate on files,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    86
 * directories, or other types of files.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    87
 *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    88
 * <p> In most cases, the methods defined here will delegate to the associated
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
    89
 * file system provider to perform the file operations.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    90
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    91
 * @since 1.7
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    92
 */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    93
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    94
public final class Files {
52458
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
    95
    // buffer size used for reading and writing
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
    96
    private static final int BUFFER_SIZE = 8192;
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
    97
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    98
    private Files() { }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
    99
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   100
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   101
     * Returns the {@code FileSystemProvider} to delegate to.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   102
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   103
    private static FileSystemProvider provider(Path path) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   104
        return path.getFileSystem().provider();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   105
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   106
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   107
    /**
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   108
     * Convert a Closeable to a Runnable by converting checked IOException
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   109
     * to UncheckedIOException
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   110
     */
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   111
    private static Runnable asUncheckedRunnable(Closeable c) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   112
        return () -> {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   113
            try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   114
                c.close();
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
   115
            } catch (IOException e) {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   116
                throw new UncheckedIOException(e);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   117
            }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   118
        };
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   119
    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
   120
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   121
    // -- File contents --
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   122
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   123
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   124
     * Opens a file, returning an input stream to read from the file. The stream
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   125
     * will not be buffered, and is not required to support the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   126
     * InputStream#mark mark} or {@link InputStream#reset reset} methods. The
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   127
     * stream will be safe for access by multiple concurrent threads. Reading
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   128
     * commences at the beginning of the file. Whether the returned stream is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   129
     * <i>asynchronously closeable</i> and/or <i>interruptible</i> is highly
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   130
     * file system provider specific and therefore not specified.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   131
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   132
     * <p> The {@code options} parameter determines how the file is opened.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   133
     * If no options are present then it is equivalent to opening the file with
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   134
     * the {@link StandardOpenOption#READ READ} option. In addition to the {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   135
     * READ} option, an implementation may also support additional implementation
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   136
     * specific options.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   137
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   138
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   139
     *          the path to the file to open
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   140
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   141
     *          options specifying how the file is opened
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   142
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   143
     * @return  a new input stream
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   144
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   145
     * @throws  IllegalArgumentException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   146
     *          if an invalid combination of options is specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   147
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   148
     *          if an unsupported option is specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   149
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   150
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   151
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   152
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   153
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   154
     *          method is invoked to check read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   155
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   156
    public static InputStream newInputStream(Path path, OpenOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   157
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   158
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   159
        return provider(path).newInputStream(path, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   160
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   161
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   162
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   163
     * Opens or creates a file, returning an output stream that may be used to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   164
     * write bytes to the file. The resulting stream will not be buffered. The
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   165
     * stream will be safe for access by multiple concurrent threads. Whether
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   166
     * the returned stream is <i>asynchronously closeable</i> and/or
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   167
     * <i>interruptible</i> is highly file system provider specific and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   168
     * therefore not specified.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   169
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   170
     * <p> This method opens or creates a file in exactly the manner specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   171
     * by the {@link #newByteChannel(Path,Set,FileAttribute[]) newByteChannel}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   172
     * method with the exception that the {@link StandardOpenOption#READ READ}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   173
     * option may not be present in the array of options. If no options are
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   174
     * present then this method works as if the {@link StandardOpenOption#CREATE
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   175
     * CREATE}, {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING},
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   176
     * and {@link StandardOpenOption#WRITE WRITE} options are present. In other
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   177
     * words, it opens the file for writing, creating the file if it doesn't
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   178
     * exist, or initially truncating an existing {@link #isRegularFile
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   179
     * regular-file} to a size of {@code 0} if it exists.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   180
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   181
     * <p> <b>Usage Examples:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   182
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   183
     *     Path path = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   184
     *
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8166
diff changeset
   185
     *     // truncate and overwrite an existing file, or create the file if
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8166
diff changeset
   186
     *     // it doesn't initially exist
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   187
     *     OutputStream out = Files.newOutputStream(path);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   188
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   189
     *     // append to an existing file, fail if the file does not exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   190
     *     out = Files.newOutputStream(path, APPEND);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   191
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   192
     *     // append to an existing file, create file if it doesn't initially exist
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8166
diff changeset
   193
     *     out = Files.newOutputStream(path, CREATE, APPEND);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   194
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   195
     *     // always create new file, failing if it already exists
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8166
diff changeset
   196
     *     out = Files.newOutputStream(path, CREATE_NEW);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   197
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   198
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   199
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   200
     *          the path to the file to open or create
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   201
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   202
     *          options specifying how the file is opened
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   203
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   204
     * @return  a new output stream
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   205
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   206
     * @throws  IllegalArgumentException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   207
     *          if {@code options} contains an invalid combination of options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   208
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   209
     *          if an unsupported option is specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   210
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   211
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   212
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   213
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   214
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   215
     *          method is invoked to check write access to the file. The {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   216
     *          SecurityManager#checkDelete(String) checkDelete} method is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   217
     *          invoked to check delete access if the file is opened with the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   218
     *          {@code DELETE_ON_CLOSE} option.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   219
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   220
    public static OutputStream newOutputStream(Path path, OpenOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   221
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   222
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   223
        return provider(path).newOutputStream(path, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   224
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   225
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   226
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   227
     * Opens or creates a file, returning a seekable byte channel to access the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   228
     * file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   229
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   230
     * <p> The {@code options} parameter determines how the file is opened.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   231
     * The {@link StandardOpenOption#READ READ} and {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   232
     * StandardOpenOption#WRITE WRITE} options determine if the file should be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   233
     * opened for reading and/or writing. If neither option (or the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   234
     * StandardOpenOption#APPEND APPEND} option) is present then the file is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   235
     * opened for reading. By default reading or writing commence at the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   236
     * beginning of the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   237
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   238
     * <p> In the addition to {@code READ} and {@code WRITE}, the following
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   239
     * options may be present:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   240
     *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
   241
     * <table class="striped">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
   242
     * <caption style="display:none">Options</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
   243
     * <thead>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
   244
     * <tr> <th scope="col">Option</th> <th scope="col">Description</th> </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
   245
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
   246
     * <tbody>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   247
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
   248
     *   <th scope="row"> {@link StandardOpenOption#APPEND APPEND} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   249
     *   <td> If this option is present then the file is opened for writing and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   250
     *     each invocation of the channel's {@code write} method first advances
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   251
     *     the position to the end of the file and then writes the requested
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   252
     *     data. Whether the advancement of the position and the writing of the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   253
     *     data are done in a single atomic operation is system-dependent and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   254
     *     therefore unspecified. This option may not be used in conjunction
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   255
     *     with the {@code READ} or {@code TRUNCATE_EXISTING} options. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   256
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   257
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
   258
     *   <th scope="row"> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   259
     *   <td> If this option is present then the existing file is truncated to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   260
     *   a size of 0 bytes. This option is ignored when the file is opened only
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   261
     *   for reading. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   262
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   263
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
   264
     *   <th scope="row"> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   265
     *   <td> If this option is present then a new file is created, failing if
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   266
     *   the file already exists or is a symbolic link. When creating a file the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   267
     *   check for the existence of the file and the creation of the file if it
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   268
     *   does not exist is atomic with respect to other file system operations.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   269
     *   This option is ignored when the file is opened only for reading. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   270
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   271
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
   272
     *   <th scope="row" > {@link StandardOpenOption#CREATE CREATE} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   273
     *   <td> If this option is present then an existing file is opened if it
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   274
     *   exists, otherwise a new file is created. This option is ignored if the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   275
     *   {@code CREATE_NEW} option is also present or the file is opened only
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   276
     *   for reading. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   277
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   278
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
   279
     *   <th scope="row" > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   280
     *   <td> When this option is present then the implementation makes a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   281
     *   <em>best effort</em> attempt to delete the file when closed by the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   282
     *   {@link SeekableByteChannel#close close} method. If the {@code close}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   283
     *   method is not invoked then a <em>best effort</em> attempt is made to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   284
     *   delete the file when the Java virtual machine terminates. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   285
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   286
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
   287
     *   <th scope="row">{@link StandardOpenOption#SPARSE SPARSE} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   288
     *   <td> When creating a new file this option is a <em>hint</em> that the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   289
     *   new file will be sparse. This option is ignored when not creating
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   290
     *   a new file. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   291
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   292
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
   293
     *   <th scope="row"> {@link StandardOpenOption#SYNC SYNC} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   294
     *   <td> Requires that every update to the file's content or metadata be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   295
     *   written synchronously to the underlying storage device. (see <a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   296
     *   href="package-summary.html#integrity"> Synchronized I/O file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   297
     *   integrity</a>). </td>
21801
b8a5ff5f0c2a 8028049: Tidy warnings cleanup for packages java.nio/java.io
yan
parents: 20779
diff changeset
   298
     * </tr>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   299
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
   300
     *   <th scope="row"> {@link StandardOpenOption#DSYNC DSYNC} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   301
     *   <td> Requires that every update to the file's content be written
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   302
     *   synchronously to the underlying storage device. (see <a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   303
     *   href="package-summary.html#integrity"> Synchronized I/O file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   304
     *   integrity</a>). </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   305
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
   306
     * </tbody>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   307
     * </table>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   308
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   309
     * <p> An implementation may also support additional implementation specific
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   310
     * options.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   311
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   312
     * <p> The {@code attrs} parameter is optional {@link FileAttribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   313
     * file-attributes} to set atomically when a new file is created.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   314
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   315
     * <p> In the case of the default provider, the returned seekable byte channel
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   316
     * is a {@link java.nio.channels.FileChannel}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   317
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   318
     * <p> <b>Usage Examples:</b>
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
   319
     * <pre>{@code
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   320
     *     Path path = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   321
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   322
     *     // open file for reading
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   323
     *     ReadableByteChannel rbc = Files.newByteChannel(path, EnumSet.of(READ)));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   324
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   325
     *     // open file for writing to the end of an existing file, creating
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   326
     *     // the file if it doesn't already exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   327
     *     WritableByteChannel wbc = Files.newByteChannel(path, EnumSet.of(CREATE,APPEND));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   328
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   329
     *     // create file with initial permissions, opening it for both reading and writing
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
   330
     *     FileAttribute<Set<PosixFilePermission>> perms = ...
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
   331
     *     SeekableByteChannel sbc =
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
   332
     *         Files.newByteChannel(path, EnumSet.of(CREATE_NEW,READ,WRITE), perms);
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
   333
     * }</pre>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   334
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   335
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   336
     *          the path to the file to open or create
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   337
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   338
     *          options specifying how the file is opened
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   339
     * @param   attrs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   340
     *          an optional list of file attributes to set atomically when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   341
     *          creating the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   342
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   343
     * @return  a new seekable byte channel
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   344
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   345
     * @throws  IllegalArgumentException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   346
     *          if the set contains an invalid combination of options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   347
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   348
     *          if an unsupported open option is specified or the array contains
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   349
     *          attributes that cannot be set atomically when creating the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   350
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   351
     *          if a file of that name already exists and the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   352
     *          StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   353
     *          <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   354
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   355
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   356
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   357
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   358
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   359
     *          method is invoked to check read access to the path if the file is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   360
     *          opened for reading. The {@link SecurityManager#checkWrite(String)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   361
     *          checkWrite} method is invoked to check write access to the path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   362
     *          if the file is opened for writing. The {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   363
     *          SecurityManager#checkDelete(String) checkDelete} method is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   364
     *          invoked to check delete access if the file is opened with the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   365
     *          {@code DELETE_ON_CLOSE} option.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   366
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   367
     * @see java.nio.channels.FileChannel#open(Path,Set,FileAttribute[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   368
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   369
    public static SeekableByteChannel newByteChannel(Path path,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   370
                                                     Set<? extends OpenOption> options,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   371
                                                     FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   372
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   373
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   374
        return provider(path).newByteChannel(path, options, attrs);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   375
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   376
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   377
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   378
     * Opens or creates a file, returning a seekable byte channel to access the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   379
     * file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   380
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   381
     * <p> This method opens or creates a file in exactly the manner specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   382
     * by the {@link #newByteChannel(Path,Set,FileAttribute[]) newByteChannel}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   383
     * method.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   384
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   385
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   386
     *          the path to the file to open or create
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   387
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   388
     *          options specifying how the file is opened
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   389
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   390
     * @return  a new seekable byte channel
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   391
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   392
     * @throws  IllegalArgumentException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   393
     *          if the set contains an invalid combination of options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   394
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   395
     *          if an unsupported open option is specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   396
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   397
     *          if a file of that name already exists and the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   398
     *          StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   399
     *          <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   400
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   401
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   402
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   403
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   404
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   405
     *          method is invoked to check read access to the path if the file is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   406
     *          opened for reading. The {@link SecurityManager#checkWrite(String)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   407
     *          checkWrite} method is invoked to check write access to the path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   408
     *          if the file is opened for writing. The {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   409
     *          SecurityManager#checkDelete(String) checkDelete} method is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   410
     *          invoked to check delete access if the file is opened with the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   411
     *          {@code DELETE_ON_CLOSE} option.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   412
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   413
     * @see java.nio.channels.FileChannel#open(Path,OpenOption[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   414
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   415
    public static SeekableByteChannel newByteChannel(Path path, OpenOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   416
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   417
    {
50701
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   418
        Set<OpenOption> set;
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   419
        if (options.length == 0) {
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   420
            set = Collections.emptySet();
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   421
        } else {
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   422
            set = new HashSet<>();
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   423
            Collections.addAll(set, options);
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   424
        }
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   425
        return newByteChannel(path, set);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   426
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   427
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   428
    // -- Directories --
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   429
10891
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   430
    private static class AcceptAllFilter
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   431
        implements DirectoryStream.Filter<Path>
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   432
    {
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   433
        private AcceptAllFilter() { }
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   434
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   435
        @Override
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   436
        public boolean accept(Path entry) { return true; }
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   437
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   438
        static final AcceptAllFilter FILTER = new AcceptAllFilter();
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   439
    }
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   440
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   441
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   442
     * Opens a directory, returning a {@link DirectoryStream} to iterate over
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   443
     * all entries in the directory. The elements returned by the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   444
     * stream's {@link DirectoryStream#iterator iterator} are of type {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   445
     * Path}, each one representing an entry in the directory. The {@code Path}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   446
     * objects are obtained as if by {@link Path#resolve(Path) resolving} the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   447
     * name of the directory entry against {@code dir}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   448
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   449
     * <p> When not using the try-with-resources construct, then directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   450
     * stream's {@code close} method should be invoked after iteration is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   451
     * completed so as to free any resources held for the open directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   452
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   453
     * <p> When an implementation supports operations on entries in the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   454
     * directory that execute in a race-free manner then the returned directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   455
     * stream is a {@link SecureDirectoryStream}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   456
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   457
     * @param   dir
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   458
     *          the path to the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   459
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   460
     * @return  a new and open {@code DirectoryStream} object
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   461
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   462
     * @throws  NotDirectoryException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   463
     *          if the file could not otherwise be opened because it is not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   464
     *          a directory <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   465
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   466
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   467
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   468
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   469
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   470
     *          method is invoked to check read access to the directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   471
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   472
    public static DirectoryStream<Path> newDirectoryStream(Path dir)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   473
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   474
    {
10891
8c554ab7cfed 7103889: (fs) Reduce String concatenation when iterating over directory
alanb
parents: 9050
diff changeset
   475
        return provider(dir).newDirectoryStream(dir, AcceptAllFilter.FILTER);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   476
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   477
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   478
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   479
     * Opens a directory, returning a {@link DirectoryStream} to iterate over
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   480
     * the entries in the directory. The elements returned by the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   481
     * stream's {@link DirectoryStream#iterator iterator} are of type {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   482
     * Path}, each one representing an entry in the directory. The {@code Path}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   483
     * objects are obtained as if by {@link Path#resolve(Path) resolving} the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   484
     * name of the directory entry against {@code dir}. The entries returned by
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   485
     * the iterator are filtered by matching the {@code String} representation
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   486
     * of their file names against the given <em>globbing</em> pattern.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   487
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   488
     * <p> For example, suppose we want to iterate over the files ending with
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   489
     * ".java" in a directory:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   490
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   491
     *     Path dir = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   492
     *     try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir, "*.java")) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   493
     *         :
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   494
     *     }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   495
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   496
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   497
     * <p> The globbing pattern is specified by the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   498
     * FileSystem#getPathMatcher getPathMatcher} method.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   499
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   500
     * <p> When not using the try-with-resources construct, then directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   501
     * stream's {@code close} method should be invoked after iteration is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   502
     * completed so as to free any resources held for the open directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   503
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   504
     * <p> When an implementation supports operations on entries in the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   505
     * directory that execute in a race-free manner then the returned directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   506
     * stream is a {@link SecureDirectoryStream}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   507
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   508
     * @param   dir
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   509
     *          the path to the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   510
     * @param   glob
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   511
     *          the glob pattern
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   512
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   513
     * @return  a new and open {@code DirectoryStream} object
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   514
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   515
     * @throws  java.util.regex.PatternSyntaxException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   516
     *          if the pattern is invalid
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   517
     * @throws  NotDirectoryException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   518
     *          if the file could not otherwise be opened because it is not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   519
     *          a directory <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   520
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   521
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   522
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   523
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   524
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   525
     *          method is invoked to check read access to the directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   526
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   527
    public static DirectoryStream<Path> newDirectoryStream(Path dir, String glob)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   528
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   529
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   530
        // avoid creating a matcher if all entries are required.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   531
        if (glob.equals("*"))
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   532
            return newDirectoryStream(dir);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   533
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   534
        // create a matcher and return a filter that uses it.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   535
        FileSystem fs = dir.getFileSystem();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   536
        final PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29269
diff changeset
   537
        DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<>() {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   538
            @Override
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   539
            public boolean accept(Path entry)  {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   540
                return matcher.matches(entry.getFileName());
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   541
            }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   542
        };
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   543
        return fs.provider().newDirectoryStream(dir, filter);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   544
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   545
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   546
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   547
     * Opens a directory, returning a {@link DirectoryStream} to iterate over
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   548
     * the entries in the directory. The elements returned by the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   549
     * stream's {@link DirectoryStream#iterator iterator} are of type {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   550
     * Path}, each one representing an entry in the directory. The {@code Path}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   551
     * objects are obtained as if by {@link Path#resolve(Path) resolving} the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   552
     * name of the directory entry against {@code dir}. The entries returned by
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   553
     * the iterator are filtered by the given {@link DirectoryStream.Filter
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   554
     * filter}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   555
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   556
     * <p> When not using the try-with-resources construct, then directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   557
     * stream's {@code close} method should be invoked after iteration is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   558
     * completed so as to free any resources held for the open directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   559
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   560
     * <p> Where the filter terminates due to an uncaught error or runtime
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   561
     * exception then it is propagated to the {@link Iterator#hasNext()
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   562
     * hasNext} or {@link Iterator#next() next} method. Where an {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   563
     * IOException} is thrown, it results in the {@code hasNext} or {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   564
     * next} method throwing a {@link DirectoryIteratorException} with the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   565
     * {@code IOException} as the cause.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   566
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   567
     * <p> When an implementation supports operations on entries in the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   568
     * directory that execute in a race-free manner then the returned directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   569
     * stream is a {@link SecureDirectoryStream}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   570
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   571
     * <p> <b>Usage Example:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   572
     * Suppose we want to iterate over the files in a directory that are
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   573
     * larger than 8K.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   574
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   575
     *     DirectoryStream.Filter&lt;Path&gt; filter = new DirectoryStream.Filter&lt;Path&gt;() {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   576
     *         public boolean accept(Path file) throws IOException {
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 12421
diff changeset
   577
     *             return (Files.size(file) &gt; 8192L);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   578
     *         }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   579
     *     };
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   580
     *     Path dir = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   581
     *     try (DirectoryStream&lt;Path&gt; stream = Files.newDirectoryStream(dir, filter)) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   582
     *         :
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   583
     *     }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   584
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   585
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   586
     * @param   dir
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   587
     *          the path to the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   588
     * @param   filter
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   589
     *          the directory stream filter
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   590
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   591
     * @return  a new and open {@code DirectoryStream} object
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   592
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   593
     * @throws  NotDirectoryException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   594
     *          if the file could not otherwise be opened because it is not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   595
     *          a directory <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   596
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   597
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   598
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   599
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   600
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   601
     *          method is invoked to check read access to the directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   602
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   603
    public static DirectoryStream<Path> newDirectoryStream(Path dir,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   604
                                                           DirectoryStream.Filter<? super Path> filter)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   605
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   606
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   607
        return provider(dir).newDirectoryStream(dir, filter);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   608
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   609
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   610
    // -- Creation and deletion --
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   611
50701
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   612
    private static final Set<OpenOption> DEFAULT_CREATE_OPTIONS =
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   613
        Set.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   614
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   615
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   616
     * Creates a new and empty file, failing if the file already exists. The
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   617
     * check for the existence of the file and the creation of the new file if
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   618
     * it does not exist are a single operation that is atomic with respect to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   619
     * all other filesystem activities that might affect the directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   620
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   621
     * <p> The {@code attrs} parameter is optional {@link FileAttribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   622
     * file-attributes} to set atomically when creating the file. Each attribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   623
     * is identified by its {@link FileAttribute#name name}. If more than one
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   624
     * attribute of the same name is included in the array then all but the last
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   625
     * occurrence is ignored.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   626
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   627
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   628
     *          the path to the file to create
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   629
     * @param   attrs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   630
     *          an optional list of file attributes to set atomically when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   631
     *          creating the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   632
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   633
     * @return  the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   634
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   635
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   636
     *          if the array contains an attribute that cannot be set atomically
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   637
     *          when creating the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   638
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   639
     *          if a file of that name already exists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   640
     *          <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   641
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   642
     *          if an I/O error occurs or the parent directory does not exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   643
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   644
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   645
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   646
     *          method is invoked to check write access to the new file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   647
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   648
    public static Path createFile(Path path, FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   649
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   650
    {
50701
80fe6f64d8a0 8199124: (fs) Reduce allocation for file system methods that are invoked with no open options
bpb
parents: 50552
diff changeset
   651
        newByteChannel(path, DEFAULT_CREATE_OPTIONS, attrs).close();
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   652
        return path;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   653
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   654
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   655
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   656
     * Creates a new directory. The check for the existence of the file and the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   657
     * creation of the directory if it does not exist are a single operation
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   658
     * that is atomic with respect to all other filesystem activities that might
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   659
     * affect the directory. The {@link #createDirectories createDirectories}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   660
     * method should be used where it is required to create all nonexistent
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   661
     * parent directories first.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   662
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   663
     * <p> The {@code attrs} parameter is optional {@link FileAttribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   664
     * file-attributes} to set atomically when creating the directory. Each
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   665
     * attribute is identified by its {@link FileAttribute#name name}. If more
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   666
     * than one attribute of the same name is included in the array then all but
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   667
     * the last occurrence is ignored.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   668
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   669
     * @param   dir
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   670
     *          the directory to create
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   671
     * @param   attrs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   672
     *          an optional list of file attributes to set atomically when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   673
     *          creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   674
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   675
     * @return  the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   676
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   677
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   678
     *          if the array contains an attribute that cannot be set atomically
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   679
     *          when creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   680
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   681
     *          if a directory could not otherwise be created because a file of
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   682
     *          that name already exists <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   683
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   684
     *          if an I/O error occurs or the parent directory does not exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   685
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   686
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   687
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   688
     *          method is invoked to check write access to the new directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   689
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   690
    public static Path createDirectory(Path dir, FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   691
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   692
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   693
        provider(dir).createDirectory(dir, attrs);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   694
        return dir;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   695
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   696
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   697
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   698
     * Creates a directory by creating all nonexistent parent directories first.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   699
     * Unlike the {@link #createDirectory createDirectory} method, an exception
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   700
     * is not thrown if the directory could not be created because it already
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   701
     * exists.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   702
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   703
     * <p> The {@code attrs} parameter is optional {@link FileAttribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   704
     * file-attributes} to set atomically when creating the nonexistent
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   705
     * directories. Each file attribute is identified by its {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   706
     * FileAttribute#name name}. If more than one attribute of the same name is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   707
     * included in the array then all but the last occurrence is ignored.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   708
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   709
     * <p> If this method fails, then it may do so after creating some, but not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   710
     * all, of the parent directories.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   711
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   712
     * @param   dir
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   713
     *          the directory to create
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   714
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   715
     * @param   attrs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   716
     *          an optional list of file attributes to set atomically when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   717
     *          creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   718
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   719
     * @return  the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   720
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   721
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   722
     *          if the array contains an attribute that cannot be set atomically
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   723
     *          when creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   724
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   725
     *          if {@code dir} exists but is not a directory <i>(optional specific
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   726
     *          exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   727
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   728
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   729
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   730
     *          in the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   731
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   732
     *          method is invoked prior to attempting to create a directory and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   733
     *          its {@link SecurityManager#checkRead(String) checkRead} is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   734
     *          invoked for each parent directory that is checked. If {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   735
     *          dir} is not an absolute path then its {@link Path#toAbsolutePath
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   736
     *          toAbsolutePath} may need to be invoked to get its absolute path.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   737
     *          This may invoke the security manager's {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   738
     *          SecurityManager#checkPropertyAccess(String) checkPropertyAccess}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   739
     *          method to check access to the system property {@code user.dir}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   740
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   741
    public static Path createDirectories(Path dir, FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   742
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   743
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   744
        // attempt to create the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   745
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   746
            createAndCheckIsDirectory(dir, attrs);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   747
            return dir;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   748
        } catch (FileAlreadyExistsException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   749
            // file exists and is not a directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   750
            throw x;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   751
        } catch (IOException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   752
            // parent may not exist or other reason
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   753
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   754
        SecurityException se = null;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   755
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   756
            dir = dir.toAbsolutePath();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   757
        } catch (SecurityException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   758
            // don't have permission to get absolute path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   759
            se = x;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   760
        }
23887
7b2fb8d5f6be 8040262: (fs) Misc. typos in comments and implementation
alanb
parents: 22351
diff changeset
   761
        // find a descendant that exists
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   762
        Path parent = dir.getParent();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   763
        while (parent != null) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   764
            try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   765
                provider(parent).checkAccess(parent);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   766
                break;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   767
            } catch (NoSuchFileException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   768
                // does not exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   769
            }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   770
            parent = parent.getParent();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   771
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   772
        if (parent == null) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   773
            // unable to find existing parent
22351
d8ac878ba804 8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents: 21801
diff changeset
   774
            if (se == null) {
d8ac878ba804 8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents: 21801
diff changeset
   775
                throw new FileSystemException(dir.toString(), null,
d8ac878ba804 8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents: 21801
diff changeset
   776
                    "Unable to determine if root directory exists");
d8ac878ba804 8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents: 21801
diff changeset
   777
            } else {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   778
                throw se;
22351
d8ac878ba804 8032220: Files.createDirectories throws exception with confusing message for root directories that exist
alanb
parents: 21801
diff changeset
   779
            }
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   780
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   781
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   782
        // create directories
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   783
        Path child = parent;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   784
        for (Path name: parent.relativize(dir)) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   785
            child = child.resolve(name);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   786
            createAndCheckIsDirectory(child, attrs);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   787
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   788
        return dir;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   789
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   790
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   791
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   792
     * Used by createDirectories to attempt to create a directory. A no-op
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   793
     * if the directory already exists.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   794
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   795
    private static void createAndCheckIsDirectory(Path dir,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   796
                                                  FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   797
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   798
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   799
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   800
            createDirectory(dir, attrs);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   801
        } catch (FileAlreadyExistsException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   802
            if (!isDirectory(dir, LinkOption.NOFOLLOW_LINKS))
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   803
                throw x;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   804
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   805
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   806
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   807
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   808
     * Creates a new empty file in the specified directory, using the given
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   809
     * prefix and suffix strings to generate its name. The resulting
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   810
     * {@code Path} is associated with the same {@code FileSystem} as the given
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   811
     * directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   812
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   813
     * <p> The details as to how the name of the file is constructed is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   814
     * implementation dependent and therefore not specified. Where possible
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   815
     * the {@code prefix} and {@code suffix} are used to construct candidate
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   816
     * names in the same manner as the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   817
     * java.io.File#createTempFile(String,String,File)} method.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   818
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   819
     * <p> As with the {@code File.createTempFile} methods, this method is only
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   820
     * part of a temporary-file facility. Where used as a <em>work files</em>,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   821
     * the resulting file may be opened using the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   822
     * StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} option so that the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   823
     * file is deleted when the appropriate {@code close} method is invoked.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   824
     * Alternatively, a {@link Runtime#addShutdownHook shutdown-hook}, or the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   825
     * {@link java.io.File#deleteOnExit} mechanism may be used to delete the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   826
     * file automatically.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   827
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   828
     * <p> The {@code attrs} parameter is optional {@link FileAttribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   829
     * file-attributes} to set atomically when creating the file. Each attribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   830
     * is identified by its {@link FileAttribute#name name}. If more than one
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   831
     * attribute of the same name is included in the array then all but the last
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   832
     * occurrence is ignored. When no file attributes are specified, then the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   833
     * resulting file may have more restrictive access permissions to files
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   834
     * created by the {@link java.io.File#createTempFile(String,String,File)}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   835
     * method.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   836
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   837
     * @param   dir
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   838
     *          the path to directory in which to create the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   839
     * @param   prefix
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   840
     *          the prefix string to be used in generating the file's name;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   841
     *          may be {@code null}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   842
     * @param   suffix
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   843
     *          the suffix string to be used in generating the file's name;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   844
     *          may be {@code null}, in which case "{@code .tmp}" is used
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   845
     * @param   attrs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   846
     *          an optional list of file attributes to set atomically when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   847
     *          creating the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   848
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   849
     * @return  the path to the newly created file that did not exist before
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   850
     *          this method was invoked
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   851
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   852
     * @throws  IllegalArgumentException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   853
     *          if the prefix or suffix parameters cannot be used to generate
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   854
     *          a candidate file name
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   855
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   856
     *          if the array contains an attribute that cannot be set atomically
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   857
     *          when creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   858
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   859
     *          if an I/O error occurs or {@code dir} does not exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   860
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   861
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   862
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   863
     *          method is invoked to check write access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   864
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   865
    public static Path createTempFile(Path dir,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   866
                                      String prefix,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   867
                                      String suffix,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   868
                                      FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   869
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   870
    {
8558
e51c07113d09 7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents: 8539
diff changeset
   871
        return TempFileHelper.createTempFile(Objects.requireNonNull(dir),
e51c07113d09 7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents: 8539
diff changeset
   872
                                             prefix, suffix, attrs);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   873
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   874
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   875
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   876
     * Creates an empty file in the default temporary-file directory, using
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   877
     * the given prefix and suffix to generate its name. The resulting {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   878
     * Path} is associated with the default {@code FileSystem}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   879
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   880
     * <p> This method works in exactly the manner specified by the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   881
     * {@link #createTempFile(Path,String,String,FileAttribute[])} method for
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   882
     * the case that the {@code dir} parameter is the temporary-file directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   883
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   884
     * @param   prefix
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   885
     *          the prefix string to be used in generating the file's name;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   886
     *          may be {@code null}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   887
     * @param   suffix
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   888
     *          the suffix string to be used in generating the file's name;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   889
     *          may be {@code null}, in which case "{@code .tmp}" is used
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   890
     * @param   attrs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   891
     *          an optional list of file attributes to set atomically when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   892
     *          creating the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   893
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   894
     * @return  the path to the newly created file that did not exist before
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   895
     *          this method was invoked
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   896
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   897
     * @throws  IllegalArgumentException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   898
     *          if the prefix or suffix parameters cannot be used to generate
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   899
     *          a candidate file name
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   900
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   901
     *          if the array contains an attribute that cannot be set atomically
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   902
     *          when creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   903
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   904
     *          if an I/O error occurs or the temporary-file directory does not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   905
     *          exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   906
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   907
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   908
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   909
     *          method is invoked to check write access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   910
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   911
    public static Path createTempFile(String prefix,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   912
                                      String suffix,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   913
                                      FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   914
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   915
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   916
        return TempFileHelper.createTempFile(null, prefix, suffix, attrs);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   917
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   918
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   919
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   920
     * Creates a new directory in the specified directory, using the given
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   921
     * prefix to generate its name.  The resulting {@code Path} is associated
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   922
     * with the same {@code FileSystem} as the given directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   923
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   924
     * <p> The details as to how the name of the directory is constructed is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   925
     * implementation dependent and therefore not specified. Where possible
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   926
     * the {@code prefix} is used to construct candidate names.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   927
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   928
     * <p> As with the {@code createTempFile} methods, this method is only
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   929
     * part of a temporary-file facility. A {@link Runtime#addShutdownHook
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   930
     * shutdown-hook}, or the {@link java.io.File#deleteOnExit} mechanism may be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   931
     * used to delete the directory automatically.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   932
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   933
     * <p> The {@code attrs} parameter is optional {@link FileAttribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   934
     * file-attributes} to set atomically when creating the directory. Each
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   935
     * attribute is identified by its {@link FileAttribute#name name}. If more
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   936
     * than one attribute of the same name is included in the array then all but
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   937
     * the last occurrence is ignored.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   938
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   939
     * @param   dir
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   940
     *          the path to directory in which to create the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   941
     * @param   prefix
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   942
     *          the prefix string to be used in generating the directory's name;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   943
     *          may be {@code null}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   944
     * @param   attrs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   945
     *          an optional list of file attributes to set atomically when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   946
     *          creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   947
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   948
     * @return  the path to the newly created directory that did not exist before
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   949
     *          this method was invoked
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   950
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   951
     * @throws  IllegalArgumentException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   952
     *          if the prefix cannot be used to generate a candidate directory name
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   953
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   954
     *          if the array contains an attribute that cannot be set atomically
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   955
     *          when creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   956
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   957
     *          if an I/O error occurs or {@code dir} does not exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   958
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   959
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   960
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   961
     *          method is invoked to check write access when creating the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   962
     *          directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   963
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   964
    public static Path createTempDirectory(Path dir,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   965
                                           String prefix,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   966
                                           FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   967
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   968
    {
8558
e51c07113d09 7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents: 8539
diff changeset
   969
        return TempFileHelper.createTempDirectory(Objects.requireNonNull(dir),
e51c07113d09 7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents: 8539
diff changeset
   970
                                                  prefix, attrs);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   971
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   972
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   973
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   974
     * Creates a new directory in the default temporary-file directory, using
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8166
diff changeset
   975
     * the given prefix to generate its name. The resulting {@code Path} is
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8166
diff changeset
   976
     * associated with the default {@code FileSystem}.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   977
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   978
     * <p> This method works in exactly the manner specified by {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   979
     * #createTempDirectory(Path,String,FileAttribute[])} method for the case
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   980
     * that the {@code dir} parameter is the temporary-file directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   981
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   982
     * @param   prefix
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   983
     *          the prefix string to be used in generating the directory's name;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   984
     *          may be {@code null}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   985
     * @param   attrs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   986
     *          an optional list of file attributes to set atomically when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   987
     *          creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   988
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   989
     * @return  the path to the newly created directory that did not exist before
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   990
     *          this method was invoked
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   991
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   992
     * @throws  IllegalArgumentException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   993
     *          if the prefix cannot be used to generate a candidate directory name
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   994
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   995
     *          if the array contains an attribute that cannot be set atomically
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   996
     *          when creating the directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   997
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   998
     *          if an I/O error occurs or the temporary-file directory does not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
   999
     *          exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1000
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1001
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1002
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1003
     *          method is invoked to check write access when creating the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1004
     *          directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1005
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1006
    public static Path createTempDirectory(String prefix,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1007
                                           FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1008
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1009
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1010
        return TempFileHelper.createTempDirectory(null, prefix, attrs);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1011
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1012
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1013
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1014
     * Creates a symbolic link to a target <i>(optional operation)</i>.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1015
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1016
     * <p> The {@code target} parameter is the target of the link. It may be an
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1017
     * {@link Path#isAbsolute absolute} or relative path and may not exist. When
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1018
     * the target is a relative path then file system operations on the resulting
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1019
     * link are relative to the path of the link.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1020
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1021
     * <p> The {@code attrs} parameter is optional {@link FileAttribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1022
     * attributes} to set atomically when creating the link. Each attribute is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1023
     * identified by its {@link FileAttribute#name name}. If more than one attribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1024
     * of the same name is included in the array then all but the last occurrence
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1025
     * is ignored.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1026
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1027
     * <p> Where symbolic links are supported, but the underlying {@link FileStore}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1028
     * does not support symbolic links, then this may fail with an {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1029
     * IOException}. Additionally, some operating systems may require that the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1030
     * Java virtual machine be started with implementation specific privileges to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1031
     * create symbolic links, in which case this method may throw {@code IOException}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1032
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1033
     * @param   link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1034
     *          the path of the symbolic link to create
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1035
     * @param   target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1036
     *          the target of the symbolic link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1037
     * @param   attrs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1038
     *          the array of attributes to set atomically when creating the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1039
     *          symbolic link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1040
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1041
     * @return  the path to the symbolic link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1042
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1043
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1044
     *          if the implementation does not support symbolic links or the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1045
     *          array contains an attribute that cannot be set atomically when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1046
     *          creating the symbolic link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1047
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1048
     *          if a file with the name already exists <i>(optional specific
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1049
     *          exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1050
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1051
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1052
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1053
     *          In the case of the default provider, and a security manager
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  1054
     *          is installed, it denies {@link LinkPermission}{@code ("symbolic")}
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1055
     *          or its {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1056
     *          method denies write access to the path of the symbolic link.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1057
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1058
    public static Path createSymbolicLink(Path link, Path target,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1059
                                          FileAttribute<?>... attrs)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1060
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1061
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1062
        provider(link).createSymbolicLink(link, target, attrs);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1063
        return link;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1064
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1065
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1066
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1067
     * Creates a new link (directory entry) for an existing file <i>(optional
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1068
     * operation)</i>.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1069
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1070
     * <p> The {@code link} parameter locates the directory entry to create.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1071
     * The {@code existing} parameter is the path to an existing file. This
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1072
     * method creates a new directory entry for the file so that it can be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1073
     * accessed using {@code link} as the path. On some file systems this is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1074
     * known as creating a "hard link". Whether the file attributes are
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1075
     * maintained for the file or for each directory entry is file system
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1076
     * specific and therefore not specified. Typically, a file system requires
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1077
     * that all links (directory entries) for a file be on the same file system.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1078
     * Furthermore, on some platforms, the Java virtual machine may require to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1079
     * be started with implementation specific privileges to create hard links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1080
     * or to create links to directories.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1081
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1082
     * @param   link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1083
     *          the link (directory entry) to create
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1084
     * @param   existing
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1085
     *          a path to an existing file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1086
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1087
     * @return  the path to the link (directory entry)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1088
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1089
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1090
     *          if the implementation does not support adding an existing file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1091
     *          to a directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1092
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1093
     *          if the entry could not otherwise be created because a file of
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1094
     *          that name already exists <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1095
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1096
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1097
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1098
     *          In the case of the default provider, and a security manager
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  1099
     *          is installed, it denies {@link LinkPermission}{@code ("hard")}
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1100
     *          or its {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1101
     *          method denies write access to either the link or the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1102
     *          existing file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1103
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1104
    public static Path createLink(Path link, Path existing) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1105
        provider(link).createLink(link, existing);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1106
        return link;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1107
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1108
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1109
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1110
     * Deletes a file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1111
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1112
     * <p> An implementation may require to examine the file to determine if the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1113
     * file is a directory. Consequently this method may not be atomic with respect
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1114
     * to other file system operations.  If the file is a symbolic link then the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1115
     * symbolic link itself, not the final target of the link, is deleted.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1116
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1117
     * <p> If the file is a directory then the directory must be empty. In some
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1118
     * implementations a directory has entries for special files or links that
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1119
     * are created when the directory is created. In such implementations a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1120
     * directory is considered empty when only the special entries exist.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1121
     * This method can be used with the {@link #walkFileTree walkFileTree}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1122
     * method to delete a directory and all entries in the directory, or an
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1123
     * entire <i>file-tree</i> where required.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1124
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1125
     * <p> On some operating systems it may not be possible to remove a file when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1126
     * it is open and in use by this Java virtual machine or other programs.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1127
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1128
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1129
     *          the path to the file to delete
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1130
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1131
     * @throws  NoSuchFileException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1132
     *          if the file does not exist <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1133
     * @throws  DirectoryNotEmptyException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1134
     *          if the file is a directory and could not otherwise be deleted
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1135
     *          because the directory is not empty <i>(optional specific
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1136
     *          exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1137
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1138
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1139
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1140
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1141
     *          installed, the {@link SecurityManager#checkDelete(String)} method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1142
     *          is invoked to check delete access to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1143
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1144
    public static void delete(Path path) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1145
        provider(path).delete(path);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1146
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1147
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1148
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1149
     * Deletes a file if it exists.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1150
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1151
     * <p> As with the {@link #delete(Path) delete(Path)} method, an
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1152
     * implementation may need to examine the file to determine if the file is a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1153
     * directory. Consequently this method may not be atomic with respect to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1154
     * other file system operations.  If the file is a symbolic link, then the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1155
     * symbolic link itself, not the final target of the link, is deleted.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1156
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1157
     * <p> If the file is a directory then the directory must be empty. In some
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1158
     * implementations a directory has entries for special files or links that
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1159
     * are created when the directory is created. In such implementations a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1160
     * directory is considered empty when only the special entries exist.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1161
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1162
     * <p> On some operating systems it may not be possible to remove a file when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1163
     * it is open and in use by this Java virtual machine or other programs.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1164
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1165
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1166
     *          the path to the file to delete
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1167
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1168
     * @return  {@code true} if the file was deleted by this method; {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1169
     *          false} if the file could not be deleted because it did not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1170
     *          exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1171
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1172
     * @throws  DirectoryNotEmptyException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1173
     *          if the file is a directory and could not otherwise be deleted
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1174
     *          because the directory is not empty <i>(optional specific
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1175
     *          exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1176
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1177
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1178
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1179
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1180
     *          installed, the {@link SecurityManager#checkDelete(String)} method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1181
     *          is invoked to check delete access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1182
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1183
    public static boolean deleteIfExists(Path path) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1184
        return provider(path).deleteIfExists(path);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1185
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1186
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1187
    // -- Copying and moving files --
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1188
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1189
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1190
     * Copy a file to a target file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1191
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1192
     * <p> This method copies a file to the target file with the {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1193
     * options} parameter specifying how the copy is performed. By default, the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1194
     * copy fails if the target file already exists or is a symbolic link,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1195
     * except if the source and target are the {@link #isSameFile same} file, in
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1196
     * which case the method completes without copying the file. File attributes
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1197
     * are not required to be copied to the target file. If symbolic links are
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1198
     * supported, and the file is a symbolic link, then the final target of the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1199
     * link is copied. If the file is a directory then it creates an empty
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1200
     * directory in the target location (entries in the directory are not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1201
     * copied). This method can be used with the {@link #walkFileTree
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1202
     * walkFileTree} method to copy a directory and all entries in the directory,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1203
     * or an entire <i>file-tree</i> where required.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1204
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1205
     * <p> The {@code options} parameter may include any of the following:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1206
     *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1207
     * <table class="striped">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1208
     * <caption style="display:none">Options</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1209
     * <thead>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
  1210
     * <tr> <th scope="col">Option</th> <th scope="col">Description</th> </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1211
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1212
     * <tbody>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1213
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
  1214
     *   <th scope="row"> {@link StandardCopyOption#REPLACE_EXISTING REPLACE_EXISTING} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1215
     *   <td> If the target file exists, then the target file is replaced if it
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1216
     *     is not a non-empty directory. If the target file exists and is a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1217
     *     symbolic link, then the symbolic link itself, not the target of
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1218
     *     the link, is replaced. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1219
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1220
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
  1221
     *   <th scope="row"> {@link StandardCopyOption#COPY_ATTRIBUTES COPY_ATTRIBUTES} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1222
     *   <td> Attempts to copy the file attributes associated with this file to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1223
     *     the target file. The exact file attributes that are copied is platform
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1224
     *     and file system dependent and therefore unspecified. Minimally, the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1225
     *     {@link BasicFileAttributes#lastModifiedTime last-modified-time} is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1226
     *     copied to the target file if supported by both the source and target
16048
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1227
     *     file stores. Copying of file timestamps may result in precision
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1228
     *     loss. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1229
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1230
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
  1231
     *   <th scope="row"> {@link LinkOption#NOFOLLOW_LINKS NOFOLLOW_LINKS} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1232
     *   <td> Symbolic links are not followed. If the file is a symbolic link,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1233
     *     then the symbolic link itself, not the target of the link, is copied.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1234
     *     It is implementation specific if file attributes can be copied to the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1235
     *     new link. In other words, the {@code COPY_ATTRIBUTES} option may be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1236
     *     ignored when copying a symbolic link. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1237
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1238
     * </tbody>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1239
     * </table>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1240
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1241
     * <p> An implementation of this interface may support additional
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1242
     * implementation specific options.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1243
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1244
     * <p> Copying a file is not an atomic operation. If an {@link IOException}
16048
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1245
     * is thrown, then it is possible that the target file is incomplete or some
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1246
     * of its file attributes have not been copied from the source file. When
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1247
     * the {@code REPLACE_EXISTING} option is specified and the target file
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1248
     * exists, then the target file is replaced. The check for the existence of
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1249
     * the file and the creation of the new file may not be atomic with respect
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1250
     * to other file system activities.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1251
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1252
     * <p> <b>Usage Example:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1253
     * Suppose we want to copy a file into a directory, giving it the same file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1254
     * name as the source file:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1255
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1256
     *     Path source = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1257
     *     Path newdir = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1258
     *     Files.copy(source, newdir.resolve(source.getFileName());
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1259
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1260
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1261
     * @param   source
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1262
     *          the path to the file to copy
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1263
     * @param   target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1264
     *          the path to the target file (may be associated with a different
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1265
     *          provider to the source path)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1266
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1267
     *          options specifying how the copy should be done
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1268
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1269
     * @return  the path to the target file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1270
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1271
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1272
     *          if the array contains a copy option that is not supported
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1273
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1274
     *          if the target file exists but cannot be replaced because the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1275
     *          {@code REPLACE_EXISTING} option is not specified <i>(optional
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1276
     *          specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1277
     * @throws  DirectoryNotEmptyException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1278
     *          the {@code REPLACE_EXISTING} option is specified but the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1279
     *          cannot be replaced because it is a non-empty directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1280
     *          <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1281
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1282
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1283
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1284
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1285
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1286
     *          method is invoked to check read access to the source file, the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1287
     *          {@link SecurityManager#checkWrite(String) checkWrite} is invoked
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1288
     *          to check write access to the target file. If a symbolic link is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1289
     *          copied the security manager is invoked to check {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1290
     *          LinkPermission}{@code ("symbolic")}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1291
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1292
    public static Path copy(Path source, Path target, CopyOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1293
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1294
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1295
        FileSystemProvider provider = provider(source);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1296
        if (provider(target) == provider) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1297
            // same provider
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1298
            provider.copy(source, target, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1299
        } else {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1300
            // different providers
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1301
            CopyMoveHelper.copyToForeignTarget(source, target, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1302
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1303
        return target;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1304
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1305
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1306
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1307
     * Move or rename a file to a target file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1308
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1309
     * <p> By default, this method attempts to move the file to the target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1310
     * file, failing if the target file exists except if the source and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1311
     * target are the {@link #isSameFile same} file, in which case this method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1312
     * has no effect. If the file is a symbolic link then the symbolic link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1313
     * itself, not the target of the link, is moved. This method may be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1314
     * invoked to move an empty directory. In some implementations a directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1315
     * has entries for special files or links that are created when the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1316
     * directory is created. In such implementations a directory is considered
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1317
     * empty when only the special entries exist. When invoked to move a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1318
     * directory that is not empty then the directory is moved if it does not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1319
     * require moving the entries in the directory.  For example, renaming a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1320
     * directory on the same {@link FileStore} will usually not require moving
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1321
     * the entries in the directory. When moving a directory requires that its
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1322
     * entries be moved then this method fails (by throwing an {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1323
     * IOException}). To move a <i>file tree</i> may involve copying rather
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1324
     * than moving directories and this can be done using the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1325
     * #copy copy} method in conjunction with the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1326
     * #walkFileTree Files.walkFileTree} utility method.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1327
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1328
     * <p> The {@code options} parameter may include any of the following:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1329
     *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1330
     * <table class="striped">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1331
     * <caption style="display:none">Options</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1332
     * <thead>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
  1333
     * <tr> <th scope="col">Option</th> <th scope="col">Description</th> </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1334
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1335
     * <tbody>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1336
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
  1337
     *   <th scope="row"> {@link StandardCopyOption#REPLACE_EXISTING REPLACE_EXISTING} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1338
     *   <td> If the target file exists, then the target file is replaced if it
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1339
     *     is not a non-empty directory. If the target file exists and is a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1340
     *     symbolic link, then the symbolic link itself, not the target of
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1341
     *     the link, is replaced. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1342
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1343
     * <tr>
45881
aaec0fbe17ae 8184208: update class="striped" tables for accessibility
jjg
parents: 45124
diff changeset
  1344
     *   <th scope="row"> {@link StandardCopyOption#ATOMIC_MOVE ATOMIC_MOVE} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1345
     *   <td> The move is performed as an atomic file system operation and all
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1346
     *     other options are ignored. If the target file exists then it is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1347
     *     implementation specific if the existing file is replaced or this method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1348
     *     fails by throwing an {@link IOException}. If the move cannot be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1349
     *     performed as an atomic file system operation then {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1350
     *     AtomicMoveNotSupportedException} is thrown. This can arise, for
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1351
     *     example, when the target location is on a different {@code FileStore}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1352
     *     and would require that the file be copied, or target location is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1353
     *     associated with a different provider to this object. </td>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  1354
     * </tbody>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1355
     * </table>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1356
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1357
     * <p> An implementation of this interface may support additional
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1358
     * implementation specific options.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1359
     *
16048
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1360
     * <p> Moving a file will copy the {@link
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1361
     * BasicFileAttributes#lastModifiedTime last-modified-time} to the target
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1362
     * file if supported by both source and target file stores. Copying of file
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1363
     * timestamps may result in precision loss. An implementation may also
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1364
     * attempt to copy other file attributes but is not required to fail if the
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1365
     * file attributes cannot be copied. When the move is performed as
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1366
     * a non-atomic operation, and an {@code IOException} is thrown, then the
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1367
     * state of the files is not defined. The original file and the target file
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1368
     * may both exist, the target file may be incomplete or some of its file
e8891a5d639f 8006645: TEST_BUG: java/nio/file/Files/CopyAndMove.java failing intermittently (sol)
dxu
parents: 14342
diff changeset
  1369
     * attributes may not been copied from the original file.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1370
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1371
     * <p> <b>Usage Examples:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1372
     * Suppose we want to rename a file to "newname", keeping the file in the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1373
     * same directory:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1374
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1375
     *     Path source = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1376
     *     Files.move(source, source.resolveSibling("newname"));
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1377
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1378
     * Alternatively, suppose we want to move a file to new directory, keeping
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1379
     * the same file name, and replacing any existing file of that name in the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1380
     * directory:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1381
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1382
     *     Path source = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1383
     *     Path newdir = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1384
     *     Files.move(source, newdir.resolve(source.getFileName()), REPLACE_EXISTING);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1385
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1386
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1387
     * @param   source
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1388
     *          the path to the file to move
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1389
     * @param   target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1390
     *          the path to the target file (may be associated with a different
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1391
     *          provider to the source path)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1392
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1393
     *          options specifying how the move should be done
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1394
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1395
     * @return  the path to the target file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1396
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1397
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1398
     *          if the array contains a copy option that is not supported
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1399
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1400
     *          if the target file exists but cannot be replaced because the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1401
     *          {@code REPLACE_EXISTING} option is not specified <i>(optional
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1402
     *          specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1403
     * @throws  DirectoryNotEmptyException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1404
     *          the {@code REPLACE_EXISTING} option is specified but the file
50447
3111982511ee 8201407: Files.move throws DirectoryNonEmptyException when moving directory across file system
bpb
parents: 48353
diff changeset
  1405
     *          cannot be replaced because it is a non-empty directory, or the
3111982511ee 8201407: Files.move throws DirectoryNonEmptyException when moving directory across file system
bpb
parents: 48353
diff changeset
  1406
     *          source is a non-empty directory containing entries that would
3111982511ee 8201407: Files.move throws DirectoryNonEmptyException when moving directory across file system
bpb
parents: 48353
diff changeset
  1407
     *          be required to be moved <i>(optional specific exceptions)</i>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1408
     * @throws  AtomicMoveNotSupportedException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1409
     *          if the options array contains the {@code ATOMIC_MOVE} option but
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1410
     *          the file cannot be moved as an atomic file system operation.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1411
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1412
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1413
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1414
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1415
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1416
     *          method is invoked to check write access to both the source and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1417
     *          target file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1418
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1419
    public static Path move(Path source, Path target, CopyOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1420
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1421
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1422
        FileSystemProvider provider = provider(source);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1423
        if (provider(target) == provider) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1424
            // same provider
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1425
            provider.move(source, target, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1426
        } else {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1427
            // different providers
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1428
            CopyMoveHelper.moveToForeignTarget(source, target, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1429
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1430
        return target;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1431
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1432
23887
7b2fb8d5f6be 8040262: (fs) Misc. typos in comments and implementation
alanb
parents: 22351
diff changeset
  1433
    // -- Miscellaneous --
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1434
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1435
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1436
     * Reads the target of a symbolic link <i>(optional operation)</i>.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1437
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1438
     * <p> If the file system supports <a href="package-summary.html#links">symbolic
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1439
     * links</a> then this method is used to read the target of the link, failing
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1440
     * if the file is not a symbolic link. The target of the link need not exist.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1441
     * The returned {@code Path} object will be associated with the same file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1442
     * system as {@code link}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1443
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1444
     * @param   link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1445
     *          the path to the symbolic link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1446
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1447
     * @return  a {@code Path} object representing the target of the link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1448
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1449
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1450
     *          if the implementation does not support symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1451
     * @throws  NotLinkException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1452
     *          if the target could otherwise not be read because the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1453
     *          is not a symbolic link <i>(optional specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1454
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1455
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1456
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1457
     *          In the case of the default provider, and a security manager
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1458
     *          is installed, it checks that {@code FilePermission} has been
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1459
     *          granted with the "{@code readlink}" action to read the link.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1460
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1461
    public static Path readSymbolicLink(Path link) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1462
        return provider(link).readSymbolicLink(link);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1463
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1464
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1465
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1466
     * Returns the {@link FileStore} representing the file store where a file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1467
     * is located.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1468
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1469
     * <p> Once a reference to the {@code FileStore} is obtained it is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1470
     * implementation specific if operations on the returned {@code FileStore},
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1471
     * or {@link FileStoreAttributeView} objects obtained from it, continue
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1472
     * to depend on the existence of the file. In particular the behavior is not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1473
     * defined for the case that the file is deleted or moved to a different
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1474
     * file store.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1475
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1476
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1477
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1478
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1479
     * @return  the file store where the file is stored
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1480
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1481
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1482
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1483
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1484
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1485
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1486
     *          method is invoked to check read access to the file, and in
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  1487
     *          addition it checks
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  1488
     *          {@link RuntimePermission}{@code ("getFileStoreAttributes")}
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1489
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1490
    public static FileStore getFileStore(Path path) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1491
        return provider(path).getFileStore(path);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1492
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1493
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1494
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1495
     * Tests if two paths locate the same file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1496
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1497
     * <p> If both {@code Path} objects are {@link Path#equals(Object) equal}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1498
     * then this method returns {@code true} without checking if the file exists.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1499
     * If the two {@code Path} objects are associated with different providers
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1500
     * then this method returns {@code false}. Otherwise, this method checks if
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1501
     * both {@code Path} objects locate the same file, and depending on the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1502
     * implementation, may require to open or access both files.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1503
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1504
     * <p> If the file system and files remain static, then this method implements
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1505
     * an equivalence relation for non-null {@code Paths}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1506
     * <ul>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1507
     * <li>It is <i>reflexive</i>: for {@code Path} {@code f},
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1508
     *     {@code isSameFile(f,f)} should return {@code true}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1509
     * <li>It is <i>symmetric</i>: for two {@code Paths} {@code f} and {@code g},
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1510
     *     {@code isSameFile(f,g)} will equal {@code isSameFile(g,f)}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1511
     * <li>It is <i>transitive</i>: for three {@code Paths}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1512
     *     {@code f}, {@code g}, and {@code h}, if {@code isSameFile(f,g)} returns
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1513
     *     {@code true} and {@code isSameFile(g,h)} returns {@code true}, then
26960
d280345c2cfb 8059840: (bb) Typo in javadoc for ByteBuffer.wrap()
igerasim
parents: 25859
diff changeset
  1514
     *     {@code isSameFile(f,h)} will return {@code true}.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1515
     * </ul>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1516
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1517
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1518
     *          one path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1519
     * @param   path2
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1520
     *          the other path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1521
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1522
     * @return  {@code true} if, and only if, the two paths locate the same file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1523
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1524
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1525
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1526
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1527
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1528
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1529
     *          method is invoked to check read access to both files.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1530
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1531
     * @see java.nio.file.attribute.BasicFileAttributes#fileKey
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1532
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1533
    public static boolean isSameFile(Path path, Path path2) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1534
        return provider(path).isSameFile(path, path2);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1535
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1536
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1537
    /**
52458
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1538
     * Finds and returns the position of the first mismatched byte in the content
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1539
     * of two files, or {@code -1L} if there is no mismatch. The position will be
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1540
     * in the inclusive range of {@code 0L} up to the size (in bytes) of the
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1541
     * smaller file.
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1542
     *
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1543
     * <p> Two files are considered to match if they satisfy one of the following
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1544
     * conditions:
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1545
     * <ul>
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1546
     * <li> The two paths locate the {@linkplain #isSameFile(Path, Path) same file},
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1547
     *      even if two {@linkplain Path#equals(Object) equal} paths locate a file
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1548
     *      does not exist, or </li>
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1549
     * <li> The two files are the same size, and every byte in the first file
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1550
     *      is identical to the corresponding byte in the second file. </li>
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1551
     * </ul>
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1552
     *
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1553
     * <p> Otherwise there is a mismatch between the two files and the value
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1554
     * returned by this method is:
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1555
     * <ul>
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1556
     * <li> The position of the first mismatched byte, or </li>
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1557
     * <li> The size of the smaller file (in bytes) when the files are different
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1558
     *      sizes and every byte of the smaller file is identical to the
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1559
     *      corresponding byte of the larger file. </li>
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1560
     * </ul>
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1561
     *
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1562
     * <p> This method may not be atomic with respect to other file system
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1563
     * operations. This method is always <i>reflexive</i> (for {@code Path f},
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1564
     * {@code mismatch(f,f)} returns {@code -1L}). If the file system and files
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1565
     * remain static, then this method is <i>symmetric</i> (for two {@code Paths f}
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1566
     * and {@code g}, {@code mismatch(f,g)} will return the same value as
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1567
     * {@code mismatch(g,f)}).
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1568
     *
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1569
     * @param   path
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1570
     *          the path to the first file
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1571
     * @param   path2
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1572
     *          the path to the second file
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1573
     *
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1574
     * @return  the position of the first mismatch or {@code -1L} if no mismatch
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1575
     *
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1576
     * @throws  IOException
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1577
     *          if an I/O error occurs
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1578
     * @throws  SecurityException
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1579
     *          In the case of the default provider, and a security manager is
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1580
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1581
     *          method is invoked to check read access to both files.
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1582
     *
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1583
     * @since 12
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1584
     */
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1585
    public static long mismatch(Path path, Path path2) throws IOException {
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1586
        if (isSameFile(path, path2)) {
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1587
            return -1;
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1588
        }
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1589
        byte[] buffer1 = new byte[BUFFER_SIZE];
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1590
        byte[] buffer2 = new byte[BUFFER_SIZE];
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1591
        try (InputStream in1 = Files.newInputStream(path);
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1592
             InputStream in2 = Files.newInputStream(path2);) {
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1593
            long totalRead = 0;
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1594
            while (true) {
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1595
                int nRead1 = in1.readNBytes(buffer1, 0, BUFFER_SIZE);
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1596
                int nRead2 = in2.readNBytes(buffer2, 0, BUFFER_SIZE);
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1597
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1598
                int i = Arrays.mismatch(buffer1, 0, nRead1, buffer2, 0, nRead2);
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1599
                if (i > -1) {
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1600
                    return totalRead + i;
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1601
                }
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1602
                if (nRead1 < BUFFER_SIZE) {
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1603
                    // we've reached the end of the files, but found no mismatch
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1604
                    return -1;
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1605
                }
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1606
                totalRead += nRead1;
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1607
            }
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1608
        }
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1609
    }
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1610
66a0e6b3ec1a 8202285: (fs) Add a method to Files for comparing file contents
joehw
parents: 52427
diff changeset
  1611
    /**
53388
8c08552a1fbd 8215467: Files.isHidden should return true for hidden directories on Windows
bpb
parents: 52700
diff changeset
  1612
     * Tells whether or not a file is considered <em>hidden</em>.
8c08552a1fbd 8215467: Files.isHidden should return true for hidden directories on Windows
bpb
parents: 52700
diff changeset
  1613
     *
8c08552a1fbd 8215467: Files.isHidden should return true for hidden directories on Windows
bpb
parents: 52700
diff changeset
  1614
     * @apiNote
8c08552a1fbd 8215467: Files.isHidden should return true for hidden directories on Windows
bpb
parents: 52700
diff changeset
  1615
     * The exact definition of hidden is platform or provider dependent. On UNIX
8c08552a1fbd 8215467: Files.isHidden should return true for hidden directories on Windows
bpb
parents: 52700
diff changeset
  1616
     * for example a file is considered to be hidden if its name begins with a
8c08552a1fbd 8215467: Files.isHidden should return true for hidden directories on Windows
bpb
parents: 52700
diff changeset
  1617
     * period character ('.'). On Windows a file is considered hidden if the DOS
8c08552a1fbd 8215467: Files.isHidden should return true for hidden directories on Windows
bpb
parents: 52700
diff changeset
  1618
     * {@link DosFileAttributes#isHidden hidden} attribute is set.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1619
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1620
     * <p> Depending on the implementation this method may require to access
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1621
     * the file system to determine if the file is considered hidden.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1622
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1623
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1624
     *          the path to the file to test
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1625
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1626
     * @return  {@code true} if the file is considered hidden
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1627
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1628
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1629
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1630
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1631
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1632
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1633
     *          method is invoked to check read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1634
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1635
    public static boolean isHidden(Path path) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1636
        return provider(path).isHidden(path);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1637
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1638
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1639
    // lazy loading of default and installed file type detectors
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1640
    private static class FileTypeDetectors{
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1641
        static final FileTypeDetector defaultFileTypeDetector =
18185
607d2fb48f47 8004288: (fs) Files.probeContentType problems
alanb
parents: 16048
diff changeset
  1642
            createDefaultFileTypeDetector();
23887
7b2fb8d5f6be 8040262: (fs) Misc. typos in comments and implementation
alanb
parents: 22351
diff changeset
  1643
        static final List<FileTypeDetector> installedDetectors =
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1644
            loadInstalledDetectors();
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1645
18185
607d2fb48f47 8004288: (fs) Files.probeContentType problems
alanb
parents: 16048
diff changeset
  1646
        // creates the default file type detector
607d2fb48f47 8004288: (fs) Files.probeContentType problems
alanb
parents: 16048
diff changeset
  1647
        private static FileTypeDetector createDefaultFileTypeDetector() {
607d2fb48f47 8004288: (fs) Files.probeContentType problems
alanb
parents: 16048
diff changeset
  1648
            return AccessController
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29269
diff changeset
  1649
                .doPrivileged(new PrivilegedAction<>() {
18185
607d2fb48f47 8004288: (fs) Files.probeContentType problems
alanb
parents: 16048
diff changeset
  1650
                    @Override public FileTypeDetector run() {
607d2fb48f47 8004288: (fs) Files.probeContentType problems
alanb
parents: 16048
diff changeset
  1651
                        return sun.nio.fs.DefaultFileTypeDetector.create();
607d2fb48f47 8004288: (fs) Files.probeContentType problems
alanb
parents: 16048
diff changeset
  1652
                }});
607d2fb48f47 8004288: (fs) Files.probeContentType problems
alanb
parents: 16048
diff changeset
  1653
        }
607d2fb48f47 8004288: (fs) Files.probeContentType problems
alanb
parents: 16048
diff changeset
  1654
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1655
        // loads all installed file type detectors
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1656
        private static List<FileTypeDetector> loadInstalledDetectors() {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1657
            return AccessController
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29269
diff changeset
  1658
                .doPrivileged(new PrivilegedAction<>() {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1659
                    @Override public List<FileTypeDetector> run() {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1660
                        List<FileTypeDetector> list = new ArrayList<>();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1661
                        ServiceLoader<FileTypeDetector> loader = ServiceLoader
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1662
                            .load(FileTypeDetector.class, ClassLoader.getSystemClassLoader());
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1663
                        for (FileTypeDetector detector: loader) {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1664
                            list.add(detector);
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1665
                        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1666
                        return list;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1667
                }});
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1668
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1669
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1670
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1671
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1672
     * Probes the content type of a file.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1673
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1674
     * <p> This method uses the installed {@link FileTypeDetector} implementations
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1675
     * to probe the given file to determine its content type. Each file type
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1676
     * detector's {@link FileTypeDetector#probeContentType probeContentType} is
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1677
     * invoked, in turn, to probe the file type. If the file is recognized then
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1678
     * the content type is returned. If the file is not recognized by any of the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1679
     * installed file type detectors then a system-default file type detector is
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1680
     * invoked to guess the content type.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1681
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1682
     * <p> A given invocation of the Java virtual machine maintains a system-wide
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1683
     * list of file type detectors. Installed file type detectors are loaded
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1684
     * using the service-provider loading facility defined by the {@link ServiceLoader}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1685
     * class. Installed file type detectors are loaded using the system class
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35717
diff changeset
  1686
     * loader. If the system class loader cannot be found then the platform class
9d0388c6b336 8142968: Module System implementation
alanb
parents: 35717
diff changeset
  1687
     * loader is used. File type detectors are typically installed
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27339
diff changeset
  1688
     * by placing them in a JAR file on the application class path,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 27339
diff changeset
  1689
     * the JAR file contains a provider-configuration file
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1690
     * named {@code java.nio.file.spi.FileTypeDetector} in the resource directory
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1691
     * {@code META-INF/services}, and the file lists one or more fully-qualified
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1692
     * names of concrete subclass of {@code FileTypeDetector } that have a zero
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1693
     * argument constructor. If the process of locating or instantiating the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1694
     * installed file type detectors fails then an unspecified error is thrown.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1695
     * The ordering that installed providers are located is implementation
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1696
     * specific.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1697
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1698
     * <p> The return value of this method is the string form of the value of a
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1699
     * Multipurpose Internet Mail Extension (MIME) content type as
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1700
     * defined by <a href="http://www.ietf.org/rfc/rfc2045.txt"><i>RFC&nbsp;2045:
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1701
     * Multipurpose Internet Mail Extensions (MIME) Part One: Format of Internet
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1702
     * Message Bodies</i></a>. The string is guaranteed to be parsable according
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1703
     * to the grammar in the RFC.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1704
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1705
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1706
     *          the path to the file to probe
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1707
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1708
     * @return  The content type of the file, or {@code null} if the content
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1709
     *          type cannot be determined
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1710
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1711
     * @throws  IOException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1712
     *          if an I/O error occurs
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1713
     * @throws  SecurityException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1714
     *          If a security manager is installed and it denies an unspecified
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1715
     *          permission required by a file type detector implementation.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1716
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1717
    public static String probeContentType(Path path)
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1718
        throws IOException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1719
    {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1720
        // try installed file type detectors
23887
7b2fb8d5f6be 8040262: (fs) Misc. typos in comments and implementation
alanb
parents: 22351
diff changeset
  1721
        for (FileTypeDetector detector: FileTypeDetectors.installedDetectors) {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1722
            String result = detector.probeContentType(path);
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1723
            if (result != null)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1724
                return result;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1725
        }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1726
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  1727
        // fallback to default
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1728
        return FileTypeDetectors.defaultFileTypeDetector.probeContentType(path);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1729
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1730
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1731
    // -- File Attributes --
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1732
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1733
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1734
     * Returns a file attribute view of a given type.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1735
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1736
     * <p> A file attribute view provides a read-only or updatable view of a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1737
     * set of file attributes. This method is intended to be used where the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1738
     * attribute view defines type-safe methods to read or update the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1739
     * attributes. The {@code type} parameter is the type of the attribute view
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1740
     * required and the method returns an instance of that type if supported.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1741
     * The {@link BasicFileAttributeView} type supports access to the basic
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1742
     * attributes of a file. Invoking this method to select a file attribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1743
     * view of that type will always return an instance of that class.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1744
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1745
     * <p> The {@code options} array may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1746
     * are handled by the resulting file attribute view for the case that the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1747
     * file is a symbolic link. By default, symbolic links are followed. If the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1748
     * option {@link LinkOption#NOFOLLOW_LINKS NOFOLLOW_LINKS} is present then
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1749
     * symbolic links are not followed. This option is ignored by implementations
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1750
     * that do not support symbolic links.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1751
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1752
     * <p> <b>Usage Example:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1753
     * Suppose we want read or set a file's ACL, if supported:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1754
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1755
     *     Path path = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1756
     *     AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1757
     *     if (view != null) {
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 12421
diff changeset
  1758
     *         List&lt;AclEntry&gt; acl = view.getAcl();
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1759
     *         :
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1760
     *     }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1761
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1762
     *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  1763
     * @param   <V>
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  1764
     *          The {@code FileAttributeView} type
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1765
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1766
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1767
     * @param   type
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1768
     *          the {@code Class} object corresponding to the file attribute view
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1769
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1770
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1771
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1772
     * @return  a file attribute view of the specified type, or {@code null} if
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1773
     *          the attribute view type is not available
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1774
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1775
    public static <V extends FileAttributeView> V getFileAttributeView(Path path,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1776
                                                                       Class<V> type,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1777
                                                                       LinkOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1778
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1779
        return provider(path).getFileAttributeView(path, type, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1780
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1781
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1782
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1783
     * Reads a file's attributes as a bulk operation.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1784
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1785
     * <p> The {@code type} parameter is the type of the attributes required
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1786
     * and this method returns an instance of that type if supported. All
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1787
     * implementations support a basic set of file attributes and so invoking
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1788
     * this method with a  {@code type} parameter of {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1789
     * BasicFileAttributes.class} will not throw {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1790
     * UnsupportedOperationException}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1791
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1792
     * <p> The {@code options} array may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1793
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1794
     * symbolic links are followed and the file attribute of the final target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1795
     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1796
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1797
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1798
     * <p> It is implementation specific if all file attributes are read as an
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1799
     * atomic operation with respect to other file system operations.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1800
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1801
     * <p> <b>Usage Example:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1802
     * Suppose we want to read a file's attributes in bulk:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1803
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1804
     *    Path path = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1805
     *    BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1806
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1807
     * Alternatively, suppose we want to read file's POSIX attributes without
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1808
     * following symbolic links:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1809
     * <pre>
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  1810
     *    PosixFileAttributes attrs =
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  1811
     *        Files.readAttributes(path, PosixFileAttributes.class, NOFOLLOW_LINKS);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1812
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1813
     *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  1814
     * @param   <A>
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  1815
     *          The {@code BasicFileAttributes} type
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1816
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1817
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1818
     * @param   type
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1819
     *          the {@code Class} of the file attributes required
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1820
     *          to read
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1821
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1822
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1823
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1824
     * @return  the file attributes
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1825
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1826
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1827
     *          if an attributes of the given type are not supported
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1828
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1829
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1830
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1831
     *          In the case of the default provider, a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1832
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1833
     *          method is invoked to check read access to the file. If this
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1834
     *          method is invoked to read security sensitive attributes then the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1835
     *          security manager may be invoke to check for additional permissions.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1836
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1837
    public static <A extends BasicFileAttributes> A readAttributes(Path path,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1838
                                                                   Class<A> type,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1839
                                                                   LinkOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1840
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1841
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1842
        return provider(path).readAttributes(path, type, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1843
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1844
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1845
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1846
     * Sets the value of a file attribute.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1847
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1848
     * <p> The {@code attribute} parameter identifies the attribute to be set
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1849
     * and takes the form:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1850
     * <blockquote>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1851
     * [<i>view-name</i><b>:</b>]<i>attribute-name</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1852
     * </blockquote>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1853
     * where square brackets [...] delineate an optional component and the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1854
     * character {@code ':'} stands for itself.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1855
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1856
     * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1857
     * FileAttributeView} that identifies a set of file attributes. If not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1858
     * specified then it defaults to {@code "basic"}, the name of the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1859
     * attribute view that identifies the basic set of file attributes common to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1860
     * many file systems. <i>attribute-name</i> is the name of the attribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1861
     * within the set.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1862
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1863
     * <p> The {@code options} array may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1864
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1865
     * symbolic links are followed and the file attribute of the final target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1866
     * of the link is set. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1867
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1868
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1869
     * <p> <b>Usage Example:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1870
     * Suppose we want to set the DOS "hidden" attribute:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1871
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1872
     *    Path path = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1873
     *    Files.setAttribute(path, "dos:hidden", true);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1874
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1875
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1876
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1877
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1878
     * @param   attribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1879
     *          the attribute to set
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1880
     * @param   value
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1881
     *          the attribute value
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1882
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1883
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1884
     *
27775
4ee8b208017c 8062955: (fs spec) Files.setLastModifiedTime should specify SecurityException more clearly
alanb
parents: 27339
diff changeset
  1885
     * @return  the given path
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1886
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1887
     * @throws  UnsupportedOperationException
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1888
     *          if the attribute view is not available
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1889
     * @throws  IllegalArgumentException
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1890
     *          if the attribute name is not specified, or is not recognized, or
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1891
     *          the attribute value is of the correct type but has an
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1892
     *          inappropriate value
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1893
     * @throws  ClassCastException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1894
     *          if the attribute value is not of the expected type or is a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1895
     *          collection containing elements that are not of the expected
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1896
     *          type
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1897
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1898
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1899
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1900
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1901
     *          installed, its {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1902
     *          method denies write access to the file. If this method is invoked
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1903
     *          to set security sensitive attributes then the security manager
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1904
     *          may be invoked to check for additional permissions.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1905
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1906
    public static Path setAttribute(Path path, String attribute, Object value,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1907
                                    LinkOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1908
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1909
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1910
        provider(path).setAttribute(path, attribute, value, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1911
        return path;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1912
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1913
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1914
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1915
     * Reads the value of a file attribute.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1916
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1917
     * <p> The {@code attribute} parameter identifies the attribute to be read
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1918
     * and takes the form:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1919
     * <blockquote>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1920
     * [<i>view-name</i><b>:</b>]<i>attribute-name</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1921
     * </blockquote>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1922
     * where square brackets [...] delineate an optional component and the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1923
     * character {@code ':'} stands for itself.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1924
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1925
     * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1926
     * FileAttributeView} that identifies a set of file attributes. If not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1927
     * specified then it defaults to {@code "basic"}, the name of the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1928
     * attribute view that identifies the basic set of file attributes common to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1929
     * many file systems. <i>attribute-name</i> is the name of the attribute.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1930
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1931
     * <p> The {@code options} array may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1932
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1933
     * symbolic links are followed and the file attribute of the final target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1934
     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1935
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1936
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1937
     * <p> <b>Usage Example:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1938
     * Suppose we require the user ID of the file owner on a system that
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1939
     * supports a "{@code unix}" view:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1940
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1941
     *    Path path = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1942
     *    int uid = (Integer)Files.getAttribute(path, "unix:uid");
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1943
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1944
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1945
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1946
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1947
     * @param   attribute
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1948
     *          the attribute to read
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1949
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1950
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1951
     *
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1952
     * @return  the attribute value
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1953
     *
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1954
     * @throws  UnsupportedOperationException
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1955
     *          if the attribute view is not available
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1956
     * @throws  IllegalArgumentException
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1957
     *          if the attribute name is not specified or is not recognized
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1958
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1959
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1960
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1961
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1962
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1963
     *          method denies read access to the file. If this method is invoked
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1964
     *          to read security sensitive attributes then the security manager
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1965
     *          may be invoked to check for additional permissions.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1966
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1967
    public static Object getAttribute(Path path, String attribute,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1968
                                      LinkOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1969
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1970
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1971
        // only one attribute should be read
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1972
        if (attribute.indexOf('*') >= 0 || attribute.indexOf(',') >= 0)
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1973
            throw new IllegalArgumentException(attribute);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1974
        Map<String,Object> map = readAttributes(path, attribute, options);
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  1975
        assert map.size() == 1;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1976
        String name;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1977
        int pos = attribute.indexOf(':');
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1978
        if (pos == -1) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1979
            name = attribute;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1980
        } else {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1981
            name = (pos == attribute.length()) ? "" : attribute.substring(pos+1);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1982
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1983
        return map.get(name);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1984
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1985
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1986
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1987
     * Reads a set of file attributes as a bulk operation.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1988
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1989
     * <p> The {@code attributes} parameter identifies the attributes to be read
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1990
     * and takes the form:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1991
     * <blockquote>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1992
     * [<i>view-name</i><b>:</b>]<i>attribute-list</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1993
     * </blockquote>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1994
     * where square brackets [...] delineate an optional component and the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1995
     * character {@code ':'} stands for itself.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1996
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1997
     * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1998
     * FileAttributeView} that identifies a set of file attributes. If not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  1999
     * specified then it defaults to {@code "basic"}, the name of the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2000
     * attribute view that identifies the basic set of file attributes common to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2001
     * many file systems.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2002
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2003
     * <p> The <i>attribute-list</i> component is a comma separated list of
35717
6d49715a72e5 8064466: (fs spec) Files.readAttributes(Path, String, LinkOption...) not clear when called with zero attributes
bpb
parents: 32143
diff changeset
  2004
     * one or more names of attributes to read. If the list contains the value
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2005
     * {@code "*"} then all attributes are read. Attributes that are not supported
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2006
     * are ignored and will not be present in the returned map. It is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2007
     * implementation specific if all attributes are read as an atomic operation
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2008
     * with respect to other file system operations.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2009
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2010
     * <p> The following examples demonstrate possible values for the {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2011
     * attributes} parameter:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2012
     *
46147
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2013
     * <table class="striped" style="text-align: left; margin-left:2em">
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  2014
     * <caption style="display:none">Possible values</caption>
46147
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2015
     * <thead>
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2016
     * <tr>
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2017
     *  <th scope="col">Example
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2018
     *  <th scope="col">Description
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2019
     * </thead>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  2020
     * <tbody>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2021
     * <tr>
46147
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2022
     *   <th scope="row"> {@code "*"} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2023
     *   <td> Read all {@link BasicFileAttributes basic-file-attributes}. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2024
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2025
     * <tr>
46147
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2026
     *   <th scope="row"> {@code "size,lastModifiedTime,lastAccessTime"} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2027
     *   <td> Reads the file size, last modified time, and last access time
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2028
     *     attributes. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2029
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2030
     * <tr>
46147
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2031
     *   <th scope="row"> {@code "posix:*"} </th>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2032
     *   <td> Read all {@link PosixFileAttributes POSIX-file-attributes}. </td>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2033
     * </tr>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2034
     * <tr>
46147
047bdc4d771d 8185994: Fix a11y and HTML issues in the java.base/java.io and java.base/java.nio packages
jjg
parents: 45881
diff changeset
  2035
     *   <th scope="row"> {@code "posix:permissions,owner,size"} </th>
23887
7b2fb8d5f6be 8040262: (fs) Misc. typos in comments and implementation
alanb
parents: 22351
diff changeset
  2036
     *   <td> Reads the POSIX file permissions, owner, and file size. </td>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2037
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 39657
diff changeset
  2038
     * </tbody>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2039
     * </table>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2040
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2041
     * <p> The {@code options} array may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2042
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2043
     * symbolic links are followed and the file attribute of the final target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2044
     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2045
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2046
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2047
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2048
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2049
     * @param   attributes
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2050
     *          the attributes to read
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2051
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2052
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2053
     *
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  2054
     * @return  a map of the attributes returned; The map's keys are the
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  2055
     *          attribute names, its values are the attribute values
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2056
     *
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  2057
     * @throws  UnsupportedOperationException
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  2058
     *          if the attribute view is not available
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  2059
     * @throws  IllegalArgumentException
35717
6d49715a72e5 8064466: (fs spec) Files.readAttributes(Path, String, LinkOption...) not clear when called with zero attributes
bpb
parents: 32143
diff changeset
  2060
     *          if no attributes are specified or an unrecognized attribute is
8808
fc799c458da8 7017446: (fs) Updates to file system API (3/2011)
alanb
parents: 8558
diff changeset
  2061
     *          specified
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2062
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2063
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2064
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2065
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2066
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2067
     *          method denies read access to the file. If this method is invoked
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2068
     *          to read security sensitive attributes then the security manager
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2069
     *          may be invoke to check for additional permissions.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2070
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2071
    public static Map<String,Object> readAttributes(Path path, String attributes,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2072
                                                    LinkOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2073
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2074
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2075
        return provider(path).readAttributes(path, attributes, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2076
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2077
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2078
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2079
     * Returns a file's POSIX file permissions.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2080
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2081
     * <p> The {@code path} parameter is associated with a {@code FileSystem}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2082
     * that supports the {@link PosixFileAttributeView}. This attribute view
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2083
     * provides access to file attributes commonly associated with files on file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2084
     * systems used by operating systems that implement the Portable Operating
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2085
     * System Interface (POSIX) family of standards.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2086
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2087
     * <p> The {@code options} array may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2088
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2089
     * symbolic links are followed and the file attribute of the final target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2090
     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2091
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2092
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2093
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2094
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2095
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2096
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2097
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2098
     * @return  the file permissions
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2099
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2100
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2101
     *          if the associated file system does not support the {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2102
     *          PosixFileAttributeView}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2103
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2104
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2105
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2106
     *          In the case of the default provider, a security manager is
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  2107
     *          installed, and it denies
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  2108
     *          {@link RuntimePermission}{@code ("accessUserInformation")}
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2109
     *          or its {@link SecurityManager#checkRead(String) checkRead} method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2110
     *          denies read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2111
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2112
    public static Set<PosixFilePermission> getPosixFilePermissions(Path path,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2113
                                                                   LinkOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2114
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2115
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2116
        return readAttributes(path, PosixFileAttributes.class, options).permissions();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2117
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2118
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2119
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2120
     * Sets a file's POSIX permissions.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2121
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2122
     * <p> The {@code path} parameter is associated with a {@code FileSystem}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2123
     * that supports the {@link PosixFileAttributeView}. This attribute view
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2124
     * provides access to file attributes commonly associated with files on file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2125
     * systems used by operating systems that implement the Portable Operating
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2126
     * System Interface (POSIX) family of standards.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2127
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2128
     * @param   path
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  2129
     *          The path to the file
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2130
     * @param   perms
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2131
     *          The new set of permissions
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2132
     *
27775
4ee8b208017c 8062955: (fs spec) Files.setLastModifiedTime should specify SecurityException more clearly
alanb
parents: 27339
diff changeset
  2133
     * @return  The given path
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  2134
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2135
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2136
     *          if the associated file system does not support the {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2137
     *          PosixFileAttributeView}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2138
     * @throws  ClassCastException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2139
     *          if the sets contains elements that are not of type {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2140
     *          PosixFilePermission}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2141
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2142
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2143
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2144
     *          In the case of the default provider, and a security manager is
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  2145
     *          installed, it denies
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  2146
     *          {@link RuntimePermission}{@code ("accessUserInformation")}
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2147
     *          or its {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2148
     *          method denies write access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2149
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2150
    public static Path setPosixFilePermissions(Path path,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2151
                                               Set<PosixFilePermission> perms)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2152
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2153
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2154
        PosixFileAttributeView view =
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2155
            getFileAttributeView(path, PosixFileAttributeView.class);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2156
        if (view == null)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2157
            throw new UnsupportedOperationException();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2158
        view.setPermissions(perms);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2159
        return path;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2160
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2161
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2162
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2163
     * Returns the owner of a file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2164
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2165
     * <p> The {@code path} parameter is associated with a file system that
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2166
     * supports {@link FileOwnerAttributeView}. This file attribute view provides
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2167
     * access to a file attribute that is the owner of the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2168
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2169
     * @param   path
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  2170
     *          The path to the file
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2171
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2172
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2173
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2174
     * @return  A user principal representing the owner of the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2175
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2176
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2177
     *          if the associated file system does not support the {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2178
     *          FileOwnerAttributeView}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2179
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2180
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2181
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2182
     *          In the case of the default provider, and a security manager is
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  2183
     *          installed, it denies
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  2184
     *          {@link RuntimePermission}{@code ("accessUserInformation")}
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2185
     *          or its {@link SecurityManager#checkRead(String) checkRead} method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2186
     *          denies read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2187
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2188
    public static UserPrincipal getOwner(Path path, LinkOption... options) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2189
        FileOwnerAttributeView view =
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2190
            getFileAttributeView(path, FileOwnerAttributeView.class, options);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2191
        if (view == null)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2192
            throw new UnsupportedOperationException();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2193
        return view.getOwner();
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2194
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2195
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2196
    /**
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2197
     * Updates the file owner.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2198
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2199
     * <p> The {@code path} parameter is associated with a file system that
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2200
     * supports {@link FileOwnerAttributeView}. This file attribute view provides
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2201
     * access to a file attribute that is the owner of the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2202
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2203
     * <p> <b>Usage Example:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2204
     * Suppose we want to make "joe" the owner of a file:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2205
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2206
     *     Path path = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2207
     *     UserPrincipalLookupService lookupService =
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2208
     *         provider(path).getUserPrincipalLookupService();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2209
     *     UserPrincipal joe = lookupService.lookupPrincipalByName("joe");
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2210
     *     Files.setOwner(path, joe);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2211
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2212
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2213
     * @param   path
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  2214
     *          The path to the file
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2215
     * @param   owner
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2216
     *          The new file owner
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2217
     *
27775
4ee8b208017c 8062955: (fs spec) Files.setLastModifiedTime should specify SecurityException more clearly
alanb
parents: 27339
diff changeset
  2218
     * @return  The given path
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  2219
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2220
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2221
     *          if the associated file system does not support the {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2222
     *          FileOwnerAttributeView}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2223
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2224
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2225
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2226
     *          In the case of the default provider, and a security manager is
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  2227
     *          installed, it denies
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 32139
diff changeset
  2228
     *          {@link RuntimePermission}{@code ("accessUserInformation")}
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2229
     *          or its {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2230
     *          method denies write access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2231
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2232
     * @see FileSystem#getUserPrincipalLookupService
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2233
     * @see java.nio.file.attribute.UserPrincipalLookupService
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2234
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2235
    public static Path setOwner(Path path, UserPrincipal owner)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2236
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2237
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2238
        FileOwnerAttributeView view =
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2239
            getFileAttributeView(path, FileOwnerAttributeView.class);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2240
        if (view == null)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2241
            throw new UnsupportedOperationException();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2242
        view.setOwner(owner);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2243
        return path;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2244
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2245
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2246
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2247
     * Tests whether a file is a symbolic link.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2248
     *
20536
2e22d22c52ed 8025983: Typo in Javadoc of Files.isRegularFile()
alanb
parents: 19800
diff changeset
  2249
     * <p> Where it is required to distinguish an I/O exception from the case
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2250
     * that the file is not a symbolic link then the file attributes can be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2251
     * read with the {@link #readAttributes(Path,Class,LinkOption[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2252
     * readAttributes} method and the file type tested with the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2253
     * BasicFileAttributes#isSymbolicLink} method.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2254
     *
18574
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  2255
     * @param   path  The path to the file
4aeaeb541678 8019380: doclint warnings in java.nio, java.nio.file.**, java.nio.channels.**
alanb
parents: 18253
diff changeset
  2256
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2257
     * @return  {@code true} if the file is a symbolic link; {@code false} if
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2258
     *          the file does not exist, is not a symbolic link, or it cannot
9026
cdf11e673c21 7033568: (file) Miscellaneous typos
alanb
parents: 8808
diff changeset
  2259
     *          be determined if the file is a symbolic link or not.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2260
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2261
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2262
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2263
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2264
     *          method denies read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2265
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2266
    public static boolean isSymbolicLink(Path path) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2267
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2268
            return readAttributes(path,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2269
                                  BasicFileAttributes.class,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2270
                                  LinkOption.NOFOLLOW_LINKS).isSymbolicLink();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2271
        } catch (IOException ioe) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2272
            return false;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2273
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2274
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2275
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2276
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2277
     * Tests whether a file is a directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2278
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2279
     * <p> The {@code options} array may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2280
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2281
     * symbolic links are followed and the file attribute of the final target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2282
     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2283
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2284
     *
20536
2e22d22c52ed 8025983: Typo in Javadoc of Files.isRegularFile()
alanb
parents: 19800
diff changeset
  2285
     * <p> Where it is required to distinguish an I/O exception from the case
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2286
     * that the file is not a directory then the file attributes can be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2287
     * read with the {@link #readAttributes(Path,Class,LinkOption[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2288
     * readAttributes} method and the file type tested with the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2289
     * BasicFileAttributes#isDirectory} method.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2290
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2291
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2292
     *          the path to the file to test
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2293
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2294
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2295
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2296
     * @return  {@code true} if the file is a directory; {@code false} if
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2297
     *          the file does not exist, is not a directory, or it cannot
9026
cdf11e673c21 7033568: (file) Miscellaneous typos
alanb
parents: 8808
diff changeset
  2298
     *          be determined if the file is a directory or not.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2299
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2300
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2301
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2302
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2303
     *          method denies read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2304
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2305
    public static boolean isDirectory(Path path, LinkOption... options) {
37346
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2306
        if (options.length == 0) {
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2307
            FileSystemProvider provider = provider(path);
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2308
            if (provider instanceof AbstractFileSystemProvider)
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2309
                return ((AbstractFileSystemProvider)provider).isDirectory(path);
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2310
        }
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2311
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2312
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2313
            return readAttributes(path, BasicFileAttributes.class, options).isDirectory();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2314
        } catch (IOException ioe) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2315
            return false;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2316
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2317
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2318
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2319
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2320
     * Tests whether a file is a regular file with opaque content.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2321
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2322
     * <p> The {@code options} array may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2323
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2324
     * symbolic links are followed and the file attribute of the final target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2325
     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2326
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2327
     *
20536
2e22d22c52ed 8025983: Typo in Javadoc of Files.isRegularFile()
alanb
parents: 19800
diff changeset
  2328
     * <p> Where it is required to distinguish an I/O exception from the case
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2329
     * that the file is not a regular file then the file attributes can be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2330
     * read with the {@link #readAttributes(Path,Class,LinkOption[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2331
     * readAttributes} method and the file type tested with the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2332
     * BasicFileAttributes#isRegularFile} method.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2333
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2334
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2335
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2336
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2337
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2338
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2339
     * @return  {@code true} if the file is a regular file; {@code false} if
9026
cdf11e673c21 7033568: (file) Miscellaneous typos
alanb
parents: 8808
diff changeset
  2340
     *          the file does not exist, is not a regular file, or it
cdf11e673c21 7033568: (file) Miscellaneous typos
alanb
parents: 8808
diff changeset
  2341
     *          cannot be determined if the file is a regular file or not.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2342
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2343
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2344
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2345
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2346
     *          method denies read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2347
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2348
    public static boolean isRegularFile(Path path, LinkOption... options) {
37346
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2349
        if (options.length == 0) {
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2350
            FileSystemProvider provider = provider(path);
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2351
            if (provider instanceof AbstractFileSystemProvider)
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2352
                return ((AbstractFileSystemProvider)provider).isRegularFile(path);
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2353
        }
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2354
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2355
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2356
            return readAttributes(path, BasicFileAttributes.class, options).isRegularFile();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2357
        } catch (IOException ioe) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2358
            return false;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2359
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2360
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2361
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2362
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2363
     * Returns a file's last modified time.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2364
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2365
     * <p> The {@code options} array may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2366
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2367
     * symbolic links are followed and the file attribute of the final target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2368
     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2369
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2370
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2371
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2372
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2373
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2374
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2375
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2376
     * @return  a {@code FileTime} representing the time the file was last
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2377
     *          modified, or an implementation specific default when a time
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2378
     *          stamp to indicate the time of last modification is not supported
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2379
     *          by the file system
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2380
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2381
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2382
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2383
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2384
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2385
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2386
     *          method denies read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2387
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2388
     * @see BasicFileAttributes#lastModifiedTime
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2389
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2390
    public static FileTime getLastModifiedTime(Path path, LinkOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2391
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2392
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2393
        return readAttributes(path, BasicFileAttributes.class, options).lastModifiedTime();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2394
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2395
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2396
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2397
     * Updates a file's last modified time attribute. The file time is converted
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2398
     * to the epoch and precision supported by the file system. Converting from
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2399
     * finer to coarser granularities result in precision loss. The behavior of
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2400
     * this method when attempting to set the last modified time when it is not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2401
     * supported by the file system or is outside the range supported by the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2402
     * underlying file store is not defined. It may or not fail by throwing an
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2403
     * {@code IOException}.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2404
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2405
     * <p> <b>Usage Example:</b>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2406
     * Suppose we want to set the last modified time to the current time:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2407
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2408
     *    Path path = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2409
     *    FileTime now = FileTime.fromMillis(System.currentTimeMillis());
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2410
     *    Files.setLastModifiedTime(path, now);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2411
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2412
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2413
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2414
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2415
     * @param   time
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2416
     *          the new last modified time
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2417
     *
27775
4ee8b208017c 8062955: (fs spec) Files.setLastModifiedTime should specify SecurityException more clearly
alanb
parents: 27339
diff changeset
  2418
     * @return  the given path
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2419
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2420
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2421
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2422
     * @throws  SecurityException
27775
4ee8b208017c 8062955: (fs spec) Files.setLastModifiedTime should specify SecurityException more clearly
alanb
parents: 27339
diff changeset
  2423
     *          In the case of the default provider, and a security manager is
4ee8b208017c 8062955: (fs spec) Files.setLastModifiedTime should specify SecurityException more clearly
alanb
parents: 27339
diff changeset
  2424
     *          installed, its {@link SecurityManager#checkWrite(String)
4ee8b208017c 8062955: (fs spec) Files.setLastModifiedTime should specify SecurityException more clearly
alanb
parents: 27339
diff changeset
  2425
     *          checkWrite} method denies write access to the file.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2426
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2427
     * @see BasicFileAttributeView#setTimes
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2428
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2429
    public static Path setLastModifiedTime(Path path, FileTime time)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2430
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2431
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2432
        getFileAttributeView(path, BasicFileAttributeView.class)
27775
4ee8b208017c 8062955: (fs spec) Files.setLastModifiedTime should specify SecurityException more clearly
alanb
parents: 27339
diff changeset
  2433
            .setTimes(Objects.requireNonNull(time), null, null);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2434
        return path;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2435
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2436
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2437
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2438
     * Returns the size of a file (in bytes). The size may differ from the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2439
     * actual size on the file system due to compression, support for sparse
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2440
     * files, or other reasons. The size of files that are not {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2441
     * #isRegularFile regular} files is implementation specific and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2442
     * therefore unspecified.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2443
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2444
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2445
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2446
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2447
     * @return  the file size, in bytes
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2448
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2449
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2450
     *          if an I/O error occurs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2451
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2452
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2453
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2454
     *          method denies read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2455
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2456
     * @see BasicFileAttributes#size
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2457
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2458
    public static long size(Path path) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2459
        return readAttributes(path, BasicFileAttributes.class).size();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2460
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2461
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2462
    // -- Accessibility --
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2463
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2464
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2465
     * Returns {@code false} if NOFOLLOW_LINKS is present.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2466
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2467
    private static boolean followLinks(LinkOption... options) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2468
        boolean followLinks = true;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2469
        for (LinkOption opt: options) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2470
            if (opt == LinkOption.NOFOLLOW_LINKS) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2471
                followLinks = false;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2472
                continue;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2473
            }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2474
            if (opt == null)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2475
                throw new NullPointerException();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2476
            throw new AssertionError("Should not get here");
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2477
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2478
        return followLinks;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2479
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2480
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2481
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2482
     * Tests whether a file exists.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2483
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2484
     * <p> The {@code options} parameter may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2485
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2486
     * symbolic links are followed. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2487
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2488
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2489
     * <p> Note that the result of this method is immediately outdated. If this
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2490
     * method indicates the file exists then there is no guarantee that a
47496
66e2e3f62eb5 8190502: (fs) Typo in java.nio.file.Files
bpb
parents: 47478
diff changeset
  2491
     * subsequent access will succeed. Care should be taken when using this
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2492
     * method in security sensitive applications.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2493
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2494
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2495
     *          the path to the file to test
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2496
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2497
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2498
     * .
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2499
     * @return  {@code true} if the file exists; {@code false} if the file does
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2500
     *          not exist or its existence cannot be determined.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2501
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2502
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2503
     *          In the case of the default provider, the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2504
     *          SecurityManager#checkRead(String)} is invoked to check
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2505
     *          read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2506
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2507
     * @see #notExists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2508
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2509
    public static boolean exists(Path path, LinkOption... options) {
37346
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2510
        if (options.length == 0) {
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2511
            FileSystemProvider provider = provider(path);
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2512
            if (provider instanceof AbstractFileSystemProvider)
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2513
                return ((AbstractFileSystemProvider)provider).exists(path);
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2514
        }
931e55c1287d 8154077: (fs) Reduce number of file system classes loaded during startup
alanb
parents: 36511
diff changeset
  2515
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2516
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2517
            if (followLinks(options)) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2518
                provider(path).checkAccess(path);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2519
            } else {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2520
                // attempt to read attributes without following links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2521
                readAttributes(path, BasicFileAttributes.class,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2522
                               LinkOption.NOFOLLOW_LINKS);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2523
            }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2524
            // file exists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2525
            return true;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2526
        } catch (IOException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2527
            // does not exist or unable to determine if file exists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2528
            return false;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2529
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2530
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2531
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2532
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2533
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2534
     * Tests whether the file located by this path does not exist. This method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2535
     * is intended for cases where it is required to take action when it can be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2536
     * confirmed that a file does not exist.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2537
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2538
     * <p> The {@code options} parameter may be used to indicate how symbolic links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2539
     * are handled for the case that the file is a symbolic link. By default,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2540
     * symbolic links are followed. If the option {@link LinkOption#NOFOLLOW_LINKS
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2541
     * NOFOLLOW_LINKS} is present then symbolic links are not followed.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2542
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2543
     * <p> Note that this method is not the complement of the {@link #exists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2544
     * exists} method. Where it is not possible to determine if a file exists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2545
     * or not then both methods return {@code false}. As with the {@code exists}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2546
     * method, the result of this method is immediately outdated. If this
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2547
     * method indicates the file does exist then there is no guarantee that a
47496
66e2e3f62eb5 8190502: (fs) Typo in java.nio.file.Files
bpb
parents: 47478
diff changeset
  2548
     * subsequent attempt to create the file will succeed. Care should be taken
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2549
     * when using this method in security sensitive applications.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2550
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2551
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2552
     *          the path to the file to test
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2553
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2554
     *          options indicating how symbolic links are handled
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2555
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2556
     * @return  {@code true} if the file does not exist; {@code false} if the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2557
     *          file exists or its existence cannot be determined
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2558
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2559
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2560
     *          In the case of the default provider, the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2561
     *          SecurityManager#checkRead(String)} is invoked to check
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2562
     *          read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2563
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2564
    public static boolean notExists(Path path, LinkOption... options) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2565
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2566
            if (followLinks(options)) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2567
                provider(path).checkAccess(path);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2568
            } else {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2569
                // attempt to read attributes without following links
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2570
                readAttributes(path, BasicFileAttributes.class,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2571
                               LinkOption.NOFOLLOW_LINKS);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2572
            }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2573
            // file exists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2574
            return false;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2575
        } catch (NoSuchFileException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2576
            // file confirmed not to exist
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2577
            return true;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2578
        } catch (IOException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2579
            return false;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2580
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2581
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2582
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2583
    /**
23887
7b2fb8d5f6be 8040262: (fs) Misc. typos in comments and implementation
alanb
parents: 22351
diff changeset
  2584
     * Used by isReadable, isWritable, isExecutable to test access to a file.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2585
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2586
    private static boolean isAccessible(Path path, AccessMode... modes) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2587
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2588
            provider(path).checkAccess(path, modes);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2589
            return true;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2590
        } catch (IOException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2591
            return false;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2592
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2593
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2594
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2595
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2596
     * Tests whether a file is readable. This method checks that a file exists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2597
     * and that this Java virtual machine has appropriate privileges that would
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2598
     * allow it open the file for reading. Depending on the implementation, this
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2599
     * method may require to read file permissions, access control lists, or
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2600
     * other file attributes in order to check the effective access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2601
     * Consequently, this method may not be atomic with respect to other file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2602
     * system operations.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2603
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2604
     * <p> Note that the result of this method is immediately outdated, there is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2605
     * no guarantee that a subsequent attempt to open the file for reading will
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2606
     * succeed (or even that it will access the same file). Care should be taken
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2607
     * when using this method in security sensitive applications.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2608
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2609
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2610
     *          the path to the file to check
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2611
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2612
     * @return  {@code true} if the file exists and is readable; {@code false}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2613
     *          if the file does not exist, read access would be denied because
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2614
     *          the Java virtual machine has insufficient privileges, or access
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2615
     *          cannot be determined
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2616
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2617
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2618
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2619
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2620
     *          is invoked to check read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2621
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2622
    public static boolean isReadable(Path path) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2623
        return isAccessible(path, AccessMode.READ);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2624
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2625
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2626
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2627
     * Tests whether a file is writable. This method checks that a file exists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2628
     * and that this Java virtual machine has appropriate privileges that would
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2629
     * allow it open the file for writing. Depending on the implementation, this
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2630
     * method may require to read file permissions, access control lists, or
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2631
     * other file attributes in order to check the effective access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2632
     * Consequently, this method may not be atomic with respect to other file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2633
     * system operations.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2634
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2635
     * <p> Note that result of this method is immediately outdated, there is no
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2636
     * guarantee that a subsequent attempt to open the file for writing will
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2637
     * succeed (or even that it will access the same file). Care should be taken
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2638
     * when using this method in security sensitive applications.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2639
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2640
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2641
     *          the path to the file to check
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2642
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2643
     * @return  {@code true} if the file exists and is writable; {@code false}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2644
     *          if the file does not exist, write access would be denied because
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2645
     *          the Java virtual machine has insufficient privileges, or access
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2646
     *          cannot be determined
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2647
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2648
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2649
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2650
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2651
     *          is invoked to check write access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2652
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2653
    public static boolean isWritable(Path path) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2654
        return isAccessible(path, AccessMode.WRITE);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2655
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2656
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2657
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2658
     * Tests whether a file is executable. This method checks that a file exists
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2659
     * and that this Java virtual machine has appropriate privileges to {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2660
     * Runtime#exec execute} the file. The semantics may differ when checking
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2661
     * access to a directory. For example, on UNIX systems, checking for
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2662
     * execute access checks that the Java virtual machine has permission to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2663
     * search the directory in order to access file or subdirectories.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2664
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2665
     * <p> Depending on the implementation, this method may require to read file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2666
     * permissions, access control lists, or other file attributes in order to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2667
     * check the effective access to the file. Consequently, this method may not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2668
     * be atomic with respect to other file system operations.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2669
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2670
     * <p> Note that the result of this method is immediately outdated, there is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2671
     * no guarantee that a subsequent attempt to execute the file will succeed
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2672
     * (or even that it will access the same file). Care should be taken when
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2673
     * using this method in security sensitive applications.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2674
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2675
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2676
     *          the path to the file to check
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2677
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2678
     * @return  {@code true} if the file exists and is executable; {@code false}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2679
     *          if the file does not exist, execute access would be denied because
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2680
     *          the Java virtual machine has insufficient privileges, or access
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2681
     *          cannot be determined
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2682
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2683
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2684
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2685
     *          installed, the {@link SecurityManager#checkExec(String)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2686
     *          checkExec} is invoked to check execute access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2687
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2688
    public static boolean isExecutable(Path path) {
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2689
        return isAccessible(path, AccessMode.EXECUTE);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2690
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2691
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2692
    // -- Recursive operations --
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2693
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2694
    /**
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2695
     * Walks a file tree.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2696
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2697
     * <p> This method walks a file tree rooted at a given starting file. The
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2698
     * file tree traversal is <em>depth-first</em> with the given {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2699
     * FileVisitor} invoked for each file encountered. File tree traversal
3722
08849c86a297 6868627: (spec) Files.walkFileTree doesn't make it clear that uncaught errors and exceptions are propagated
alanb
parents: 3065
diff changeset
  2700
     * completes when all accessible files in the tree have been visited, or a
08849c86a297 6868627: (spec) Files.walkFileTree doesn't make it clear that uncaught errors and exceptions are propagated
alanb
parents: 3065
diff changeset
  2701
     * visit method returns a result of {@link FileVisitResult#TERMINATE
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2702
     * TERMINATE}. Where a visit method terminates due an {@code IOException},
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2703
     * an uncaught error, or runtime exception, then the traversal is terminated
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2704
     * and the error or exception is propagated to the caller of this method.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2705
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2706
     * <p> For each file encountered this method attempts to read its {@link
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2707
     * java.nio.file.attribute.BasicFileAttributes}. If the file is not a
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2708
     * directory then the {@link FileVisitor#visitFile visitFile} method is
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2709
     * invoked with the file attributes. If the file attributes cannot be read,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2710
     * due to an I/O exception, then the {@link FileVisitor#visitFileFailed
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2711
     * visitFileFailed} method is invoked with the I/O exception.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2712
     *
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2713
     * <p> Where the file is a directory, and the directory could not be opened,
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2714
     * then the {@code visitFileFailed} method is invoked with the I/O exception,
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2715
     * after which, the file tree walk continues, by default, at the next
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2716
     * <em>sibling</em> of the directory.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2717
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2718
     * <p> Where the directory is opened successfully, then the entries in the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2719
     * directory, and their <em>descendants</em> are visited. When all entries
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2720
     * have been visited, or an I/O error occurs during iteration of the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2721
     * directory, then the directory is closed and the visitor's {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2722
     * FileVisitor#postVisitDirectory postVisitDirectory} method is invoked.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2723
     * The file tree walk then continues, by default, at the next <em>sibling</em>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2724
     * of the directory.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2725
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2726
     * <p> By default, symbolic links are not automatically followed by this
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2727
     * method. If the {@code options} parameter contains the {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2728
     * FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then symbolic links are
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2729
     * followed. When following links, and the attributes of the target cannot
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2730
     * be read, then this method attempts to get the {@code BasicFileAttributes}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2731
     * of the link. If they can be read then the {@code visitFile} method is
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2732
     * invoked with the attributes of the link (otherwise the {@code visitFileFailed}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2733
     * method is invoked as specified above).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2734
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2735
     * <p> If the {@code options} parameter contains the {@link
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2736
     * FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then this method keeps
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2737
     * track of directories visited so that cycles can be detected. A cycle
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2738
     * arises when there is an entry in a directory that is an ancestor of the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2739
     * directory. Cycle detection is done by recording the {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2740
     * java.nio.file.attribute.BasicFileAttributes#fileKey file-key} of directories,
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2741
     * or if file keys are not available, by invoking the {@link #isSameFile
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2742
     * isSameFile} method to test if a directory is the same file as an
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2743
     * ancestor. When a cycle is detected it is treated as an I/O error, and the
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2744
     * {@link FileVisitor#visitFileFailed visitFileFailed} method is invoked with
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2745
     * an instance of {@link FileSystemLoopException}.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2746
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2747
     * <p> The {@code maxDepth} parameter is the maximum number of levels of
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2748
     * directories to visit. A value of {@code 0} means that only the starting
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2749
     * file is visited, unless denied by the security manager. A value of
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2750
     * {@link Integer#MAX_VALUE MAX_VALUE} may be used to indicate that all
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2751
     * levels should be visited. The {@code visitFile} method is invoked for all
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2752
     * files, including directories, encountered at {@code maxDepth}, unless the
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2753
     * basic file attributes cannot be read, in which case the {@code
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2754
     * visitFileFailed} method is invoked.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2755
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2756
     * <p> If a visitor returns a result of {@code null} then {@code
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2757
     * NullPointerException} is thrown.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2758
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2759
     * <p> When a security manager is installed and it denies access to a file
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2760
     * (or directory), then it is ignored and the visitor is not invoked for
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2761
     * that file (or directory).
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2762
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2763
     * @param   start
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2764
     *          the starting file
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2765
     * @param   options
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2766
     *          options to configure the traversal
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2767
     * @param   maxDepth
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2768
     *          the maximum number of directory levels to visit
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2769
     * @param   visitor
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2770
     *          the file visitor to invoke for each file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2771
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2772
     * @return  the starting file
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2773
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2774
     * @throws  IllegalArgumentException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2775
     *          if the {@code maxDepth} parameter is negative
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2776
     * @throws  SecurityException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2777
     *          If the security manager denies access to the starting file.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2778
     *          In the case of the default provider, the {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2779
     *          SecurityManager#checkRead(String) checkRead} method is invoked
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2780
     *          to check read access to the directory.
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2781
     * @throws  IOException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2782
     *          if an I/O error is thrown by a visitor method
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2783
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2784
    public static Path walkFileTree(Path start,
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2785
                                    Set<FileVisitOption> options,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2786
                                    int maxDepth,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2787
                                    FileVisitor<? super Path> visitor)
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2788
        throws IOException
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2789
    {
17170
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2790
        /**
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2791
         * Create a FileTreeWalker to walk the file tree, invoking the visitor
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2792
         * for each event.
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2793
         */
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2794
        try (FileTreeWalker walker = new FileTreeWalker(options, maxDepth)) {
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2795
            FileTreeWalker.Event ev = walker.walk(start);
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2796
            do {
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2797
                FileVisitResult result;
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2798
                switch (ev.type()) {
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2799
                    case ENTRY :
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2800
                        IOException ioe = ev.ioeException();
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2801
                        if (ioe == null) {
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2802
                            assert ev.attributes() != null;
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2803
                            result = visitor.visitFile(ev.file(), ev.attributes());
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2804
                        } else {
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2805
                            result = visitor.visitFileFailed(ev.file(), ioe);
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2806
                        }
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2807
                        break;
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2808
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2809
                    case START_DIRECTORY :
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2810
                        result = visitor.preVisitDirectory(ev.file(), ev.attributes());
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2811
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2812
                        // if SKIP_SIBLINGS and SKIP_SUBTREE is returned then
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2813
                        // there shouldn't be any more events for the current
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2814
                        // directory.
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2815
                        if (result == FileVisitResult.SKIP_SUBTREE ||
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2816
                            result == FileVisitResult.SKIP_SIBLINGS)
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2817
                            walker.pop();
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2818
                        break;
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2819
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2820
                    case END_DIRECTORY :
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2821
                        result = visitor.postVisitDirectory(ev.file(), ev.ioeException());
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2822
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2823
                        // SKIP_SIBLINGS is a no-op for postVisitDirectory
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2824
                        if (result == FileVisitResult.SKIP_SIBLINGS)
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2825
                            result = FileVisitResult.CONTINUE;
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2826
                        break;
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2827
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2828
                    default :
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2829
                        throw new AssertionError("Should not get here");
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2830
                }
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2831
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2832
                if (Objects.requireNonNull(result) != FileVisitResult.CONTINUE) {
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2833
                    if (result == FileVisitResult.TERMINATE) {
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2834
                        break;
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2835
                    } else if (result == FileVisitResult.SKIP_SIBLINGS) {
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2836
                        walker.skipRemainingSiblings();
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2837
                    }
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2838
                }
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2839
                ev = walker.next();
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2840
            } while (ev != null);
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2841
        }
9f33b89c7978 8012930: (fs) Eliminate recursion from FileTreeWalker
alanb
parents: 16048
diff changeset
  2842
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2843
        return start;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2844
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2845
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2846
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2847
     * Walks a file tree.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2848
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2849
     * <p> This method works as if invoking it were equivalent to evaluating the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2850
     * expression:
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2851
     * <blockquote><pre>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2852
     * walkFileTree(start, EnumSet.noneOf(FileVisitOption.class), Integer.MAX_VALUE, visitor)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2853
     * </pre></blockquote>
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2854
     * In other words, it does not follow symbolic links, and visits all levels
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8166
diff changeset
  2855
     * of the file tree.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2856
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2857
     * @param   start
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2858
     *          the starting file
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2859
     * @param   visitor
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2860
     *          the file visitor to invoke for each file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2861
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2862
     * @return  the starting file
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2863
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2864
     * @throws  SecurityException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2865
     *          If the security manager denies access to the starting file.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2866
     *          In the case of the default provider, the {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2867
     *          SecurityManager#checkRead(String) checkRead} method is invoked
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2868
     *          to check read access to the directory.
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2869
     * @throws  IOException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2870
     *          if an I/O error is thrown by a visitor method
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  2871
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2872
    public static Path walkFileTree(Path start, FileVisitor<? super Path> visitor)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2873
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2874
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2875
        return walkFileTree(start,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2876
                            EnumSet.noneOf(FileVisitOption.class),
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2877
                            Integer.MAX_VALUE,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2878
                            visitor);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2879
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2880
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2881
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2882
    // -- Utility methods for simple usages --
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2883
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2884
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2885
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2886
     * Opens a file for reading, returning a {@code BufferedReader} that may be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2887
     * used to read text from the file in an efficient manner. Bytes from the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2888
     * file are decoded into characters using the specified charset. Reading
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2889
     * commences at the beginning of the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2890
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2891
     * <p> The {@code Reader} methods that read from the file throw {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2892
     * IOException} if a malformed or unmappable byte sequence is read.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2893
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2894
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2895
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2896
     * @param   cs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2897
     *          the charset to use for decoding
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2898
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2899
     * @return  a new buffered reader, with default buffer size, to read text
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2900
     *          from the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2901
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2902
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2903
     *          if an I/O error occurs opening the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2904
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2905
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2906
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2907
     *          method is invoked to check read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2908
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2909
     * @see #readAllLines
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2910
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2911
    public static BufferedReader newBufferedReader(Path path, Charset cs)
6695
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2912
        throws IOException
0e91810597f9 6907737: (file) FileVisitor and Files.walkFileTree issues
alanb
parents: 5506
diff changeset
  2913
    {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2914
        CharsetDecoder decoder = cs.newDecoder();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2915
        Reader reader = new InputStreamReader(newInputStream(path), decoder);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2916
        return new BufferedReader(reader);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2917
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2918
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2919
    /**
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2920
     * Opens a file for reading, returning a {@code BufferedReader} to read text
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2921
     * from the file in an efficient manner. Bytes from the file are decoded into
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2922
     * characters using the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2923
     * charset}.
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2924
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2925
     * <p> This method works as if invoking it were equivalent to evaluating the
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2926
     * expression:
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2927
     * <pre>{@code
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2928
     * Files.newBufferedReader(path, StandardCharsets.UTF_8)
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2929
     * }</pre>
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2930
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2931
     * @param   path
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2932
     *          the path to the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2933
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2934
     * @return  a new buffered reader, with default buffer size, to read text
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2935
     *          from the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2936
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2937
     * @throws  IOException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2938
     *          if an I/O error occurs opening the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2939
     * @throws  SecurityException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2940
     *          In the case of the default provider, and a security manager is
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2941
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2942
     *          method is invoked to check read access to the file.
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2943
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2944
     * @since 1.8
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2945
     */
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2946
    public static BufferedReader newBufferedReader(Path path) throws IOException {
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2947
        return newBufferedReader(path, StandardCharsets.UTF_8);
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2948
    }
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2949
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  2950
    /**
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2951
     * Opens or creates a file for writing, returning a {@code BufferedWriter}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2952
     * that may be used to write text to the file in an efficient manner.
26960
d280345c2cfb 8059840: (bb) Typo in javadoc for ByteBuffer.wrap()
igerasim
parents: 25859
diff changeset
  2953
     * The {@code options} parameter specifies how the file is created or
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2954
     * opened. If no options are present then this method works as if the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2955
     * StandardOpenOption#CREATE CREATE}, {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2956
     * StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2957
     * StandardOpenOption#WRITE WRITE} options are present. In other words, it
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2958
     * opens the file for writing, creating the file if it doesn't exist, or
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2959
     * initially truncating an existing {@link #isRegularFile regular-file} to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2960
     * a size of {@code 0} if it exists.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2961
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2962
     * <p> The {@code Writer} methods to write text throw {@code IOException}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2963
     * if the text cannot be encoded using the specified charset.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2964
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2965
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2966
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2967
     * @param   cs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2968
     *          the charset to use for encoding
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2969
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2970
     *          options specifying how the file is opened
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2971
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2972
     * @return  a new buffered writer, with default buffer size, to write text
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2973
     *          to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2974
     *
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  2975
     * @throws  IllegalArgumentException
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  2976
     *          if {@code options} contains an invalid combination of options
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2977
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2978
     *          if an I/O error occurs opening or creating the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2979
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2980
     *          if an unsupported option is specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2981
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2982
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2983
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
30379
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  2984
     *          method is invoked to check write access to the file. The {@link
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  2985
     *          SecurityManager#checkDelete(String) checkDelete} method is
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  2986
     *          invoked to check delete access if the file is opened with the
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  2987
     *          {@code DELETE_ON_CLOSE} option.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2988
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2989
     * @see #write(Path,Iterable,Charset,OpenOption[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2990
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2991
    public static BufferedWriter newBufferedWriter(Path path, Charset cs,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2992
                                                   OpenOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2993
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2994
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2995
        CharsetEncoder encoder = cs.newEncoder();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2996
        Writer writer = new OutputStreamWriter(newOutputStream(path, options), encoder);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2997
        return new BufferedWriter(writer);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2998
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  2999
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3000
    /**
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3001
     * Opens or creates a file for writing, returning a {@code BufferedWriter}
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3002
     * to write text to the file in an efficient manner. The text is encoded
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3003
     * into bytes for writing using the {@link StandardCharsets#UTF_8 UTF-8}
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3004
     * {@link Charset charset}.
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3005
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3006
     * <p> This method works as if invoking it were equivalent to evaluating the
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3007
     * expression:
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3008
     * <pre>{@code
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3009
     * Files.newBufferedWriter(path, StandardCharsets.UTF_8, options)
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3010
     * }</pre>
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3011
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3012
     * @param   path
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3013
     *          the path to the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3014
     * @param   options
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3015
     *          options specifying how the file is opened
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3016
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3017
     * @return  a new buffered writer, with default buffer size, to write text
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3018
     *          to the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3019
     *
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3020
     * @throws  IllegalArgumentException
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3021
     *          if {@code options} contains an invalid combination of options
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3022
     * @throws  IOException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3023
     *          if an I/O error occurs opening or creating the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3024
     * @throws  UnsupportedOperationException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3025
     *          if an unsupported option is specified
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3026
     * @throws  SecurityException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3027
     *          In the case of the default provider, and a security manager is
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3028
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
30379
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3029
     *          method is invoked to check write access to the file. The {@link
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3030
     *          SecurityManager#checkDelete(String) checkDelete} method is
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3031
     *          invoked to check delete access if the file is opened with the
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3032
     *          {@code DELETE_ON_CLOSE} option.
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3033
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3034
     * @since 1.8
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3035
     */
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3036
    public static BufferedWriter newBufferedWriter(Path path, OpenOption... options)
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3037
        throws IOException
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3038
    {
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3039
        return newBufferedWriter(path, StandardCharsets.UTF_8, options);
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3040
    }
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3041
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3042
    /**
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3043
     * Copies all bytes from an input stream to a file. On return, the input
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3044
     * stream will be at end of stream.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3045
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3046
     * <p> By default, the copy fails if the target file already exists or is a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3047
     * symbolic link. If the {@link StandardCopyOption#REPLACE_EXISTING
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3048
     * REPLACE_EXISTING} option is specified, and the target file already exists,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3049
     * then it is replaced if it is not a non-empty directory. If the target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3050
     * file exists and is a symbolic link, then the symbolic link is replaced.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3051
     * In this release, the {@code REPLACE_EXISTING} option is the only option
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3052
     * required to be supported by this method. Additional options may be
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3053
     * supported in future releases.
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3054
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3055
     * <p>  If an I/O error occurs reading from the input stream or writing to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3056
     * the file, then it may do so after the target file has been created and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3057
     * after some bytes have been read or written. Consequently the input
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3058
     * stream may not be at end of stream and may be in an inconsistent state.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3059
     * It is strongly recommended that the input stream be promptly closed if an
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3060
     * I/O error occurs.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3061
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3062
     * <p> This method may block indefinitely reading from the input stream (or
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3063
     * writing to the file). The behavior for the case that the input stream is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3064
     * <i>asynchronously closed</i> or the thread interrupted during the copy is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3065
     * highly input stream and file system provider specific and therefore not
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3066
     * specified.
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3067
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3068
     * <p> <b>Usage example</b>: Suppose we want to capture a web page and save
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3069
     * it to a file:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3070
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3071
     *     Path path = ...
52700
b206bdfb9fe2 8213911: Use example.com in java.net and other examples
darcy
parents: 52458
diff changeset
  3072
     *     URI u = URI.create("http://www.example.com/");
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3073
     *     try (InputStream in = u.toURL().openStream()) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3074
     *         Files.copy(in, path);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3075
     *     }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3076
     * </pre>
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3077
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3078
     * @param   in
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3079
     *          the input stream to read from
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3080
     * @param   target
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3081
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3082
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3083
     *          options specifying how the copy should be done
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3084
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3085
     * @return  the number of bytes read or written
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3086
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3087
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3088
     *          if an I/O error occurs when reading or writing
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3089
     * @throws  FileAlreadyExistsException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3090
     *          if the target file exists but cannot be replaced because the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3091
     *          {@code REPLACE_EXISTING} option is not specified <i>(optional
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3092
     *          specific exception)</i>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3093
     * @throws  DirectoryNotEmptyException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3094
     *          the {@code REPLACE_EXISTING} option is specified but the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3095
     *          cannot be replaced because it is a non-empty directory
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3096
     *          <i>(optional specific exception)</i>     *
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3097
     * @throws  UnsupportedOperationException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3098
     *          if {@code options} contains a copy option that is not supported
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3099
     * @throws  SecurityException
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3100
     *          In the case of the default provider, and a security manager is
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3101
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3102
     *          method is invoked to check write access to the file. Where the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3103
     *          {@code REPLACE_EXISTING} option is specified, the security
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3104
     *          manager's {@link SecurityManager#checkDelete(String) checkDelete}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3105
     *          method is invoked to check that an existing file can be deleted.
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3106
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3107
    public static long copy(InputStream in, Path target, CopyOption... options)
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3108
        throws IOException
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3109
    {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3110
        // ensure not null before opening file
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
  3111
        Objects.requireNonNull(in);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3112
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3113
        // check for REPLACE_EXISTING
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3114
        boolean replaceExisting = false;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3115
        for (CopyOption opt: options) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3116
            if (opt == StandardCopyOption.REPLACE_EXISTING) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3117
                replaceExisting = true;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3118
            } else {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3119
                if (opt == null) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3120
                    throw new NullPointerException("options contains 'null'");
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3121
                }  else {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3122
                    throw new UnsupportedOperationException(opt + " not supported");
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3123
                }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3124
            }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3125
        }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3126
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3127
        // attempt to delete an existing file
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3128
        SecurityException se = null;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3129
        if (replaceExisting) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3130
            try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3131
                deleteIfExists(target);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3132
            } catch (SecurityException x) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3133
                se = x;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3134
            }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3135
        }
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3136
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3137
        // attempt to create target file. If it fails with
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3138
        // FileAlreadyExistsException then it may be because the security
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3139
        // manager prevented us from deleting the file, in which case we just
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3140
        // throw the SecurityException.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3141
        OutputStream ostream;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3142
        try {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3143
            ostream = newOutputStream(target, StandardOpenOption.CREATE_NEW,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3144
                                              StandardOpenOption.WRITE);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3145
        } catch (FileAlreadyExistsException x) {
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3146
            if (se != null)
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3147
                throw se;
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3148
            // someone else won the race and created the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3149
            throw x;
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3150
        }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3151
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3152
        // do the copy
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3153
        try (OutputStream out = ostream) {
48353
315c690bb90b 8193842: Replace Files.copy(InputStream,OutputStream) with InputStream.transferTo(OutputStream)
bpb
parents: 47496
diff changeset
  3154
            return in.transferTo(out);
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3155
        }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3156
    }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3157
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3158
    /**
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3159
     * Copies all bytes from a file to an output stream.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3160
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3161
     * <p> If an I/O error occurs reading from the file or writing to the output
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3162
     * stream, then it may do so after some bytes have been read or written.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3163
     * Consequently the output stream may be in an inconsistent state. It is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3164
     * strongly recommended that the output stream be promptly closed if an I/O
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3165
     * error occurs.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3166
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3167
     * <p> This method may block indefinitely writing to the output stream (or
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3168
     * reading from the file). The behavior for the case that the output stream
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3169
     * is <i>asynchronously closed</i> or the thread interrupted during the copy
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3170
     * is highly output stream and file system provider specific and therefore
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3171
     * not specified.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3172
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3173
     * <p> Note that if the given output stream is {@link java.io.Flushable}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3174
     * then its {@link java.io.Flushable#flush flush} method may need to invoked
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3175
     * after this method completes so as to flush any buffered output.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3176
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3177
     * @param   source
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3178
     *          the  path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3179
     * @param   out
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3180
     *          the output stream to write to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3181
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3182
     * @return  the number of bytes read or written
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3183
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3184
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3185
     *          if an I/O error occurs when reading or writing
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3186
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3187
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3188
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3189
     *          method is invoked to check read access to the file.
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3190
     */
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3191
    public static long copy(Path source, OutputStream out) throws IOException {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3192
        // ensure not null before opening file
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
  3193
        Objects.requireNonNull(out);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3194
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3195
        try (InputStream in = newInputStream(source)) {
48353
315c690bb90b 8193842: Replace Files.copy(InputStream,OutputStream) with InputStream.transferTo(OutputStream)
bpb
parents: 47496
diff changeset
  3196
            return in.transferTo(out);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3197
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3198
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3199
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50820
diff changeset
  3200
    private static final jdk.internal.access.JavaLangAccess JLA =
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 50820
diff changeset
  3201
            jdk.internal.access.SharedSecrets.getJavaLangAccess();
50552
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3202
19185
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3203
    /**
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3204
     * Reads all the bytes from an input stream. Uses {@code initialSize} as a hint
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3205
     * about how many bytes the stream will have.
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3206
     *
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3207
     * @param   source
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3208
     *          the input stream to read from
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3209
     * @param   initialSize
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3210
     *          the initial size of the byte array to allocate
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3211
     *
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3212
     * @return  a byte array containing the bytes read from the file
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3213
     *
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3214
     * @throws  IOException
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3215
     *          if an I/O error occurs reading from the stream
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3216
     * @throws  OutOfMemoryError
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3217
     *          if an array of the required size cannot be allocated
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3218
     */
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3219
    private static byte[] read(InputStream source, int initialSize) throws IOException {
19185
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3220
        int capacity = initialSize;
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3221
        byte[] buf = new byte[capacity];
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3222
        int nread = 0;
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3223
        int n;
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3224
        for (;;) {
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3225
            // read to EOF which may read more or less than initialSize (eg: file
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3226
            // is truncated while we are reading)
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3227
            while ((n = source.read(buf, nread, capacity - nread)) > 0)
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3228
                nread += n;
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3229
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3230
            // if last call to source.read() returned -1, we are done
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3231
            // otherwise, try to read one more byte; if that failed we're done too
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3232
            if (n < 0 || (n = source.read()) < 0)
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3233
                break;
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3234
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3235
            // one more byte was read; need to allocate a larger buffer
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 53388
diff changeset
  3236
            capacity = Math.max(ArraysSupport.newLength(capacity,
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 53388
diff changeset
  3237
                                                        1,       /* minimum growth */
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 53388
diff changeset
  3238
                                                        capacity /* preferred growth */),
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 53388
diff changeset
  3239
                                BUFFER_SIZE);
19185
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3240
            buf = Arrays.copyOf(buf, capacity);
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3241
            buf[nread++] = (byte)n;
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3242
        }
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3243
        return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3244
    }
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3245
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3246
    /**
34aa83720203 8020669: (fs) Files.readAllBytes() does not read any data when Files.size() is 0
igerasim
parents: 18822
diff changeset
  3247
     * Reads all the bytes from a file. The method ensures that the file is
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3248
     * closed when all bytes have been read or an I/O error, or other runtime
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3249
     * exception, is thrown.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3250
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3251
     * <p> Note that this method is intended for simple cases where it is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3252
     * convenient to read all bytes into a byte array. It is not intended for
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3253
     * reading in large files.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3254
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3255
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3256
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3257
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3258
     * @return  a byte array containing the bytes read from the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3259
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3260
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3261
     *          if an I/O error occurs reading from the stream
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3262
     * @throws  OutOfMemoryError
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3263
     *          if an array of the required size cannot be allocated, for
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3264
     *          example the file is larger that {@code 2GB}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3265
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3266
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3267
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3268
     *          method is invoked to check read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3269
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3270
    public static byte[] readAllBytes(Path path) throws IOException {
20541
bd0a8b142cb3 8024788: (fs) Files.readAllBytes uses FileChannel which may not be supported by all providers
alanb
parents: 20536
diff changeset
  3271
        try (SeekableByteChannel sbc = Files.newByteChannel(path);
bd0a8b142cb3 8024788: (fs) Files.readAllBytes uses FileChannel which may not be supported by all providers
alanb
parents: 20536
diff changeset
  3272
             InputStream in = Channels.newInputStream(sbc)) {
50784
57f5cba78093 8205612: (fc) Files.readAllBytes fails with ClosedByInterruptException when interrupt status set
alanb
parents: 50701
diff changeset
  3273
            if (sbc instanceof FileChannelImpl)
57f5cba78093 8205612: (fc) Files.readAllBytes fails with ClosedByInterruptException when interrupt status set
alanb
parents: 50701
diff changeset
  3274
                ((FileChannelImpl) sbc).setUninterruptible();
20541
bd0a8b142cb3 8024788: (fs) Files.readAllBytes uses FileChannel which may not be supported by all providers
alanb
parents: 20536
diff changeset
  3275
            long size = sbc.size();
54971
4285b4d13471 8223593: Refactor code for reallocating storage
igerasim
parents: 53388
diff changeset
  3276
            if (size > (long) Integer.MAX_VALUE)
17728
8bbdf55ba9d4 8014928: (fs) Files.readAllBytes() copies content to new array when content completely read
alanb
parents: 17696
diff changeset
  3277
                throw new OutOfMemoryError("Required array size too large");
20541
bd0a8b142cb3 8024788: (fs) Files.readAllBytes uses FileChannel which may not be supported by all providers
alanb
parents: 20536
diff changeset
  3278
            return read(in, (int)size);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3279
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3280
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3281
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3282
    /**
50552
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3283
     * Reads all content from a file into a string, decoding from bytes to characters
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3284
     * using the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset charset}.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3285
     * The method ensures that the file is closed when all content have been read
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3286
     * or an I/O error, or other runtime exception, is thrown.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3287
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3288
     * <p> This method is equivalent to:
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3289
     * {@code readString(path, StandardCharsets.UTF_8) }
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3290
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3291
     * @param   path the path to the file
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3292
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3293
     * @return  a String containing the content read from the file
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3294
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3295
     * @throws  IOException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3296
     *          if an I/O error occurs reading from the file or a malformed or
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3297
     *          unmappable byte sequence is read
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3298
     * @throws  OutOfMemoryError
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3299
     *          if the file is extremely large, for example larger than {@code 2GB}
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3300
     * @throws  SecurityException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3301
     *          In the case of the default provider, and a security manager is
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3302
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3303
     *          method is invoked to check read access to the file.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3304
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3305
     * @since 11
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3306
     */
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3307
    public static String readString(Path path) throws IOException {
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3308
        return readString(path, StandardCharsets.UTF_8);
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3309
    }
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3310
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3311
    /**
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3312
     * Reads all characters from a file into a string, decoding from bytes to characters
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3313
     * using the specified {@linkplain Charset charset}.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3314
     * The method ensures that the file is closed when all content have been read
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3315
     * or an I/O error, or other runtime exception, is thrown.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3316
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3317
     * <p> This method reads all content including the line separators in the middle
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3318
     * and/or at the end. The resulting string will contain line separators as they
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3319
     * appear in the file.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3320
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3321
     * @apiNote
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3322
     * This method is intended for simple cases where it is appropriate and convenient
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3323
     * to read the content of a file into a String. It is not intended for reading
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3324
     * very large files.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3325
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3326
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3327
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3328
     * @param   path the path to the file
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3329
     * @param   cs the charset to use for decoding
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3330
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3331
     * @return  a String containing the content read from the file
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3332
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3333
     * @throws  IOException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3334
     *          if an I/O error occurs reading from the file or a malformed or
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3335
     *          unmappable byte sequence is read
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3336
     * @throws  OutOfMemoryError
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3337
     *          if the file is extremely large, for example larger than {@code 2GB}
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3338
     * @throws  SecurityException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3339
     *          In the case of the default provider, and a security manager is
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3340
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3341
     *          method is invoked to check read access to the file.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3342
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3343
     * @since 11
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3344
     */
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3345
    public static String readString(Path path, Charset cs) throws IOException {
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3346
        Objects.requireNonNull(path);
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3347
        Objects.requireNonNull(cs);
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3348
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3349
        byte[] ba = readAllBytes(path);
50820
35f52a3cd6bd 8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents: 50784
diff changeset
  3350
        return JLA.newStringNoRepl(ba, cs);
50552
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3351
    }
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3352
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3353
    /**
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3354
     * Read all lines from a file. This method ensures that the file is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3355
     * closed when all bytes have been read or an I/O error, or other runtime
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3356
     * exception, is thrown. Bytes from the file are decoded into characters
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3357
     * using the specified charset.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3358
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3359
     * <p> This method recognizes the following as line terminators:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3360
     * <ul>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3361
     *   <li> <code>&#92;u000D</code> followed by <code>&#92;u000A</code>,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3362
     *     CARRIAGE RETURN followed by LINE FEED </li>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3363
     *   <li> <code>&#92;u000A</code>, LINE FEED </li>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3364
     *   <li> <code>&#92;u000D</code>, CARRIAGE RETURN </li>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3365
     * </ul>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3366
     * <p> Additional Unicode line terminators may be recognized in future
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3367
     * releases.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3368
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3369
     * <p> Note that this method is intended for simple cases where it is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3370
     * convenient to read all lines in a single operation. It is not intended
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3371
     * for reading in large files.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3372
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3373
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3374
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3375
     * @param   cs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3376
     *          the charset to use for decoding
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3377
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3378
     * @return  the lines from the file as a {@code List}; whether the {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3379
     *          List} is modifiable or not is implementation dependent and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3380
     *          therefore not specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3381
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3382
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3383
     *          if an I/O error occurs reading from the file or a malformed or
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3384
     *          unmappable byte sequence is read
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3385
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3386
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3387
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3388
     *          method is invoked to check read access to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3389
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3390
     * @see #newBufferedReader
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3391
     */
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3392
    public static List<String> readAllLines(Path path, Charset cs) throws IOException {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3393
        try (BufferedReader reader = newBufferedReader(path, cs)) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3394
            List<String> result = new ArrayList<>();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3395
            for (;;) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3396
                String line = reader.readLine();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3397
                if (line == null)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3398
                    break;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3399
                result.add(line);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3400
            }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3401
            return result;
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3402
        }
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2057
diff changeset
  3403
    }
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3404
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3405
    /**
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3406
     * Read all lines from a file. Bytes from the file are decoded into characters
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3407
     * using the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset charset}.
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3408
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3409
     * <p> This method works as if invoking it were equivalent to evaluating the
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3410
     * expression:
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3411
     * <pre>{@code
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3412
     * Files.readAllLines(path, StandardCharsets.UTF_8)
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3413
     * }</pre>
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3414
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3415
     * @param   path
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3416
     *          the path to the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3417
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3418
     * @return  the lines from the file as a {@code List}; whether the {@code
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3419
     *          List} is modifiable or not is implementation dependent and
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3420
     *          therefore not specified
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3421
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3422
     * @throws  IOException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3423
     *          if an I/O error occurs reading from the file or a malformed or
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3424
     *          unmappable byte sequence is read
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3425
     * @throws  SecurityException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3426
     *          In the case of the default provider, and a security manager is
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3427
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3428
     *          method is invoked to check read access to the file.
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3429
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3430
     * @since 1.8
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3431
     */
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3432
    public static List<String> readAllLines(Path path) throws IOException {
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3433
        return readAllLines(path, StandardCharsets.UTF_8);
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3434
    }
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3435
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3436
    /**
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
  3437
     * Writes bytes to a file. The {@code options} parameter specifies how
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3438
     * the file is created or opened. If no options are present then this method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3439
     * works as if the {@link StandardOpenOption#CREATE CREATE}, {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3440
     * StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3441
     * StandardOpenOption#WRITE WRITE} options are present. In other words, it
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3442
     * opens the file for writing, creating the file if it doesn't exist, or
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3443
     * initially truncating an existing {@link #isRegularFile regular-file} to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3444
     * a size of {@code 0}. All bytes in the byte array are written to the file.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3445
     * The method ensures that the file is closed when all bytes have been
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3446
     * written (or an I/O error or other runtime exception is thrown). If an I/O
39657
90ff5a56fe54 8161455: Missing word in API documentation
bpb
parents: 37346
diff changeset
  3447
     * error occurs then it may do so after the file has been created or
90ff5a56fe54 8161455: Missing word in API documentation
bpb
parents: 37346
diff changeset
  3448
     * truncated, or after some bytes have been written to the file.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3449
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3450
     * <p> <b>Usage example</b>: By default the method creates a new file or
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8166
diff changeset
  3451
     * overwrites an existing file. Suppose you instead want to append bytes
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3452
     * to an existing file:
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3453
     * <pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3454
     *     Path path = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3455
     *     byte[] bytes = ...
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3456
     *     Files.write(path, bytes, StandardOpenOption.APPEND);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3457
     * </pre>
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3458
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3459
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3460
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3461
     * @param   bytes
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3462
     *          the byte array with the bytes to write
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3463
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3464
     *          options specifying how the file is opened
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3465
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3466
     * @return  the path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3467
     *
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3468
     * @throws  IllegalArgumentException
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3469
     *          if {@code options} contains an invalid combination of options
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3470
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3471
     *          if an I/O error occurs writing to or creating the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3472
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3473
     *          if an unsupported option is specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3474
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3475
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3476
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
30379
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3477
     *          method is invoked to check write access to the file. The {@link
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3478
     *          SecurityManager#checkDelete(String) checkDelete} method is
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3479
     *          invoked to check delete access if the file is opened with the
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3480
     *          {@code DELETE_ON_CLOSE} option.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3481
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3482
    public static Path write(Path path, byte[] bytes, OpenOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3483
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3484
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3485
        // ensure bytes is not null before opening file
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
  3486
        Objects.requireNonNull(bytes);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3487
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3488
        try (OutputStream out = Files.newOutputStream(path, options)) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3489
            int len = bytes.length;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3490
            int rem = len;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3491
            while (rem > 0) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3492
                int n = Math.min(rem, BUFFER_SIZE);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3493
                out.write(bytes, (len-rem), n);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3494
                rem -= n;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3495
            }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3496
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3497
        return path;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3498
    }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3499
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3500
    /**
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3501
     * Write lines of text to a file. Each line is a char sequence and is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3502
     * written to the file in sequence with each line terminated by the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3503
     * platform's line separator, as defined by the system property {@code
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3504
     * line.separator}. Characters are encoded into bytes using the specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3505
     * charset.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3506
     *
26960
d280345c2cfb 8059840: (bb) Typo in javadoc for ByteBuffer.wrap()
igerasim
parents: 25859
diff changeset
  3507
     * <p> The {@code options} parameter specifies how the file is created
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3508
     * or opened. If no options are present then this method works as if the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3509
     * {@link StandardOpenOption#CREATE CREATE}, {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3510
     * StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3511
     * StandardOpenOption#WRITE WRITE} options are present. In other words, it
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3512
     * opens the file for writing, creating the file if it doesn't exist, or
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3513
     * initially truncating an existing {@link #isRegularFile regular-file} to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3514
     * a size of {@code 0}. The method ensures that the file is closed when all
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3515
     * lines have been written (or an I/O error or other runtime exception is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3516
     * thrown). If an I/O error occurs then it may do so after the file has
39657
90ff5a56fe54 8161455: Missing word in API documentation
bpb
parents: 37346
diff changeset
  3517
     * been created or truncated, or after some bytes have been written to the
90ff5a56fe54 8161455: Missing word in API documentation
bpb
parents: 37346
diff changeset
  3518
     * file.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3519
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3520
     * @param   path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3521
     *          the path to the file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3522
     * @param   lines
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3523
     *          an object to iterate over the char sequences
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3524
     * @param   cs
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3525
     *          the charset to use for encoding
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3526
     * @param   options
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3527
     *          options specifying how the file is opened
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3528
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3529
     * @return  the path
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3530
     *
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3531
     * @throws  IllegalArgumentException
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3532
     *          if {@code options} contains an invalid combination of options
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3533
     * @throws  IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3534
     *          if an I/O error occurs writing to or creating the file, or the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3535
     *          text cannot be encoded using the specified charset
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3536
     * @throws  UnsupportedOperationException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3537
     *          if an unsupported option is specified
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3538
     * @throws  SecurityException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3539
     *          In the case of the default provider, and a security manager is
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3540
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
30379
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3541
     *          method is invoked to check write access to the file. The {@link
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3542
     *          SecurityManager#checkDelete(String) checkDelete} method is
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3543
     *          invoked to check delete access if the file is opened with the
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3544
     *          {@code DELETE_ON_CLOSE} option.
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3545
     */
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3546
    public static Path write(Path path, Iterable<? extends CharSequence> lines,
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3547
                             Charset cs, OpenOption... options)
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3548
        throws IOException
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3549
    {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3550
        // ensure lines is not null before opening file
8166
13423c0952ad 7012540: java.util.Objects.nonNull() incorrectly named
briangoetz
parents: 8158
diff changeset
  3551
        Objects.requireNonNull(lines);
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3552
        CharsetEncoder encoder = cs.newEncoder();
58515
8f849d3ec1e5 8232003: (fs) Files.write can leak file descriptor in the exception case
sgehwolf
parents: 54971
diff changeset
  3553
        try (OutputStream out = newOutputStream(path, options);
8f849d3ec1e5 8232003: (fs) Files.write can leak file descriptor in the exception case
sgehwolf
parents: 54971
diff changeset
  3554
             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, encoder))) {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3555
            for (CharSequence line: lines) {
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3556
                writer.append(line);
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3557
                writer.newLine();
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3558
            }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3559
        }
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3560
        return path;
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7668
diff changeset
  3561
    }
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3562
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3563
    /**
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3564
     * Write lines of text to a file. Characters are encoded into bytes using
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3565
     * the {@link StandardCharsets#UTF_8 UTF-8} {@link Charset charset}.
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3566
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3567
     * <p> This method works as if invoking it were equivalent to evaluating the
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3568
     * expression:
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3569
     * <pre>{@code
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3570
     * Files.write(path, lines, StandardCharsets.UTF_8, options);
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3571
     * }</pre>
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3572
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3573
     * @param   path
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3574
     *          the path to the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3575
     * @param   lines
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3576
     *          an object to iterate over the char sequences
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3577
     * @param   options
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3578
     *          options specifying how the file is opened
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3579
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3580
     * @return  the path
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3581
     *
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3582
     * @throws  IllegalArgumentException
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3583
     *          if {@code options} contains an invalid combination of options
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3584
     * @throws  IOException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3585
     *          if an I/O error occurs writing to or creating the file, or the
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3586
     *          text cannot be encoded as {@code UTF-8}
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3587
     * @throws  UnsupportedOperationException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3588
     *          if an unsupported option is specified
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3589
     * @throws  SecurityException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3590
     *          In the case of the default provider, and a security manager is
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3591
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
30379
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3592
     *          method is invoked to check write access to the file. The {@link
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3593
     *          SecurityManager#checkDelete(String) checkDelete} method is
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3594
     *          invoked to check delete access if the file is opened with the
42d3c3090d20 8065109: (fs spec) Files.newBufferedWriter doesn't specify SecurityException for DELETE_ON_CLOSE option
bpb
parents: 29986
diff changeset
  3595
     *          {@code DELETE_ON_CLOSE} option.
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3596
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3597
     * @since 1.8
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3598
     */
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3599
    public static Path write(Path path,
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3600
                             Iterable<? extends CharSequence> lines,
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3601
                             OpenOption... options)
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3602
        throws IOException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3603
    {
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3604
        return write(path, lines, StandardCharsets.UTF_8, options);
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3605
    }
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3606
50552
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3607
    /**
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3608
     * Write a {@linkplain java.lang.CharSequence CharSequence} to a file.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3609
     * Characters are encoded into bytes using the
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3610
     * {@link StandardCharsets#UTF_8 UTF-8} {@link Charset charset}.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3611
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3612
     * <p> This method is equivalent to:
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3613
     * {@code writeString(path, test, StandardCharsets.UTF_8, options) }
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3614
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3615
     * @param   path
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3616
     *          the path to the file
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3617
     * @param   csq
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3618
     *          the CharSequence to be written
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3619
     * @param   options
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3620
     *          options specifying how the file is opened
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3621
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3622
     * @return  the path
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3623
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3624
     * @throws  IllegalArgumentException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3625
     *          if {@code options} contains an invalid combination of options
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3626
     * @throws  IOException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3627
     *          if an I/O error occurs writing to or creating the file, or the
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3628
     *          text cannot be encoded using the specified charset
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3629
     * @throws  UnsupportedOperationException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3630
     *          if an unsupported option is specified
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3631
     * @throws  SecurityException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3632
     *          In the case of the default provider, and a security manager is
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3633
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3634
     *          method is invoked to check write access to the file. The {@link
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3635
     *          SecurityManager#checkDelete(String) checkDelete} method is
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3636
     *          invoked to check delete access if the file is opened with the
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3637
     *          {@code DELETE_ON_CLOSE} option.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3638
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3639
     * @since 11
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3640
     */
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3641
    public static Path writeString(Path path, CharSequence csq, OpenOption... options)
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3642
            throws IOException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3643
    {
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3644
        return writeString(path, csq, StandardCharsets.UTF_8, options);
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3645
    }
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3646
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3647
    /**
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3648
     * Write a {@linkplain java.lang.CharSequence CharSequence} to a file.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3649
     * Characters are encoded into bytes using the specified
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3650
     * {@linkplain java.nio.charset.Charset charset}.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3651
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3652
     * <p> All characters are written as they are, including the line separators in
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3653
     * the char sequence. No extra characters are added.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3654
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3655
     * <p> The {@code options} parameter specifies how the file is created
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3656
     * or opened. If no options are present then this method works as if the
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3657
     * {@link StandardOpenOption#CREATE CREATE}, {@link
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3658
     * StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING}, and {@link
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3659
     * StandardOpenOption#WRITE WRITE} options are present. In other words, it
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3660
     * opens the file for writing, creating the file if it doesn't exist, or
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3661
     * initially truncating an existing {@link #isRegularFile regular-file} to
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3662
     * a size of {@code 0}.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3663
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3664
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3665
     * @param   path
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3666
     *          the path to the file
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3667
     * @param   csq
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3668
     *          the CharSequence to be written
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3669
     * @param   cs
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3670
     *          the charset to use for encoding
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3671
     * @param   options
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3672
     *          options specifying how the file is opened
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3673
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3674
     * @return  the path
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3675
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3676
     * @throws  IllegalArgumentException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3677
     *          if {@code options} contains an invalid combination of options
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3678
     * @throws  IOException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3679
     *          if an I/O error occurs writing to or creating the file, or the
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3680
     *          text cannot be encoded using the specified charset
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3681
     * @throws  UnsupportedOperationException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3682
     *          if an unsupported option is specified
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3683
     * @throws  SecurityException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3684
     *          In the case of the default provider, and a security manager is
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3685
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3686
     *          method is invoked to check write access to the file. The {@link
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3687
     *          SecurityManager#checkDelete(String) checkDelete} method is
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3688
     *          invoked to check delete access if the file is opened with the
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3689
     *          {@code DELETE_ON_CLOSE} option.
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3690
     *
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3691
     * @since 11
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3692
     */
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3693
    public static Path writeString(Path path, CharSequence csq, Charset cs, OpenOption... options)
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3694
            throws IOException
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3695
    {
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3696
        // ensure the text is not null before opening file
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3697
        Objects.requireNonNull(path);
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3698
        Objects.requireNonNull(csq);
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3699
        Objects.requireNonNull(cs);
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3700
50820
35f52a3cd6bd 8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents: 50784
diff changeset
  3701
        byte[] bytes = JLA.getBytesNoRepl(String.valueOf(csq), cs);
35f52a3cd6bd 8205058: (fs) Files read/writeString should throw CharacterCodingException instead of IOException with an IllegalArgumentException as cause
joehw
parents: 50784
diff changeset
  3702
        write(path, bytes, options);
50552
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3703
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3704
        return path;
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3705
    }
7439ceaae8e4 8201276: (fs) Add methods to Files for reading/writing a string from/to a file
joehw
parents: 50447
diff changeset
  3706
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3707
    // -- Stream APIs --
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3708
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3709
    /**
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3710
     * Return a lazily populated {@code Stream}, the elements of
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3711
     * which are the entries in the directory.  The listing is not recursive.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3712
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3713
     * <p> The elements of the stream are {@link Path} objects that are
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3714
     * obtained as if by {@link Path#resolve(Path) resolving} the name of the
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3715
     * directory entry against {@code dir}. Some file systems maintain special
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3716
     * links to the directory itself and the directory's parent directory.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3717
     * Entries representing these links are not included.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3718
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3719
     * <p> The stream is <i>weakly consistent</i>. It is thread safe but does
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3720
     * not freeze the directory while iterating, so it may (or may not)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3721
     * reflect updates to the directory that occur after returning from this
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3722
     * method.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3723
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3724
     * <p> The returned stream contains a reference to an open directory.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3725
     * The directory is closed by closing the stream.
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3726
     *
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3727
     * <p> Operating on a closed stream behaves as if the end of stream
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3728
     * has been reached. Due to read-ahead, one or more elements may be
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3729
     * returned after the stream has been closed.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3730
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3731
     * <p> If an {@link IOException} is thrown when accessing the directory
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3732
     * after this method has returned, it is wrapped in an {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3733
     * UncheckedIOException} which will be thrown from the method that caused
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3734
     * the access to take place.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3735
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3736
     * @apiNote
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3737
     * This method must be used within a try-with-resources statement or similar
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3738
     * control structure to ensure that the stream's open directory is closed
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3739
     * promptly after the stream's operations have completed.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3740
     *
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3741
     * @param   dir  The path to the directory
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3742
     *
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3743
     * @return  The {@code Stream} describing the content of the
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3744
     *          directory
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3745
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3746
     * @throws  NotDirectoryException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3747
     *          if the file could not otherwise be opened because it is not
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3748
     *          a directory <i>(optional specific exception)</i>
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3749
     * @throws  IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3750
     *          if an I/O error occurs when opening the directory
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3751
     * @throws  SecurityException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3752
     *          In the case of the default provider, and a security manager is
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3753
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3754
     *          method is invoked to check read access to the directory.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3755
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3756
     * @see     #newDirectoryStream(Path)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3757
     * @since   1.8
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3758
     */
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3759
    public static Stream<Path> list(Path dir) throws IOException {
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3760
        DirectoryStream<Path> ds = Files.newDirectoryStream(dir);
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3761
        try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3762
            final Iterator<Path> delegate = ds.iterator();
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3763
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3764
            // Re-wrap DirectoryIteratorException to UncheckedIOException
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 29269
diff changeset
  3765
            Iterator<Path> iterator = new Iterator<>() {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3766
                @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3767
                public boolean hasNext() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3768
                    try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3769
                        return delegate.hasNext();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3770
                    } catch (DirectoryIteratorException e) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3771
                        throw new UncheckedIOException(e.getCause());
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3772
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3773
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3774
                @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3775
                public Path next() {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3776
                    try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3777
                        return delegate.next();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3778
                    } catch (DirectoryIteratorException e) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3779
                        throw new UncheckedIOException(e.getCause());
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3780
                    }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3781
                }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3782
            };
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3783
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3784
            Spliterator<Path> spliterator =
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3785
                Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3786
            return StreamSupport.stream(spliterator, false)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3787
                                .onClose(asUncheckedRunnable(ds));
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3788
        } catch (Error|RuntimeException e) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3789
            try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3790
                ds.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3791
            } catch (IOException ex) {
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3792
                try {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3793
                    e.addSuppressed(ex);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3794
                } catch (Throwable ignore) {}
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3795
            }
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3796
            throw e;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3797
        }
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3798
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3799
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3800
    /**
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3801
     * Return a {@code Stream} that is lazily populated with {@code
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3802
     * Path} by walking the file tree rooted at a given starting file.  The
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3803
     * file tree is traversed <em>depth-first</em>, the elements in the stream
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3804
     * are {@link Path} objects that are obtained as if by {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3805
     * Path#resolve(Path) resolving} the relative path against {@code start}.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3806
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3807
     * <p> The {@code stream} walks the file tree as elements are consumed.
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3808
     * The {@code Stream} returned is guaranteed to have at least one
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3809
     * element, the starting file itself. For each file visited, the stream
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3810
     * attempts to read its {@link BasicFileAttributes}. If the file is a
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3811
     * directory and can be opened successfully, entries in the directory, and
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3812
     * their <em>descendants</em> will follow the directory in the stream as
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3813
     * they are encountered. When all entries have been visited, then the
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3814
     * directory is closed. The file tree walk then continues at the next
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3815
     * <em>sibling</em> of the directory.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3816
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3817
     * <p> The stream is <i>weakly consistent</i>. It does not freeze the
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3818
     * file tree while iterating, so it may (or may not) reflect updates to
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3819
     * the file tree that occur after returned from this method.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3820
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3821
     * <p> By default, symbolic links are not automatically followed by this
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3822
     * method. If the {@code options} parameter contains the {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3823
     * FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then symbolic links are
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3824
     * followed. When following links, and the attributes of the target cannot
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3825
     * be read, then this method attempts to get the {@code BasicFileAttributes}
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3826
     * of the link.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3827
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3828
     * <p> If the {@code options} parameter contains the {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3829
     * FileVisitOption#FOLLOW_LINKS FOLLOW_LINKS} option then the stream keeps
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3830
     * track of directories visited so that cycles can be detected. A cycle
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3831
     * arises when there is an entry in a directory that is an ancestor of the
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3832
     * directory. Cycle detection is done by recording the {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3833
     * java.nio.file.attribute.BasicFileAttributes#fileKey file-key} of directories,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3834
     * or if file keys are not available, by invoking the {@link #isSameFile
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3835
     * isSameFile} method to test if a directory is the same file as an
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3836
     * ancestor. When a cycle is detected it is treated as an I/O error with
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3837
     * an instance of {@link FileSystemLoopException}.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3838
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3839
     * <p> The {@code maxDepth} parameter is the maximum number of levels of
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3840
     * directories to visit. A value of {@code 0} means that only the starting
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3841
     * file is visited, unless denied by the security manager. A value of
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3842
     * {@link Integer#MAX_VALUE MAX_VALUE} may be used to indicate that all
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3843
     * levels should be visited.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3844
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3845
     * <p> When a security manager is installed and it denies access to a file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3846
     * (or directory), then it is ignored and not included in the stream.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3847
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3848
     * <p> The returned stream contains references to one or more open directories.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3849
     * The directories are closed by closing the stream.
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3850
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3851
     * <p> If an {@link IOException} is thrown when accessing the directory
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3852
     * after this method has returned, it is wrapped in an {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3853
     * UncheckedIOException} which will be thrown from the method that caused
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3854
     * the access to take place.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3855
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3856
     * @apiNote
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3857
     * This method must be used within a try-with-resources statement or similar
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3858
     * control structure to ensure that the stream's open directories are closed
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3859
     * promptly after the stream's operations have completed.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3860
     *
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3861
     * @param   start
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3862
     *          the starting file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3863
     * @param   maxDepth
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3864
     *          the maximum number of directory levels to visit
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3865
     * @param   options
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3866
     *          options to configure the traversal
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3867
     *
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3868
     * @return  the {@link Stream} of {@link Path}
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3869
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3870
     * @throws  IllegalArgumentException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3871
     *          if the {@code maxDepth} parameter is negative
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3872
     * @throws  SecurityException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3873
     *          If the security manager denies access to the starting file.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3874
     *          In the case of the default provider, the {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3875
     *          SecurityManager#checkRead(String) checkRead} method is invoked
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3876
     *          to check read access to the directory.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3877
     * @throws  IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3878
     *          if an I/O error is thrown when accessing the starting file.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3879
     * @since   1.8
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3880
     */
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3881
    public static Stream<Path> walk(Path start,
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3882
                                    int maxDepth,
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3883
                                    FileVisitOption... options)
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3884
        throws IOException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3885
    {
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3886
        FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3887
        try {
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3888
            Spliterator<FileTreeWalker.Event> spliterator =
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3889
                Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  3890
            return StreamSupport.stream(spliterator, false)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3891
                                .onClose(iterator::close)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3892
                                .map(entry -> entry.file());
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3893
        } catch (Error|RuntimeException e) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3894
            iterator.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3895
            throw e;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3896
        }
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3897
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3898
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3899
    /**
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3900
     * Return a {@code Stream} that is lazily populated with {@code
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3901
     * Path} by walking the file tree rooted at a given starting file.  The
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3902
     * file tree is traversed <em>depth-first</em>, the elements in the stream
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3903
     * are {@link Path} objects that are obtained as if by {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3904
     * Path#resolve(Path) resolving} the relative path against {@code start}.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3905
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3906
     * <p> This method works as if invoking it were equivalent to evaluating the
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3907
     * expression:
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3908
     * <blockquote><pre>
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3909
     * walk(start, Integer.MAX_VALUE, options)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3910
     * </pre></blockquote>
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3911
     * In other words, it visits all levels of the file tree.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3912
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3913
     * <p> The returned stream contains references to one or more open directories.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3914
     * The directories are closed by closing the stream.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3915
     *
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3916
     * @apiNote
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3917
     * This method must be used within a try-with-resources statement or similar
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3918
     * control structure to ensure that the stream's open directories are closed
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3919
     * promptly after the stream's operations have completed.
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3920
     *
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3921
     * @param   start
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3922
     *          the starting file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3923
     * @param   options
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3924
     *          options to configure the traversal
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3925
     *
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3926
     * @return  the {@link Stream} of {@link Path}
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3927
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3928
     * @throws  SecurityException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3929
     *          If the security manager denies access to the starting file.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3930
     *          In the case of the default provider, the {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3931
     *          SecurityManager#checkRead(String) checkRead} method is invoked
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3932
     *          to check read access to the directory.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3933
     * @throws  IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3934
     *          if an I/O error is thrown when accessing the starting file.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3935
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3936
     * @see     #walk(Path, int, FileVisitOption...)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3937
     * @since   1.8
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3938
     */
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  3939
    public static Stream<Path> walk(Path start, FileVisitOption... options) throws IOException {
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3940
        return walk(start, Integer.MAX_VALUE, options);
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3941
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3942
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3943
    /**
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3944
     * Return a {@code Stream} that is lazily populated with {@code
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3945
     * Path} by searching for files in a file tree rooted at a given starting
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3946
     * file.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3947
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3948
     * <p> This method walks the file tree in exactly the manner specified by
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3949
     * the {@link #walk walk} method. For each file encountered, the given
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3950
     * {@link BiPredicate} is invoked with its {@link Path} and {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3951
     * BasicFileAttributes}. The {@code Path} object is obtained as if by
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3952
     * {@link Path#resolve(Path) resolving} the relative path against {@code
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3953
     * start} and is only included in the returned {@link Stream} if
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3954
     * the {@code BiPredicate} returns true. Compare to calling {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3955
     * java.util.stream.Stream#filter filter} on the {@code Stream}
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3956
     * returned by {@code walk} method, this method may be more efficient by
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3957
     * avoiding redundant retrieval of the {@code BasicFileAttributes}.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3958
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3959
     * <p> The returned stream contains references to one or more open directories.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3960
     * The directories are closed by closing the stream.
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3961
     *
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3962
     * <p> If an {@link IOException} is thrown when accessing the directory
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3963
     * after returned from this method, it is wrapped in an {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3964
     * UncheckedIOException} which will be thrown from the method that caused
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3965
     * the access to take place.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3966
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3967
     * @apiNote
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3968
     * This method must be used within a try-with-resources statement or similar
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3969
     * control structure to ensure that the stream's open directories are closed
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3970
     * promptly after the stream's operations have completed.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  3971
     *
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3972
     * @param   start
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3973
     *          the starting file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3974
     * @param   maxDepth
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3975
     *          the maximum number of directory levels to search
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3976
     * @param   matcher
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3977
     *          the function used to decide whether a file should be included
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3978
     *          in the returned stream
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3979
     * @param   options
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3980
     *          options to configure the traversal
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3981
     *
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3982
     * @return  the {@link Stream} of {@link Path}
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3983
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3984
     * @throws  IllegalArgumentException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3985
     *          if the {@code maxDepth} parameter is negative
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3986
     * @throws  SecurityException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3987
     *          If the security manager denies access to the starting file.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3988
     *          In the case of the default provider, the {@link
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3989
     *          SecurityManager#checkRead(String) checkRead} method is invoked
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3990
     *          to check read access to the directory.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3991
     * @throws  IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3992
     *          if an I/O error is thrown when accessing the starting file.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3993
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3994
     * @see     #walk(Path, int, FileVisitOption...)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3995
     * @since   1.8
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  3996
     */
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3997
    public static Stream<Path> find(Path start,
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3998
                                    int maxDepth,
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  3999
                                    BiPredicate<Path, BasicFileAttributes> matcher,
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4000
                                    FileVisitOption... options)
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4001
        throws IOException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4002
    {
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4003
        FileTreeIterator iterator = new FileTreeIterator(start, maxDepth, options);
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4004
        try {
27339
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  4005
            Spliterator<FileTreeWalker.Event> spliterator =
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  4006
                Spliterators.spliteratorUnknownSize(iterator, Spliterator.DISTINCT);
2cf6bc8c2d2c 8062632: (fs spec) Package description could be clearer on the cases where NPE is thrown
alanb
parents: 26960
diff changeset
  4007
            return StreamSupport.stream(spliterator, false)
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4008
                                .onClose(iterator::close)
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4009
                                .filter(entry -> matcher.test(entry.file(), entry.attributes()))
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4010
                                .map(entry -> entry.file());
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4011
        } catch (Error|RuntimeException e) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4012
            iterator.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4013
            throw e;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4014
        }
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4015
    }
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4016
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4017
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4018
    /**
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4019
     * Read all lines from a file as a {@code Stream}. Unlike {@link
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4020
     * #readAllLines(Path, Charset) readAllLines}, this method does not read
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4021
     * all lines into a {@code List}, but instead populates lazily as the stream
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4022
     * is consumed.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4023
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4024
     * <p> Bytes from the file are decoded into characters using the specified
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4025
     * charset and the same line terminators as specified by {@code
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4026
     * readAllLines} are supported.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4027
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4028
     * <p> The returned stream contains a reference to an open file. The file
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4029
     * is closed by closing the stream.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4030
     *
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4031
     * <p> The file contents should not be modified during the execution of the
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4032
     * terminal stream operation. Otherwise, the result of the terminal stream
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4033
     * operation is undefined.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4034
     *
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4035
     * <p> After this method returns, then any subsequent I/O exception that
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4036
     * occurs while reading from the file or when a malformed or unmappable byte
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4037
     * sequence is read, is wrapped in an {@link UncheckedIOException} that will
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4038
     * be thrown from the
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4039
     * {@link java.util.stream.Stream} method that caused the read to take
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4040
     * place. In case an {@code IOException} is thrown when closing the file,
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4041
     * it is also wrapped as an {@code UncheckedIOException}.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4042
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4043
     * @apiNote
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4044
     * This method must be used within a try-with-resources statement or similar
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4045
     * control structure to ensure that the stream's open file is closed promptly
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4046
     * after the stream's operations have completed.
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4047
     *
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4048
     * @implNote
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4049
     * This implementation supports good parallel stream performance for the
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4050
     * standard charsets {@link StandardCharsets#UTF_8 UTF-8},
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4051
     * {@link StandardCharsets#US_ASCII US-ASCII} and
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4052
     * {@link StandardCharsets#ISO_8859_1 ISO-8859-1}.  Such
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4053
     * <em>line-optimal</em> charsets have the property that the encoded bytes
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4054
     * of a line feed ('\n') or a carriage return ('\r') are efficiently
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4055
     * identifiable from other encoded characters when randomly accessing the
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4056
     * bytes of the file.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4057
     *
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4058
     * <p> For non-<em>line-optimal</em> charsets the stream source's
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4059
     * spliterator has poor splitting properties, similar to that of a
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4060
     * spliterator associated with an iterator or that associated with a stream
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4061
     * returned from {@link BufferedReader#lines()}.  Poor splitting properties
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4062
     * can result in poor parallel stream performance.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4063
     *
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4064
     * <p> For <em>line-optimal</em> charsets the stream source's spliterator
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4065
     * has good splitting properties, assuming the file contains a regular
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4066
     * sequence of lines.  Good splitting properties can result in good parallel
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4067
     * stream performance.  The spliterator for a <em>line-optimal</em> charset
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4068
     * takes advantage of the charset properties (a line feed or a carriage
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4069
     * return being efficient identifiable) such that when splitting it can
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4070
     * approximately divide the number of covered lines in half.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4071
     *
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4072
     * @param   path
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4073
     *          the path to the file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4074
     * @param   cs
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4075
     *          the charset to use for decoding
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4076
     *
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4077
     * @return  the lines from the file as a {@code Stream}
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4078
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4079
     * @throws  IOException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4080
     *          if an I/O error occurs opening the file
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4081
     * @throws  SecurityException
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4082
     *          In the case of the default provider, and a security manager is
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4083
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4084
     *          method is invoked to check read access to the file.
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4085
     *
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4086
     * @see     #readAllLines(Path, Charset)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4087
     * @see     #newBufferedReader(Path, Charset)
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4088
     * @see     java.io.BufferedReader#lines()
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4089
     * @since   1.8
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4090
     */
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4091
    public static Stream<String> lines(Path path, Charset cs) throws IOException {
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4092
        // Use the good splitting spliterator if:
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4093
        // 1) the path is associated with the default file system;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4094
        // 2) the character set is supported; and
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4095
        // 3) the file size is such that all bytes can be indexed by int values
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4096
        //    (this limitation is imposed by ByteBuffer)
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4097
        if (path.getFileSystem() == FileSystems.getDefault() &&
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4098
            FileChannelLinesSpliterator.SUPPORTED_CHARSET_NAMES.contains(cs.name())) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4099
            FileChannel fc = FileChannel.open(path, StandardOpenOption.READ);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4100
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4101
            Stream<String> fcls = createFileChannelLinesStream(fc, cs);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4102
            if (fcls != null) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4103
                return fcls;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4104
            }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4105
            fc.close();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4106
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4107
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4108
        return createBufferedReaderLinesStream(Files.newBufferedReader(path, cs));
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4109
    }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4110
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4111
    private static Stream<String> createFileChannelLinesStream(FileChannel fc, Charset cs) throws IOException {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4112
        try {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4113
            // Obtaining the size from the FileChannel is much faster
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4114
            // than obtaining using path.toFile().length()
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4115
            long length = fc.size();
32139
caacc3d1a348 8132539: (fs) Files.lines(path).collect() returns wrong value in JDK 9 with certain files
bpb
parents: 31258
diff changeset
  4116
            // FileChannel.size() may in certain circumstances return zero
caacc3d1a348 8132539: (fs) Files.lines(path).collect() returns wrong value in JDK 9 with certain files
bpb
parents: 31258
diff changeset
  4117
            // for a non-zero length file so disallow this case.
caacc3d1a348 8132539: (fs) Files.lines(path).collect() returns wrong value in JDK 9 with certain files
bpb
parents: 31258
diff changeset
  4118
            if (length > 0 && length <= Integer.MAX_VALUE) {
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4119
                Spliterator<String> s = new FileChannelLinesSpliterator(fc, cs, 0, (int) length);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4120
                return StreamSupport.stream(s, false)
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4121
                        .onClose(Files.asUncheckedRunnable(fc));
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4122
            }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4123
        } catch (Error|RuntimeException|IOException e) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4124
            try {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4125
                fc.close();
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4126
            } catch (IOException ex) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4127
                try {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4128
                    e.addSuppressed(ex);
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4129
                } catch (Throwable ignore) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4130
                }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4131
            }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4132
            throw e;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4133
        }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4134
        return null;
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4135
    }
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4136
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4137
    private static Stream<String> createBufferedReaderLinesStream(BufferedReader br) {
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4138
        try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4139
            return br.lines().onClose(asUncheckedRunnable(br));
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4140
        } catch (Error|RuntimeException e) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4141
            try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4142
                br.close();
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4143
            } catch (IOException ex) {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4144
                try {
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4145
                    e.addSuppressed(ex);
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4146
                } catch (Throwable ignore) {
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4147
                }
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4148
            }
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4149
            throw e;
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19185
diff changeset
  4150
        }
17696
23a863fbb6c3 8006884: (fs) Add Files.list, lines and find
henryjen
parents: 17170
diff changeset
  4151
    }
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4152
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4153
    /**
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4154
     * Read all lines from a file as a {@code Stream}. Bytes from the file are
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4155
     * decoded into characters using the {@link StandardCharsets#UTF_8 UTF-8}
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4156
     * {@link Charset charset}.
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4157
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4158
     * <p> The returned stream contains a reference to an open file. The file
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4159
     * is closed by closing the stream.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4160
     *
31258
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4161
     * <p> The file contents should not be modified during the execution of the
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4162
     * terminal stream operation. Otherwise, the result of the terminal stream
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4163
     * operation is undefined.
8e44e5e2563e 8072773: (fs) Files.lines needs a better splitting implementation for stream source
psandoz
parents: 30379
diff changeset
  4164
     *
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4165
     * <p> This method works as if invoking it were equivalent to evaluating the
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4166
     * expression:
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4167
     * <pre>{@code
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4168
     * Files.lines(path, StandardCharsets.UTF_8)
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4169
     * }</pre>
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4170
     *
29269
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4171
     * @apiNote
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4172
     * This method must be used within a try-with-resources statement or similar
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4173
     * control structure to ensure that the stream's open file is closed promptly
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4174
     * after the stream's operations have completed.
5f136809bb3c 8073923: Files.lines() documentation needs clarification
smarks
parents: 27799
diff changeset
  4175
     *
20779
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4176
     * @param   path
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4177
     *          the path to the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4178
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4179
     * @return  the lines from the file as a {@code Stream}
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4180
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4181
     * @throws  IOException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4182
     *          if an I/O error occurs opening the file
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4183
     * @throws  SecurityException
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4184
     *          In the case of the default provider, and a security manager is
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4185
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4186
     *          method is invoked to check read access to the file.
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4187
     *
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4188
     * @since 1.8
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4189
     */
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4190
    public static Stream<String> lines(Path path) throws IOException {
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4191
        return lines(path, StandardCharsets.UTF_8);
483371f41926 8019526: (fs) Files.lines, etc without Charset parameter
alanb
parents: 20541
diff changeset
  4192
    }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff changeset
  4193
}