src/java.base/share/classes/java/io/File.java
author bpb
Mon, 25 Nov 2019 15:30:35 -0800
changeset 59263 f34ad283fcd6
parent 58288 48e480e56aad
permissions -rw-r--r--
8179320: File.getUsableSpace() returns a negative number on very large file system Reviewed-by: alanb, rriggs, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53258
e348b0160d61 8216172: File.renameTo(File dest) should check for NPE at the very beginning
bpb
parents: 52902
diff changeset
     2
 * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
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: 5172
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5172
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5172
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5172
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5172
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URI;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.URISyntaxException;
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
    32
import java.nio.file.FileStore;
53258
e348b0160d61 8216172: File.renameTo(File dest) should check for NPE at the very beginning
bpb
parents: 52902
diff changeset
    33
import java.nio.file.FileSystems;
e348b0160d61 8216172: File.renameTo(File dest) should check for NPE at the very beginning
bpb
parents: 52902
diff changeset
    34
import java.nio.file.Path;
2601
39743edb9b8b 6721753: File.createTempFile produces guessable file names
alanb
parents: 681
diff changeset
    35
import java.security.SecureRandom;
53258
e348b0160d61 8216172: File.renameTo(File dest) should check for NPE at the very beginning
bpb
parents: 52902
diff changeset
    36
import java.util.ArrayList;
e348b0160d61 8216172: File.renameTo(File dest) should check for NPE at the very beginning
bpb
parents: 52902
diff changeset
    37
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * An abstract representation of file and directory pathnames.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> User interfaces and operating systems use system-dependent <em>pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * strings</em> to name files and directories.  This class presents an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * abstract, system-independent view of hierarchical pathnames.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <em>abstract pathname</em> has two components:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <li> An optional system-dependent <em>prefix</em> string,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    50
 *      such as a disk-drive specifier, {@code "/"}&nbsp;for the UNIX root
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    51
 *      directory, or {@code "\\\\"}&nbsp;for a Microsoft Windows UNC pathname, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <li> A sequence of zero or more string <em>names</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * The first name in an abstract pathname may be a directory name or, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * case of Microsoft Windows UNC pathnames, a hostname.  Each subsequent name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * in an abstract pathname denotes a directory; the last name may denote
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * either a directory or a file.  The <em>empty</em> abstract pathname has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * prefix and an empty name sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p> The conversion of a pathname string to or from an abstract pathname is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * inherently system-dependent.  When an abstract pathname is converted into a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * pathname string, each name is separated from the next by a single copy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * the default <em>separator character</em>.  The default name-separator
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    65
 * character is defined by the system property {@code file.separator}, and
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
    66
 * is made available in the public static fields {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
    67
 * #separator} and {@link #separatorChar} of this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * When a pathname string is converted into an abstract pathname, the names
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * within it may be separated by the default name-separator character or by any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * other name-separator character that is supported by the underlying system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p> A pathname, whether abstract or in string form, may be either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <em>absolute</em> or <em>relative</em>.  An absolute pathname is complete in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * that no other information is required in order to locate the file that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * denotes.  A relative pathname, in contrast, must be interpreted in terms of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * information taken from some other pathname.  By default the classes in the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    77
 * {@code java.io} package always resolve relative pathnames against the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * current user directory.  This directory is named by the system property
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    79
 * {@code user.dir}, and is typically the directory in which the Java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * virtual machine was invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> The <em>parent</em> of an abstract pathname may be obtained by invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the {@link #getParent} method of this class and consists of the pathname's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * prefix and each name in the pathname's name sequence except for the last.
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
    85
 * Each directory's absolute pathname is an ancestor of any {@code File}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * object with an absolute abstract pathname which begins with the directory's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * absolute pathname.  For example, the directory denoted by the abstract
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
    88
 * pathname {@code "/usr"} is an ancestor of the directory denoted by the
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
    89
 * pathname {@code "/usr/local/bin"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <p> The prefix concept is used to handle root directories on UNIX platforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * and drive specifiers, root directories and UNC pathnames on Microsoft Windows platforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * <li> For UNIX platforms, the prefix of an absolute pathname is always
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    98
 * {@code "/"}.  Relative pathnames have no prefix.  The abstract pathname
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
    99
 * denoting the root directory has the prefix {@code "/"} and an empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * name sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <li> For Microsoft Windows platforms, the prefix of a pathname that contains a drive
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   103
 * specifier consists of the drive letter followed by {@code ":"} and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   104
 * possibly followed by {@code "\\"} if the pathname is absolute.  The
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   105
 * prefix of a UNC pathname is {@code "\\\\"}; the hostname and the share
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * name are the first two names in the name sequence.  A relative pathname that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * does not specify a drive has no prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * <p> Instances of this class may or may not denote an actual file-system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * object such as a file or a directory.  If it does denote such an object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * then that object resides in a <i>partition</i>.  A partition is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * operating system-specific portion of storage for a file system.  A single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * contain multiple partitions.  The object, if any, will reside on the
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 43103
diff changeset
   117
 * partition <a id="partName">named</a> by some ancestor of the absolute
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * form of this pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * <p> A file system may implement restrictions to certain operations on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * actual file-system object, such as reading, writing, and executing.  These
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * restrictions are collectively known as <i>access permissions</i>.  The file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * system may have multiple sets of access permissions on a single object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * For example, one set may apply to the object's <i>owner</i>, and another
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * may apply to all other users.  The access permissions on an object may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * cause some methods in this class to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   128
 * <p> Instances of the {@code File} class are immutable; that is, once
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   129
 * created, the abstract pathname represented by a {@code File} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * will never change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53258
diff changeset
   132
 * <h2>Interoperability with {@code java.nio.file} package</h2>
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   133
 *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   134
 * <p> The <a href="../../java/nio/file/package-summary.html">{@code java.nio.file}</a>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   135
 * package defines interfaces and classes for the Java virtual machine to access
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   136
 * files, file attributes, and file systems. This API may be used to overcome
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   137
 * many of the limitations of the {@code java.io.File} class.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   138
 * The {@link #toPath toPath} method may be used to obtain a {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   139
 * Path} that uses the abstract path represented by a {@code File} object to
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   140
 * locate a file. The resulting {@code Path} may be used with the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   141
 * java.nio.file.Files} class to provide more efficient and extensive access to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   142
 * additional file operations, file attributes, and I/O exceptions to help
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   143
 * diagnose errors when an operation on a file fails.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   144
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * @author  unascribed
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21278
diff changeset
   146
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
public class File
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    implements Serializable, Comparable<File>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * The FileSystem object representing the platform's local file system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
14324
3510b4bf90ee 4239752: FileSystem should be a platform-specific class to avoid native code
dxu
parents: 13568
diff changeset
   156
    private static final FileSystem fs = DefaultFileSystem.getFileSystem();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   159
     * This abstract pathname's normalized pathname string. A normalized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * pathname string uses the default name-separator character and does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * contain any duplicate or redundant separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
11907
c021db66251d 7146152: File.path should be final
forax
parents: 11117
diff changeset
   165
    private final String path;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   168
     * Enum type that indicates the status of a file path.
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   169
     */
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   170
    private static enum PathStatus { INVALID, CHECKED };
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   171
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   172
    /**
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   173
     * The flag indicating whether the file path is invalid.
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   174
     */
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   175
    private transient PathStatus status = null;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   176
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   177
    /**
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   178
     * Check if the file has an invalid path. Currently, the inspection of
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   179
     * a file path is very limited, and it only covers Nul character check.
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   180
     * Returning true means the path is definitely invalid/garbage. But
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   181
     * returning false does not guarantee that the path is valid.
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   182
     *
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   183
     * @return true if the file path is invalid.
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   184
     */
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   185
    final boolean isInvalid() {
57816
a445d4305fad 8229899: Make java.io.File.isInvalid() less racy
aeubanks
parents: 54441
diff changeset
   186
        PathStatus s = status;
a445d4305fad 8229899: Make java.io.File.isInvalid() less racy
aeubanks
parents: 54441
diff changeset
   187
        if (s == null) {
a445d4305fad 8229899: Make java.io.File.isInvalid() less racy
aeubanks
parents: 54441
diff changeset
   188
            s = (this.path.indexOf('\u0000') < 0) ? PathStatus.CHECKED
a445d4305fad 8229899: Make java.io.File.isInvalid() less racy
aeubanks
parents: 54441
diff changeset
   189
                                                  : PathStatus.INVALID;
a445d4305fad 8229899: Make java.io.File.isInvalid() less racy
aeubanks
parents: 54441
diff changeset
   190
            status = s;
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   191
        }
57816
a445d4305fad 8229899: Make java.io.File.isInvalid() less racy
aeubanks
parents: 54441
diff changeset
   192
        return s == PathStatus.INVALID;
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   193
    }
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   194
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   195
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * The length of this abstract pathname's prefix, or zero if it has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     */
11907
c021db66251d 7146152: File.path should be final
forax
parents: 11117
diff changeset
   199
    private final transient int prefixLength;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Returns the length of this abstract pathname's prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * For use by FileSystem classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    int getPrefixLength() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return prefixLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * The system-dependent default name-separator character.  This field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * initialized to contain the first character of the value of the system
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   212
     * property {@code file.separator}.  On UNIX systems the value of this
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   213
     * field is {@code '/'}; on Microsoft Windows systems it is {@code '\\'}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public static final char separatorChar = fs.getSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * The system-dependent default name-separator character, represented as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * string for convenience.  This string contains a single character, namely
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   222
     * {@link #separatorChar}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public static final String separator = "" + separatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * The system-dependent path-separator character.  This field is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * initialized to contain the first character of the value of the system
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   229
     * property {@code path.separator}.  This character is used to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * separate filenames in a sequence of files given as a <em>path list</em>.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   231
     * On UNIX systems, this character is {@code ':'}; on Microsoft Windows systems it
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   232
     * is {@code ';'}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public static final char pathSeparatorChar = fs.getPathSeparator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * The system-dependent path-separator character, represented as a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * for convenience.  This string contains a single character, namely
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   241
     * {@link #pathSeparatorChar}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public static final String pathSeparator = "" + pathSeparatorChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /* -- Constructors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Internal constructor for already-normalized pathname strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private File(String pathname, int prefixLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        this.path = pathname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        this.prefixLength = prefixLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Internal constructor for already-normalized pathname strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * The parameter order is used to disambiguate this method from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * public(File, String) constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private File(String child, File parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        assert parent.path != null;
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52220
diff changeset
   263
        assert (!parent.path.isEmpty());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        this.path = fs.resolve(parent.path, child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        this.prefixLength = parent.prefixLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   269
     * Creates a new {@code File} instance by converting the given
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * pathname string into an abstract pathname.  If the given string is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * the empty string, then the result is the empty abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param   pathname  A pathname string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @throws  NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   275
     *          If the {@code pathname} argument is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public File(String pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        if (pathname == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        this.path = fs.normalize(pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        this.prefixLength = fs.prefixLength(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /* Note: The two-argument File constructors do not interpret an empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
       parent abstract pathname as the current user directory.  An empty parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
       instead causes the child to be resolved against the system-dependent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
       directory defined by the FileSystem.getDefaultParent method.  On Unix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
       this default is "/", while on Microsoft Windows it is "\\".  This is required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
       compatibility with the original behavior of this class. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   293
     * Creates a new {@code File} instance from a parent pathname string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * and a child pathname string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   296
     * <p> If {@code parent} is {@code null} then the new
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   297
     * {@code File} instance is created as if by invoking the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   298
     * single-argument {@code File} constructor on the given
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   299
     * {@code child} pathname string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   301
     * <p> Otherwise the {@code parent} pathname string is taken to denote
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   302
     * a directory, and the {@code child} pathname string is taken to
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   303
     * denote either a directory or a file.  If the {@code child} pathname
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * string is absolute then it is converted into a relative pathname in a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   305
     * system-dependent way.  If {@code parent} is the empty string then
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   306
     * the new {@code File} instance is created by converting
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   307
     * {@code child} into an abstract pathname and resolving the result
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * against a system-dependent default directory.  Otherwise each pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * string is converted into an abstract pathname and the child abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * pathname is resolved against the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @param   parent  The parent pathname string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * @param   child   The child pathname string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @throws  NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   315
     *          If {@code child} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    public File(String parent, String child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        if (parent != null) {
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52220
diff changeset
   322
            if (parent.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                this.path = fs.resolve(fs.getDefaultParent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                       fs.normalize(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                this.path = fs.resolve(fs.normalize(parent),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                       fs.normalize(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            this.path = fs.normalize(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        this.prefixLength = fs.prefixLength(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   336
     * Creates a new {@code File} instance from a parent abstract
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * pathname and a child pathname string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   339
     * <p> If {@code parent} is {@code null} then the new
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   340
     * {@code File} instance is created as if by invoking the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   341
     * single-argument {@code File} constructor on the given
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   342
     * {@code child} pathname string.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   344
     * <p> Otherwise the {@code parent} abstract pathname is taken to
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   345
     * denote a directory, and the {@code child} pathname string is taken
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   346
     * to denote either a directory or a file.  If the {@code child}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * pathname string is absolute then it is converted into a relative
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   348
     * pathname in a system-dependent way.  If {@code parent} is the empty
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   349
     * abstract pathname then the new {@code File} instance is created by
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   350
     * converting {@code child} into an abstract pathname and resolving
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * the result against a system-dependent default directory.  Otherwise each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * pathname string is converted into an abstract pathname and the child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * abstract pathname is resolved against the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param   parent  The parent abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @param   child   The child pathname string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * @throws  NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   358
     *          If {@code child} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    public File(File parent, String child) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (child == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (parent != null) {
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52220
diff changeset
   365
            if (parent.path.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                this.path = fs.resolve(fs.getDefaultParent(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                                       fs.normalize(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                this.path = fs.resolve(parent.path,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                       fs.normalize(child));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            this.path = fs.normalize(child);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        this.prefixLength = fs.prefixLength(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   379
     * Creates a new {@code File} instance by converting the given
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   380
     * {@code file:} URI into an abstract pathname.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   382
     * <p> The exact form of a {@code file:} URI is system-dependent, hence
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * the transformation performed by this constructor is also
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * system-dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * <p> For a given abstract pathname <i>f</i> it is guaranteed that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   388
     * <blockquote><code>
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   389
     * new File(</code><i>&nbsp;f</i><code>.{@link #toURI()
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   390
     * toURI}()).equals(</code><i>&nbsp;f</i><code>.{@link #getAbsoluteFile() getAbsoluteFile}())
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   391
     * </code></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * so long as the original abstract pathname, the URI, and the new abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * pathname are all created in (possibly different invocations of) the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * Java virtual machine.  This relationship typically does not hold,
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   396
     * however, when a {@code file:} URI that is created in a virtual machine
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * on one operating system is converted into an abstract pathname in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * virtual machine on a different operating system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @param  uri
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *         An absolute, hierarchical URI with a scheme equal to
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   402
     *         {@code "file"}, a non-empty path component, and undefined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *         authority, query, and fragment components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * @throws  NullPointerException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   406
     *          If {@code uri} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @throws  IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *          If the preconditions on the parameter do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see #toURI()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @see java.net.URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public File(URI uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        // Check our many preconditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if (!uri.isAbsolute())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            throw new IllegalArgumentException("URI is not absolute");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if (uri.isOpaque())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            throw new IllegalArgumentException("URI is not hierarchical");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        String scheme = uri.getScheme();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            throw new IllegalArgumentException("URI scheme is not \"file\"");
34782
fe102f179318 8145988: Use the raw methods of java.net.URI when possible
redestad
parents: 33674
diff changeset
   425
        if (uri.getRawAuthority() != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            throw new IllegalArgumentException("URI has an authority component");
34782
fe102f179318 8145988: Use the raw methods of java.net.URI when possible
redestad
parents: 33674
diff changeset
   427
        if (uri.getRawFragment() != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            throw new IllegalArgumentException("URI has a fragment component");
34782
fe102f179318 8145988: Use the raw methods of java.net.URI when possible
redestad
parents: 33674
diff changeset
   429
        if (uri.getRawQuery() != null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throw new IllegalArgumentException("URI has a query component");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        String p = uri.getPath();
52902
e3398b2e1ab0 8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents: 52220
diff changeset
   432
        if (p.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw new IllegalArgumentException("URI path component is empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // Okay, now initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        p = fs.fromURIPath(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (File.separatorChar != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            p = p.replace('/', File.separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        this.path = fs.normalize(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        this.prefixLength = fs.prefixLength(this.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /* -- Path-component accessors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Returns the name of the file or directory denoted by this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * pathname.  This is just the last name in the pathname's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * sequence.  If the pathname's name sequence is empty, then the empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * string is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @return  The name of the file or directory denoted by this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *          pathname, or the empty string if this pathname's name sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *          is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        int index = path.lastIndexOf(separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        if (index < prefixLength) return path.substring(prefixLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return path.substring(index + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Returns the pathname string of this abstract pathname's parent, or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   464
     * {@code null} if this pathname does not name a parent directory.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * <p> The <em>parent</em> of an abstract pathname consists of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * pathname's prefix, if any, and each name in the pathname's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * sequence except for the last.  If the name sequence is empty then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * the pathname does not name a parent directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @return  The pathname string of the parent directory named by this
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   472
     *          abstract pathname, or {@code null} if this pathname
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *          does not name a parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public String getParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        int index = path.lastIndexOf(separatorChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (index < prefixLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            if ((prefixLength > 0) && (path.length() > prefixLength))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                return path.substring(0, prefixLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        return path.substring(0, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * Returns the abstract pathname of this abstract pathname's parent,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   487
     * or {@code null} if this pathname does not name a parent
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * <p> The <em>parent</em> of an abstract pathname consists of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * pathname's prefix, if any, and each name in the pathname's name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * sequence except for the last.  If the name sequence is empty then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * the pathname does not name a parent directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @return  The abstract pathname of the parent directory named by this
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   496
     *          abstract pathname, or {@code null} if this pathname
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *          does not name a parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public File getParentFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        String p = this.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        if (p == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return new File(p, this.prefixLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Converts this abstract pathname into a pathname string.  The resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * string uses the {@link #separator default name-separator character} to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * separate the names in the name sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @return  The string form of this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    public String getPath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    /* -- Path operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * Tests whether this abstract pathname is absolute.  The definition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * absolute pathname is system dependent.  On UNIX systems, a pathname is
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   524
     * absolute if its prefix is {@code "/"}.  On Microsoft Windows systems, a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * pathname is absolute if its prefix is a drive specifier followed by
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   526
     * {@code "\\"}, or if its prefix is {@code "\\\\"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   528
     * @return  {@code true} if this abstract pathname is absolute,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   529
     *          {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    public boolean isAbsolute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        return fs.isAbsolute(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Returns the absolute pathname string of this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <p> If this abstract pathname is already absolute, then the pathname
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   539
     * string is simply returned as if by the {@link #getPath}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * method.  If this abstract pathname is the empty abstract pathname then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * the pathname string of the current user directory, which is named by the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   542
     * system property {@code user.dir}, is returned.  Otherwise this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * pathname is resolved in a system-dependent way.  On UNIX systems, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * relative pathname is made absolute by resolving it against the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * user directory.  On Microsoft Windows systems, a relative pathname is made absolute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * by resolving it against the current directory of the drive named by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * pathname, if any; if not, it is resolved against the current user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * @return  The absolute pathname string denoting the same file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *          directory as this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *          If a required system property value cannot be accessed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @see     java.io.File#isAbsolute()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public String getAbsolutePath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return fs.resolve(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Returns the absolute form of this abstract pathname.  Equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <code>new&nbsp;File(this.{@link #getAbsolutePath})</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @return  The absolute abstract pathname denoting the same file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *          directory as this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *          If a required system property value cannot be accessed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    public File getAbsoluteFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        String absPath = getAbsolutePath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        return new File(absPath, fs.prefixLength(absPath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Returns the canonical pathname string of this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <p> A canonical pathname is both absolute and unique.  The precise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * definition of canonical form is system-dependent.  This method first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * converts this pathname to absolute form if necessary, as if by invoking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * {@link #getAbsolutePath} method, and then maps it to its unique form in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * system-dependent way.  This typically involves removing redundant names
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   587
     * such as {@code "."} and {@code ".."} from the pathname, resolving
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * symbolic links (on UNIX platforms), and converting drive letters to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * standard case (on Microsoft Windows platforms).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <p> Every pathname that denotes an existing file or directory has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * unique canonical form.  Every pathname that denotes a nonexistent file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * or directory also has a unique canonical form.  The canonical form of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * the pathname of a nonexistent file or directory may be different from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * the canonical form of the same pathname after the file or directory is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * created.  Similarly, the canonical form of the pathname of an existing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * file or directory may be different from the canonical form of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * pathname after the file or directory is deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * @return  The canonical pathname string denoting the same file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *          directory as this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     *          If an I/O error occurs, which is possible because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *          construction of the canonical pathname may require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *          filesystem queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *          If a required system property value cannot be accessed, or
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   610
     *          if a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   611
     *          java.lang.SecurityManager#checkRead} method denies
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *          read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21278
diff changeset
   614
     * @since   1.1
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   615
     * @see     Path#toRealPath
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public String getCanonicalPath() throws IOException {
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   618
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   619
            throw new IOException("Invalid file path");
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   620
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        return fs.canonicalize(fs.resolve(this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * Returns the canonical form of this abstract pathname.  Equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * <code>new&nbsp;File(this.{@link #getCanonicalPath})</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @return  The canonical pathname string denoting the same file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *          directory as this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *          If an I/O error occurs, which is possible because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *          construction of the canonical pathname may require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     *          filesystem queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *          If a required system property value cannot be accessed, or
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   638
     *          if a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   639
     *          java.lang.SecurityManager#checkRead} method denies
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *          read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @since 1.2
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   643
     * @see     Path#toRealPath
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public File getCanonicalFile() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        String canonPath = getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        return new File(canonPath, fs.prefixLength(canonPath));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    private static String slashify(String path, boolean isDirectory) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        String p = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        if (File.separatorChar != '/')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            p = p.replace(File.separatorChar, '/');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        if (!p.startsWith("/"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            p = "/" + p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (!p.endsWith("/") && isDirectory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            p = p + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   662
     * Converts this abstract pathname into a {@code file:} URL.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * exact form of the URL is system-dependent.  If it can be determined that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * the file denoted by this abstract pathname is a directory, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * resulting URL will end with a slash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @return  A URL object representing the equivalent file URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @throws  MalformedURLException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *          If the path cannot be parsed as a URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @see     #toURI()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @see     java.net.URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @see     java.net.URI#toURL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * @see     java.net.URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @deprecated This method does not automatically escape characters that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * are illegal in URLs.  It is recommended that new code convert an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * abstract pathname into a URL by first converting it into a URI, via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * {@link #toURI() toURI} method, and then converting the URI into a URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * via the {@link java.net.URI#toURL() URI.toURL} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public URL toURL() throws MalformedURLException {
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   686
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   687
            throw new MalformedURLException("Invalid file path");
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   688
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        return new URL("file", "", slashify(getAbsolutePath(), isDirectory()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    /**
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   693
     * Constructs a {@code file:} URI that represents this abstract pathname.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * <p> The exact form of the URI is system-dependent.  If it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * determined that the file denoted by this abstract pathname is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * directory, then the resulting URI will end with a slash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * <p> For a given abstract pathname <i>f</i>, it is guaranteed that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   701
     * <blockquote><code>
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   702
     * new {@link #File(java.net.URI) File}(</code><i>&nbsp;f</i><code>.toURI()).equals(
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   703
     * </code><i>&nbsp;f</i><code>.{@link #getAbsoluteFile() getAbsoluteFile}())
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   704
     * </code></blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * so long as the original abstract pathname, the URI, and the new abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * pathname are all created in (possibly different invocations of) the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     * Java virtual machine.  Due to the system-dependent nature of abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * pathnames, however, this relationship typically does not hold when a
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   710
     * {@code file:} URI that is created in a virtual machine on one operating
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * system is converted into an abstract pathname in a virtual machine on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * different operating system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   714
     * <p> Note that when this abstract pathname represents a UNC pathname then
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   715
     * all components of the UNC (including the server name component) are encoded
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   716
     * in the {@code URI} path. The authority component is undefined, meaning
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   717
     * that it is represented as {@code null}. The {@link Path} class defines the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   718
     * {@link Path#toUri toUri} method to encode the server name in the authority
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   719
     * component of the resulting {@code URI}. The {@link #toPath toPath} method
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   720
     * may be used to obtain a {@code Path} representing this abstract pathname.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
   721
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @return  An absolute, hierarchical URI with a scheme equal to
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   723
     *          {@code "file"}, a path representing this abstract pathname,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *          and undefined authority, query, and fragment components
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @throws SecurityException If a required system property value cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * be accessed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @see #File(java.net.URI)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @see java.net.URI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @see java.net.URI#toURL()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    public URI toURI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            File f = getAbsoluteFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            String sp = slashify(f.getPath(), f.isDirectory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (sp.startsWith("//"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                sp = "//" + sp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            return new URI("file", null, sp, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        } catch (URISyntaxException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            throw new Error(x);         // Can't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    /* -- Attribute accessors -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Tests whether the application can read the file denoted by this
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
   750
     * abstract pathname. On some platforms it may be possible to start the
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
   751
     * Java virtual machine with special privileges that allow it to read
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
   752
     * files that are marked as unreadable. Consequently this method may return
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
   753
     * {@code true} even though the file does not have read permissions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   755
     * @return  {@code true} if and only if the file specified by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *          abstract pathname exists <em>and</em> can be read by the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   757
     *          application; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   760
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   761
     *          java.lang.SecurityManager#checkRead(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    public boolean canRead() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   769
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   770
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   771
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return fs.checkAccess(this, FileSystem.ACCESS_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     * Tests whether the application can modify the file denoted by this
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
   777
     * abstract pathname. On some platforms it may be possible to start the
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
   778
     * Java virtual machine with special privileges that allow it to modify
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
   779
     * files that are marked read-only. Consequently this method may return
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
   780
     * {@code true} even though the file is marked read-only.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   782
     * @return  {@code true} if and only if the file system actually
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *          contains a file denoted by this abstract pathname <em>and</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     *          the application is allowed to write to the file;
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   785
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   788
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   789
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    public boolean canWrite() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   797
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   798
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   799
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        return fs.checkAccess(this, FileSystem.ACCESS_WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * Tests whether the file or directory denoted by this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   807
     * @return  {@code true} if and only if the file or directory denoted
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   808
     *          by this abstract pathname exists; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   811
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   812
     *          java.lang.SecurityManager#checkRead(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *          method denies read access to the file or directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public boolean exists() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   820
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   821
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   822
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * Tests whether the file denoted by this abstract pathname is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   830
     * <p> Where it is required to distinguish an I/O exception from the case
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   831
     * that the file is not a directory, or where several attributes of the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   832
     * same file are required at the same time, then the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   833
     * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   834
     * Files.readAttributes} method may be used.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   835
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   836
     * @return {@code true} if and only if the file denoted by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *          abstract pathname exists <em>and</em> is a directory;
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   838
     *          {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   841
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   842
     *          java.lang.SecurityManager#checkRead(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    public boolean isDirectory() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   850
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   851
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   852
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        return ((fs.getBooleanAttributes(this) & FileSystem.BA_DIRECTORY)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * Tests whether the file denoted by this abstract pathname is a normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * file.  A file is <em>normal</em> if it is not a directory and, in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * addition, satisfies other system-dependent criteria.  Any non-directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * file created by a Java application is guaranteed to be a normal file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   863
     * <p> Where it is required to distinguish an I/O exception from the case
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   864
     * that the file is not a normal file, or where several attributes of the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   865
     * same file are required at the same time, then the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   866
     * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   867
     * Files.readAttributes} method may be used.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   868
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   869
     * @return  {@code true} if and only if the file denoted by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     *          abstract pathname exists <em>and</em> is a normal file;
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   871
     *          {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   874
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   875
     *          java.lang.SecurityManager#checkRead(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
    public boolean isFile() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   883
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   884
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   885
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        return ((fs.getBooleanAttributes(this) & FileSystem.BA_REGULAR) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * Tests whether the file named by this abstract pathname is a hidden
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * file.  The exact definition of <em>hidden</em> is system-dependent.  On
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * UNIX systems, a file is considered to be hidden if its name begins with
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   893
     * a period character ({@code '.'}).  On Microsoft Windows systems, a file is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * considered to be hidden if it has been marked as such in the filesystem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   896
     * @return  {@code true} if and only if the file denoted by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *          abstract pathname is hidden according to the conventions of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     *          underlying platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   901
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   902
     *          java.lang.SecurityManager#checkRead(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    public boolean isHidden() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   912
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   913
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   914
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        return ((fs.getBooleanAttributes(this) & FileSystem.BA_HIDDEN) != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * Returns the time that the file denoted by this abstract pathname was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * last modified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
45639
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   922
     * @apiNote
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   923
     * While the unit of time of the return value is milliseconds, the
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   924
     * granularity of the value depends on the underlying file system and may
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   925
     * be larger.  For example, some file systems use time stamps in units of
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   926
     * seconds.
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   927
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   928
     * <p> Where it is required to distinguish an I/O exception from the case
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   929
     * where {@code 0L} is returned, or where several attributes of the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   930
     * same file are required at the same time, or where the time of last
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   931
     * access or the creation time are required, then the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   932
     * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
45639
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   933
     * Files.readAttributes} method may be used.  If however only the
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   934
     * time of last modification is required, then the
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   935
     * {@link java.nio.file.Files#getLastModifiedTime(Path,LinkOption[])
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   936
     * Files.getLastModifiedTime} method may be used instead.
45251
f7d47a5e1e99 7086489: File.lastModified should accuracy as well as resolution
bpb
parents: 44844
diff changeset
   937
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   938
     * @return  A {@code long} value representing the time the file was
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *          last modified, measured in milliseconds since the epoch
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   940
     *          (00:00:00 GMT, January 1, 1970), or {@code 0L} if the
45639
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   941
     *          file does not exist or if an I/O error occurs.  The value may
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   942
     *          be negative indicating the number of milliseconds before the
a5e12e2c192a 6791812: (file spec) Incompatible File.lastModified() and setLastModified() for negative time
bpb
parents: 45251
diff changeset
   943
     *          epoch
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   946
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   947
     *          java.lang.SecurityManager#checkRead(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
    public long lastModified() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   955
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   956
            return 0L;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   957
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        return fs.getLastModifiedTime(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * Returns the length of the file denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * The return value is unspecified if this pathname denotes a directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   965
     * <p> Where it is required to distinguish an I/O exception from the case
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   966
     * that {@code 0L} is returned, or where several attributes of the same file
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   967
     * are required at the same time, then the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   968
     * java.nio.file.Files#readAttributes(Path,Class,LinkOption[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   969
     * Files.readAttributes} method may be used.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
   970
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @return  The length, in bytes, of the file denoted by this abstract
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   972
     *          pathname, or {@code 0L} if the file does not exist.  Some
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
   973
     *          operating systems may return {@code 0L} for pathnames
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *          denoting system-dependent entities such as devices or pipes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   977
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
   978
     *          java.lang.SecurityManager#checkRead(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *          method denies read access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
    public long length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   986
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   987
            return 0L;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
   988
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        return fs.getLength(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
    /* -- File operations -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     * Atomically creates a new, empty file named by this abstract pathname if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * and only if a file with this name does not yet exist.  The check for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     * existence of the file and the creation of the file if it does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
     * are a single operation that is atomic with respect to all other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
     * filesystem activities that might affect the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
     * Note: this method should <i>not</i> be used for file-locking, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
     * the resulting protocol cannot be made to work reliably. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
     * {@link java.nio.channels.FileLock FileLock}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * facility should be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1007
     * @return  {@code true} if the named file does not exist and was
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1008
     *          successfully created; {@code false} if the named file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     *          already exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     *          If an I/O error occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1015
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1016
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
    public boolean createNewFile() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
        if (security != null) security.checkWrite(path);
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1024
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1025
            throw new IOException("Invalid file path");
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1026
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        return fs.createFileExclusively(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * Deletes the file or directory denoted by this abstract pathname.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
     * this pathname denotes a directory, then the directory must be empty in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     * order to be deleted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1035
     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1036
     * java.nio.file.Files#delete(Path) delete} method to throw an {@link IOException}
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1037
     * when a file cannot be deleted. This is useful for error reporting and to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1038
     * diagnose why a file cannot be deleted.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1039
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1040
     * @return  {@code true} if and only if the file or directory is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1041
     *          successfully deleted; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1044
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1045
     *          java.lang.SecurityManager#checkDelete} method denies
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     *          delete access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    public boolean delete() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
            security.checkDelete(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1053
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1054
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1055
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        return fs.delete(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * Requests that the file or directory denoted by this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * pathname be deleted when the virtual machine terminates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * Files (or directories) are deleted in the reverse order that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * they are registered. Invoking this method to delete a file or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * directory that is already registered for deletion has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * Deletion will be attempted only for normal termination of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * virtual machine, as defined by the Java Language Specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * <p> Once deletion has been requested, it is not possible to cancel the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     * request.  This method should therefore be used with care.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * Note: this method should <i>not</i> be used for file-locking, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     * the resulting protocol cannot be made to work reliably. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * {@link java.nio.channels.FileLock FileLock}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * facility should be used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1078
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1079
     *          java.lang.SecurityManager#checkDelete} method denies
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     *          delete access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * @see #delete
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
    public void deleteOnExit() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
            security.checkDelete(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1091
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1092
            return;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1093
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        DeleteOnExitHook.add(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * Returns an array of strings naming the files and directories in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * <p> If this abstract pathname does not denote a directory, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * method returns {@code null}.  Otherwise an array of strings is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * returned, one for each file or directory in the directory.  Names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     * denoting the directory itself and the directory's parent directory are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * not included in the result.  Each string is a file name rather than a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     * complete path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * <p> There is no guarantee that the name strings in the resulting array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * will appear in any specific order; they are not, in particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * guaranteed to appear in alphabetical order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1112
     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1113
     * java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method to
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1114
     * open a directory and iterate over the names of the files in the directory.
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1115
     * This may use less resources when working with very large directories, and
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1116
     * may be more responsive when working with remote directories.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1117
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * @return  An array of strings naming the files and directories in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     *          directory denoted by this abstract pathname.  The array will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     *          empty if the directory is empty.  Returns {@code null} if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *          this abstract pathname does not denote a directory, or if an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     *          I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     *          the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    public String[] list() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
            security.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1134
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1135
            return null;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1136
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        return fs.list(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     * Returns an array of strings naming the files and directories in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
     * directory denoted by this abstract pathname that satisfy the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
     * filter.  The behavior of this method is the same as that of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
     * {@link #list()} method, except that the strings in the returned array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * must satisfy the filter.  If the given {@code filter} is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * then all names are accepted.  Otherwise, a name satisfies the filter if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * and only if the value {@code true} results when the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * FilenameFilter#accept FilenameFilter.accept(File,&nbsp;String)} method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * of the filter is invoked on this abstract pathname and the name of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * file or directory in the directory that it denotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * @param  filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *         A filename filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @return  An array of strings naming the files and directories in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     *          directory denoted by this abstract pathname that were accepted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     *          by the given {@code filter}.  The array will be empty if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     *          directory is empty or if no names were accepted by the filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     *          Returns {@code null} if this abstract pathname does not denote
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *          a directory, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *          the directory
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1166
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1167
     * @see java.nio.file.Files#newDirectoryStream(Path,String)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    public String[] list(FilenameFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        String names[] = list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        if ((names == null) || (filter == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        }
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1174
        List<String> v = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        for (int i = 0 ; i < names.length ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            if (filter.accept(this, names[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
                v.add(names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1180
        return v.toArray(new String[v.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * Returns an array of abstract pathnames denoting the files in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     * directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * <p> If this abstract pathname does not denote a directory, then this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * method returns {@code null}.  Otherwise an array of {@code File} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * is returned, one for each file or directory in the directory.  Pathnames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * denoting the directory itself and the directory's parent directory are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * not included in the result.  Each resulting abstract pathname is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * constructed from this abstract pathname using the {@link #File(File,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     * String) File(File,&nbsp;String)} constructor.  Therefore if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * pathname is absolute then each resulting pathname is absolute; if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * pathname is relative then each resulting pathname will be relative to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * the same directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * <p> There is no guarantee that the name strings in the resulting array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * will appear in any specific order; they are not, in particular,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * guaranteed to appear in alphabetical order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1202
     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1203
     * java.nio.file.Files#newDirectoryStream(Path) newDirectoryStream} method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1204
     * to open a directory and iterate over the names of the files in the
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1205
     * directory. This may use less resources when working with very large
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1206
     * directories.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1207
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     * @return  An array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *          directories in the directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     *          The array will be empty if the directory is empty.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     *          {@code null} if this abstract pathname does not denote a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     *          directory, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     *          the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @since  1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
    public File[] listFiles() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        String[] ss = list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        if (ss == null) return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        int n = ss.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        File[] fs = new File[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        for (int i = 0; i < n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            fs[i] = new File(ss[i], this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        return fs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * Returns an array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * directories in the directory denoted by this abstract pathname that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * satisfy the specified filter.  The behavior of this method is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * as that of the {@link #listFiles()} method, except that the pathnames in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     * the returned array must satisfy the filter.  If the given {@code filter}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * is {@code null} then all pathnames are accepted.  Otherwise, a pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * satisfies the filter if and only if the value {@code true} results when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     * the {@link FilenameFilter#accept
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * FilenameFilter.accept(File,&nbsp;String)} method of the filter is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * invoked on this abstract pathname and the name of a file or directory in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * the directory that it denotes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
     * @param  filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *         A filename filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * @return  An array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     *          directories in the directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     *          The array will be empty if the directory is empty.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     *          {@code null} if this abstract pathname does not denote a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     *          directory, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
     *          the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @since  1.2
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1260
     * @see java.nio.file.Files#newDirectoryStream(Path,String)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    public File[] listFiles(FilenameFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        String ss[] = list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        if (ss == null) return null;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1265
        ArrayList<File> files = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        for (String s : ss)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            if ((filter == null) || filter.accept(this, s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                files.add(new File(s, this));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        return files.toArray(new File[files.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * Returns an array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * directories in the directory denoted by this abstract pathname that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     * satisfy the specified filter.  The behavior of this method is the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * as that of the {@link #listFiles()} method, except that the pathnames in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * the returned array must satisfy the filter.  If the given {@code filter}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * is {@code null} then all pathnames are accepted.  Otherwise, a pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * satisfies the filter if and only if the value {@code true} results when
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * the {@link FileFilter#accept FileFilter.accept(File)} method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * filter is invoked on the pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * @param  filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *         A file filter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     * @return  An array of abstract pathnames denoting the files and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     *          directories in the directory denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *          The array will be empty if the directory is empty.  Returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     *          {@code null} if this abstract pathname does not denote a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *          directory, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     *          If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     *          SecurityManager#checkRead(String)} method denies read access to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *          the directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * @since  1.2
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1298
     * @see java.nio.file.Files#newDirectoryStream(Path,java.nio.file.DirectoryStream.Filter)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    public File[] listFiles(FileFilter filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        String ss[] = list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
        if (ss == null) return null;
7803
56bc97d69d93 6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents: 5506
diff changeset
  1303
        ArrayList<File> files = new ArrayList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        for (String s : ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            File f = new File(s, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            if ((filter == null) || filter.accept(f))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                files.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        return files.toArray(new File[files.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * Creates the directory named by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1315
     * @return  {@code true} if and only if the directory was
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1316
     *          created; {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1319
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1320
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *          method does not permit the named directory to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    public boolean mkdir() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1328
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1329
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1330
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        return fs.createDirectory(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     * Creates the directory named by this abstract pathname, including any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * necessary but nonexistent parent directories.  Note that if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * operation fails it may have succeeded in creating some of the necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * parent directories.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1340
     * @return  {@code true} if and only if the directory was created,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1341
     *          along with all necessary parent directories; {@code false}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     *          otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1345
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1346
     *          java.lang.SecurityManager#checkRead(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     *          method does not permit verification of the existence of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     *          named directory and all necessary parent directories; or if
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1349
     *          the {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1350
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     *          method does not permit the named directory and all necessary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *          parent directories to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
    public boolean mkdirs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        if (exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        if (mkdir()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        File canonFile = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
            canonFile = getCanonicalFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
        File parent = canonFile.getParentFile();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        return (parent != null && (parent.mkdirs() || parent.exists()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                canonFile.mkdir());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * Renames the file denoted by this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * <p> Many aspects of the behavior of this method are inherently
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     * platform-dependent: The rename operation might not be able to move a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * file from one filesystem to another, it might not be atomic, and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * might not succeed if a file with the destination abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * already exists.  The return value should always be checked to make sure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * that the rename operation was successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1383
     * <p> Note that the {@link java.nio.file.Files} class defines the {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1384
     * java.nio.file.Files#move move} method to move or rename a file in a
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1385
     * platform independent manner.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1386
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * @param  dest  The new abstract pathname for the named file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1389
     * @return  {@code true} if and only if the renaming succeeded;
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1390
     *          {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1393
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1394
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     *          method denies write access to either the old or new pathnames
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * @throws  NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1398
     *          If parameter {@code dest} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    public boolean renameTo(File dest) {
53258
e348b0160d61 8216172: File.renameTo(File dest) should check for NPE at the very beginning
bpb
parents: 52902
diff changeset
  1401
        if (dest == null) {
e348b0160d61 8216172: File.renameTo(File dest) should check for NPE at the very beginning
bpb
parents: 52902
diff changeset
  1402
            throw new NullPointerException();
e348b0160d61 8216172: File.renameTo(File dest) should check for NPE at the very beginning
bpb
parents: 52902
diff changeset
  1403
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            security.checkWrite(dest.path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1409
        if (this.isInvalid() || dest.isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1410
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1411
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        return fs.rename(this, dest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * Sets the last-modified time of the file or directory named by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * <p> All platforms support file-modification times to the nearest second,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * but some provide more precision.  The argument will be truncated to fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * the supported precision.  If the operation succeeds and no intervening
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * operations on the file take place, then the next invocation of the
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1423
     * {@link #lastModified} method will return the (possibly
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1424
     * truncated) {@code time} argument that was passed to this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @param  time  The new last-modified time, measured in milliseconds since
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     *               the epoch (00:00:00 GMT, January 1, 1970)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1429
     * @return {@code true} if and only if the operation succeeded;
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1430
     *          {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * @throws  IllegalArgumentException  If the argument is negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1435
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1436
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     *          method denies write access to the named file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
    public boolean setLastModified(long time) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        if (time < 0) throw new IllegalArgumentException("Negative time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1447
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1448
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1449
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        return fs.setLastModifiedTime(this, time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * Marks the file or directory named by this abstract pathname so that
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1455
     * only read operations are allowed. After invoking this method the file
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1456
     * or directory will not change until it is either deleted or marked
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1457
     * to allow write access. On some platforms it may be possible to start the
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1458
     * Java virtual machine with special privileges that allow it to modify
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1459
     * files that are marked read-only. Whether or not a read-only file or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     * directory may be deleted depends upon the underlying system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1462
     * @return {@code true} if and only if the operation succeeded;
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1463
     *          {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1466
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1467
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     *          method denies write access to the named file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
    public boolean setReadOnly() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1477
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1478
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1479
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        return fs.setReadOnly(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1483
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * Sets the owner's or everybody's write permission for this abstract
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1485
     * pathname. On some platforms it may be possible to start the Java virtual
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1486
     * machine with special privileges that allow it to modify files that
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1487
     * disallow write operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1489
     * <p> The {@link java.nio.file.Files} class defines methods that operate on
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1490
     * file attributes including file permissions. This may be used when finer
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1491
     * manipulation of file permissions is required.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1492
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * @param   writable
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1494
     *          If {@code true}, sets the access permission to allow write
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1495
     *          operations; if {@code false} to disallow write operations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @param   ownerOnly
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1498
     *          If {@code true}, the write permission applies only to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
     *          owner's write permission; otherwise, it applies to everybody.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     *          the underlying file system can not distinguish the owner's write
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     *          permission from that of others, then the permission will apply to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     *          everybody, regardless of this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1504
     * @return  {@code true} if and only if the operation succeeded. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     *          operation will fail if the user does not have permission to change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     *          the access permissions of this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1509
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1510
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     *          method denies write access to the named file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    public boolean setWritable(boolean writable, boolean ownerOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1520
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1521
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1522
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        return fs.setPermission(this, FileSystem.ACCESS_WRITE, writable, ownerOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
     * A convenience method to set the owner's write permission for this abstract
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1528
     * pathname. On some platforms it may be possible to start the Java virtual
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1529
     * machine with special privileges that allow it to modify files that
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1530
     * disallow write operations.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1532
     * <p> An invocation of this method of the form {@code file.setWritable(arg)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1535
     * <pre>{@code
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1536
     *     file.setWritable(arg, true)
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1537
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     * @param   writable
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1540
     *          If {@code true}, sets the access permission to allow write
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1541
     *          operations; if {@code false} to disallow write operations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1543
     * @return  {@code true} if and only if the operation succeeded.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     *          operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     *          change the access permissions of this abstract pathname.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1548
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1549
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    public boolean setWritable(boolean writable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        return setWritable(writable, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * Sets the owner's or everybody's read permission for this abstract
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1560
     * pathname. On some platforms it may be possible to start the Java virtual
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1561
     * machine with special privileges that allow it to read files that are
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1562
     * marked as unreadable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1564
     * <p> The {@link java.nio.file.Files} class defines methods that operate on
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1565
     * file attributes including file permissions. This may be used when finer
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1566
     * manipulation of file permissions is required.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1567
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * @param   readable
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1569
     *          If {@code true}, sets the access permission to allow read
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1570
     *          operations; if {@code false} to disallow read operations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * @param   ownerOnly
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1573
     *          If {@code true}, the read permission applies only to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     *          owner's read permission; otherwise, it applies to everybody.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     *          the underlying file system can not distinguish the owner's read
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     *          permission from that of others, then the permission will apply to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     *          everybody, regardless of this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1579
     * @return  {@code true} if and only if the operation succeeded.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     *          operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     *          change the access permissions of this abstract pathname.  If
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1582
     *          {@code readable} is {@code false} and the underlying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     *          file system does not implement a read permission, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     *          operation will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1587
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1588
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    public boolean setReadable(boolean readable, boolean ownerOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1598
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1599
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1600
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        return fs.setPermission(this, FileSystem.ACCESS_READ, readable, ownerOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * A convenience method to set the owner's read permission for this abstract
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1606
     * pathname. On some platforms it may be possible to start the Java virtual
27263
819f5f87d485 8023173: FileDescriptor should respect append flag
igerasim
parents: 25859
diff changeset
  1607
     * machine with special privileges that allow it to read files that are
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1608
     * marked as unreadable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1610
     * <p>An invocation of this method of the form {@code file.setReadable(arg)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1613
     * <pre>{@code
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1614
     *     file.setReadable(arg, true)
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1615
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * @param  readable
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1618
     *          If {@code true}, sets the access permission to allow read
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1619
     *          operations; if {@code false} to disallow read operations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1621
     * @return  {@code true} if and only if the operation succeeded.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     *          operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     *          change the access permissions of this abstract pathname.  If
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1624
     *          {@code readable} is {@code false} and the underlying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *          file system does not implement a read permission, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     *          operation will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1629
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1630
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    public boolean setReadable(boolean readable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        return setReadable(readable, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * Sets the owner's or everybody's execute permission for this abstract
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1641
     * pathname. On some platforms it may be possible to start the Java virtual
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1642
     * machine with special privileges that allow it to execute files that are
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1643
     * not marked executable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1645
     * <p> The {@link java.nio.file.Files} class defines methods that operate on
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1646
     * file attributes including file permissions. This may be used when finer
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1647
     * manipulation of file permissions is required.
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1648
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * @param   executable
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1650
     *          If {@code true}, sets the access permission to allow execute
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1651
     *          operations; if {@code false} to disallow execute operations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * @param   ownerOnly
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1654
     *          If {@code true}, the execute permission applies only to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     *          owner's execute permission; otherwise, it applies to everybody.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     *          If the underlying file system can not distinguish the owner's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     *          execute permission from that of others, then the permission will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     *          apply to everybody, regardless of this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1660
     * @return  {@code true} if and only if the operation succeeded.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     *          operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     *          change the access permissions of this abstract pathname.  If
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1663
     *          {@code executable} is {@code false} and the underlying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     *          file system does not implement an execute permission, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     *          operation will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1668
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1669
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    public boolean setExecutable(boolean executable, boolean ownerOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
            security.checkWrite(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1679
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1680
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1681
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        return fs.setPermission(this, FileSystem.ACCESS_EXECUTE, executable, ownerOnly);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
    /**
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1686
     * A convenience method to set the owner's execute permission for this
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1687
     * abstract pathname. On some platforms it may be possible to start the Java
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1688
     * virtual machine with special privileges that allow it to execute files
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1689
     * that are not marked executable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1691
     * <p>An invocation of this method of the form {@code file.setExcutable(arg)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1694
     * <pre>{@code
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1695
     *     file.setExecutable(arg, true)
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1696
     * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * @param   executable
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1699
     *          If {@code true}, sets the access permission to allow execute
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1700
     *          operations; if {@code false} to disallow execute operations
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1702
     * @return   {@code true} if and only if the operation succeeded.  The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     *           operation will fail if the user does not have permission to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     *           change the access permissions of this abstract pathname.  If
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1705
     *           {@code executable} is {@code false} and the underlying
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20478
diff changeset
  1706
     *           file system does not implement an execute permission, then the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     *           operation will fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1710
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1711
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     *          method denies write access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
    public boolean setExecutable(boolean executable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        return setExecutable(executable, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * Tests whether the application can execute the file denoted by this
13568
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1722
     * abstract pathname. On some platforms it may be possible to start the
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1723
     * Java virtual machine with special privileges that allow it to execute
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1724
     * files that are not marked executable. Consequently this method may return
ce5ab758aeb5 6931128: (spec) File attribute tests fail when run as root.
robm
parents: 11907
diff changeset
  1725
     * {@code true} even though the file does not have execute permissions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  1727
     * @return  {@code true} if and only if the abstract pathname exists
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     *          <em>and</em> the application is allowed to execute the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1731
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1732
     *          java.lang.SecurityManager#checkExec(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     *          method denies execute access to the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    public boolean canExecute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            security.checkExec(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1742
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1743
            return false;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1744
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        return fs.checkAccess(this, FileSystem.ACCESS_EXECUTE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    /* -- Filesystem interface -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * List the available filesystem roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * <p> A particular Java platform may support zero or more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * hierarchically-organized file systems.  Each file system has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * {@code root} directory from which all other files in that file system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * can be reached.  Windows platforms, for example, have a root directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * for each active drive; UNIX platforms have a single root directory,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * namely {@code "/"}.  The set of available filesystem roots is affected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * by various system-level operations such as the insertion or ejection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * removable media and the disconnecting or unmounting of physical or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * virtual disk drives.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * <p> This method returns an array of {@code File} objects that denote the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * root directories of the available filesystem roots.  It is guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * that the canonical pathname of any file physically present on the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * machine will begin with one of the roots returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * <p> The canonical pathname of a file that resides on some other machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * and is accessed via a remote-filesystem protocol such as SMB or NFS may
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * or may not begin with one of the roots returned by this method.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * pathname of a remote file is syntactically indistinguishable from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * pathname of a local file then it will begin with one of the roots
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * returned by this method.  Thus, for example, {@code File} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * denoting the root directories of the mapped network drives of a Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     * platform will be returned by this method, while {@code File} objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
     * containing UNC pathnames will not be returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
     * <p> Unlike most methods in this class, this method does not throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
     * security exceptions.  If a security manager exists and its {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
     * SecurityManager#checkRead(String)} method denies read access to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
     * particular root directory, then that directory will not appear in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     * @return  An array of {@code File} objects denoting the available
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
     *          filesystem roots, or {@code null} if the set of roots could not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     *          be determined.  The array will be empty if there are no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     *          filesystem roots.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     * @since  1.2
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1791
     * @see java.nio.file.FileStore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    public static File[] listRoots() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        return fs.listRoots();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
    /* -- Disk usage -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
     * Returns the size of the partition <a href="#partName">named</a> by this
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1802
     * abstract pathname. If the total number of bytes in the partition is
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1803
     * greater than {@link Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1804
     * returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1806
     * @return  The size, in bytes, of the partition or {@code 0L} if this
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1807
     *          abstract pathname does not name a partition or if the size
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1808
     *          cannot be obtained
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     *          If a security manager has been installed and it denies
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1812
     *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     *          or its {@link SecurityManager#checkRead(String)} method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     *          read access to the file named by this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * @since  1.6
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1817
     * @see FileStore#getTotalSpace
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    public long getTotalSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
            sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            sm.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1825
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1826
            return 0L;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1827
        }
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1828
        long space = fs.getSpace(this, FileSystem.SPACE_TOTAL);
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1829
        return space >= 0L ? space : Long.MAX_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     * Returns the number of unallocated bytes in the partition <a
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1834
     * href="#partName">named</a> by this abstract path name.  If the
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1835
     * number of unallocated bytes in the partition is greater than
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1836
     * {@link Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
     * <p> The returned number of unallocated bytes is a hint, but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
     * a guarantee, that it is possible to use most or any of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
     * bytes.  The number of unallocated bytes is most likely to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
     * accurate immediately after this call.  It is likely to be made
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
     * inaccurate by any external I/O operations including those made
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
     * on the system outside of this virtual machine.  This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * makes no guarantee that write operations to this file system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * will succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1847
     * @return  The number of unallocated bytes on the partition or {@code 0L}
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1848
     *          if the abstract pathname does not name a partition or if this
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1849
     *          number cannot be obtained.  This value will be less than or
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1850
     *          equal to the total file system size returned by
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1851
     *          {@link #getTotalSpace}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
     *          If a security manager has been installed and it denies
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1855
     *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
     *          or its {@link SecurityManager#checkRead(String)} method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
     *          read access to the file named by this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * @since  1.6
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1860
     * @see FileStore#getUnallocatedSpace
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
    public long getFreeSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
            sm.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1868
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1869
            return 0L;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1870
        }
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1871
        long space = fs.getSpace(this, FileSystem.SPACE_FREE);
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1872
        return space >= 0L ? space : Long.MAX_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     * Returns the number of bytes available to this virtual machine on the
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1877
     * partition <a href="#partName">named</a> by this abstract pathname.  If
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1878
     * the number of available bytes in the partition is greater than
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1879
     * {@link Long#MAX_VALUE}, then {@code Long.MAX_VALUE} will be returned.
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1880
     * When possible, this method checks for write permissions and other
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1881
     * operating system restrictions and will therefore usually provide a more
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1882
     * accurate estimate of how much new data can actually be written than
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1883
     * {@link #getFreeSpace}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * <p> The returned number of available bytes is a hint, but not a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     * guarantee, that it is possible to use most or any of these bytes.  The
54441
03ea2b6428f0 8221597: A typo in the Java API doc for File.getUsableSpace()
bpb
parents: 54206
diff changeset
  1887
     * number of available bytes is most likely to be accurate immediately
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * after this call.  It is likely to be made inaccurate by any external
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * I/O operations including those made on the system outside of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     * virtual machine.  This method makes no guarantee that write operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
     * to this file system will succeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
     *
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1893
     * @return  The number of available bytes on the partition or {@code 0L}
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1894
     *          if the abstract pathname does not name a partition or if this
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1895
     *          number cannot be obtained.  On systems where this information
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1896
     *          is not available, this method will be equivalent to a call to
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1897
     *          {@link #getFreeSpace}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * @throws  SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     *          If a security manager has been installed and it denies
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  1901
     *          {@link RuntimePermission}{@code ("getFileSystemAttributes")}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     *          or its {@link SecurityManager#checkRead(String)} method denies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *          read access to the file named by this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * @since  1.6
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1906
     * @see FileStore#getUsableSpace
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
    public long getUsableSpace() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
            sm.checkPermission(new RuntimePermission("getFileSystemAttributes"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            sm.checkRead(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        }
17430
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1914
        if (isInvalid()) {
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1915
            return 0L;
c445531b8f6b 8003992: File and other classes in java.io do not handle embedded nulls properly
dxu
parents: 14324
diff changeset
  1916
        }
59263
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1917
        long space = fs.getSpace(this, FileSystem.SPACE_USABLE);
f34ad283fcd6 8179320: File.getUsableSpace() returns a negative number on very large file system
bpb
parents: 58288
diff changeset
  1918
        return space >= 0L ? space : Long.MAX_VALUE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    /* -- Temporary files -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  1923
    private static class TempDirectory {
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  1924
        private TempDirectory() { }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  1926
        // temporary directory location
37593
824750ada3d6 8154231: Simplify access to System properties from JDK code
redestad
parents: 34782
diff changeset
  1927
        private static final File tmpdir = new File(
37781
71ed5645f17c 8155775: Re-examine naming of privileged methods to access System properties
redestad
parents: 37593
diff changeset
  1928
                GetPropertyAction.privilegedGetProperty("java.io.tmpdir"));
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  1929
        static File location() {
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  1930
            return tmpdir;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  1931
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1933
        // file name generation
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1934
        private static final SecureRandom random = new SecureRandom();
42777
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1935
        private static int shortenSubName(int subNameLength, int excess,
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1936
            int nameMin) {
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1937
            int newLength = Math.max(nameMin, subNameLength - excess);
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1938
            if (newLength < subNameLength) {
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1939
                return newLength;
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1940
            }
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1941
            return subNameLength;
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1942
        }
18157
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents: 17430
diff changeset
  1943
        static File generateFile(String prefix, String suffix, File dir)
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents: 17430
diff changeset
  1944
            throws IOException
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents: 17430
diff changeset
  1945
        {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  1946
            long n = random.nextLong();
42777
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1947
            String nus = Long.toUnsignedString(n);
20478
56135e8cbc46 8025128: File.createTempFile fails if prefix is absolute path
dxu
parents: 19029
diff changeset
  1948
56135e8cbc46 8025128: File.createTempFile fails if prefix is absolute path
dxu
parents: 19029
diff changeset
  1949
            // Use only the file name from the supplied prefix
56135e8cbc46 8025128: File.createTempFile fails if prefix is absolute path
dxu
parents: 19029
diff changeset
  1950
            prefix = (new File(prefix)).getName();
42777
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1951
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1952
            int prefixLength = prefix.length();
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1953
            int nusLength = nus.length();
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1954
            int suffixLength = suffix.length();;
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1955
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1956
            String name;
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1957
            int nameMax = fs.getNameMax(dir.getPath());
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1958
            int excess = prefixLength + nusLength + suffixLength - nameMax;
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1959
            if (excess <= 0) {
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1960
                name = prefix + nus + suffix;
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1961
            } else {
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1962
                // Name exceeds the maximum path component length: shorten it
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1963
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1964
                // Attempt to shorten the prefix length to no less then 3
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1965
                prefixLength = shortenSubName(prefixLength, excess, 3);
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1966
                excess = prefixLength + nusLength + suffixLength - nameMax;
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1967
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1968
                if (excess > 0) {
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1969
                    // Attempt to shorten the suffix length to no less than
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1970
                    // 0 or 4 depending on whether it begins with a dot ('.')
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1971
                    suffixLength = shortenSubName(suffixLength, excess,
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1972
                        suffix.indexOf(".") == 0 ? 4 : 0);
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1973
                    suffixLength = shortenSubName(suffixLength, excess, 3);
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1974
                    excess = prefixLength + nusLength + suffixLength - nameMax;
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1975
                }
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1976
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1977
                if (excess > 0 && excess <= nusLength - 5) {
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1978
                    // Attempt to shorten the random character string length
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1979
                    // to no less than 5
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1980
                    nusLength = shortenSubName(nusLength, excess, 5);
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1981
                }
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1982
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1983
                StringBuilder sb =
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1984
                    new StringBuilder(prefixLength + nusLength + suffixLength);
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1985
                sb.append(prefixLength < prefix.length() ?
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1986
                    prefix.substring(0, prefixLength) : prefix);
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1987
                sb.append(nusLength < nus.length() ?
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1988
                    nus.substring(0, nusLength) : nus);
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1989
                sb.append(suffixLength < suffix.length() ?
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1990
                    suffix.substring(0, suffixLength) : suffix);
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1991
                name = sb.toString();
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1992
            }
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  1993
43103
18e663e79d31 8152272: Unable to create temporary file using createTempFile method if System.getProperty(file.separator) is used
bpb
parents: 42777
diff changeset
  1994
            // Normalize the path component
18e663e79d31 8152272: Unable to create temporary file using createTempFile method if System.getProperty(file.separator) is used
bpb
parents: 42777
diff changeset
  1995
            name = fs.normalize(name);
18e663e79d31 8152272: Unable to create temporary file using createTempFile method if System.getProperty(file.separator) is used
bpb
parents: 42777
diff changeset
  1996
18157
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents: 17430
diff changeset
  1997
            File f = new File(dir, name);
20478
56135e8cbc46 8025128: File.createTempFile fails if prefix is absolute path
dxu
parents: 19029
diff changeset
  1998
            if (!name.equals(f.getName()) || f.isInvalid()) {
56135e8cbc46 8025128: File.createTempFile fails if prefix is absolute path
dxu
parents: 19029
diff changeset
  1999
                if (System.getSecurityManager() != null)
56135e8cbc46 8025128: File.createTempFile fails if prefix is absolute path
dxu
parents: 19029
diff changeset
  2000
                    throw new IOException("Unable to create temporary file");
56135e8cbc46 8025128: File.createTempFile fails if prefix is absolute path
dxu
parents: 19029
diff changeset
  2001
                else
42777
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  2002
                    throw new IOException("Unable to create temporary file, "
a94fc33e9866 8148023: File.createTempFile is not adhering to the contract regarding file name lengths
bpb
parents: 41881
diff changeset
  2003
                        + name);
20478
56135e8cbc46 8025128: File.createTempFile fails if prefix is absolute path
dxu
parents: 19029
diff changeset
  2004
            }
18157
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents: 17430
diff changeset
  2005
            return f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
     * <p> Creates a new empty file in the specified directory, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
     * given prefix and suffix strings to generate its name.  If this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
     * returns successfully then it is guaranteed that:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * <li> The file denoted by the returned abstract pathname did not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     *      before this method was invoked, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * <li> Neither this method nor any of its variants will return the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     *      abstract pathname again in the current invocation of the virtual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     *      machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     * This method provides only part of a temporary-file facility.  To arrange
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * for a file created by this method to be deleted automatically, use the
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  2024
     * {@link #deleteOnExit} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2026
     * <p> The {@code prefix} argument must be at least three characters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
     * long.  It is recommended that the prefix be a short, meaningful string
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2028
     * such as {@code "hjb"} or {@code "mail"}.  The
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2029
     * {@code suffix} argument may be {@code null}, in which case the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2030
     * suffix {@code ".tmp"} will be used.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * <p> To create the new file, the prefix and the suffix may first be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     * adjusted to fit the limitations of the underlying platform.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * prefix is too long then it will be truncated, but its first three
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * characters will always be preserved.  If the suffix is too long then it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     * too will be truncated, but if it begins with a period character
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2037
     * ({@code '.'}) then the period and the first three characters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
     * following it will always be preserved.  Once these adjustments have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
     * made the name of the new file will be generated by concatenating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
     * prefix, five or more internally-generated characters, and the suffix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2042
     * <p> If the {@code directory} argument is {@code null} then the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * system-dependent default temporary-file directory will be used.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * default temporary-file directory is specified by the system property
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2045
     * {@code java.io.tmpdir}.  On UNIX systems the default value of this
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2046
     * property is typically {@code "/tmp"} or {@code "/var/tmp"}; on
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2047
     * Microsoft Windows systems it is typically {@code "C:\\WINNT\\TEMP"}.  A different
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * value may be given to this system property when the Java virtual machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * is invoked, but programmatic changes to this property are not guaranteed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     * to have any effect upon the temporary directory used by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * @param  prefix     The prefix string to be used in generating the file's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     *                    name; must be at least three characters long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * @param  suffix     The suffix string to be used in generating the file's
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2056
     *                    name; may be {@code null}, in which case the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2057
     *                    suffix {@code ".tmp"} will be used
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
     * @param  directory  The directory in which the file is to be created, or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2060
     *                    {@code null} if the default temporary-file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
     *                    directory is to be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * @return  An abstract pathname denoting a newly-created empty file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * @throws  IllegalArgumentException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2066
     *          If the {@code prefix} argument contains fewer than three
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     *          characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * @throws  IOException  If a file could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  2072
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  2073
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     *          method does not allow a file to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
    public static File createTempFile(String prefix, String suffix,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                                      File directory)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
    {
25223
4d33af5975a5 8048840: File.createTempFile has uninformative failure message
jmanson
parents: 24865
diff changeset
  2082
        if (prefix.length() < 3) {
4d33af5975a5 8048840: File.createTempFile has uninformative failure message
jmanson
parents: 24865
diff changeset
  2083
            throw new IllegalArgumentException("Prefix string \"" + prefix +
4d33af5975a5 8048840: File.createTempFile has uninformative failure message
jmanson
parents: 24865
diff changeset
  2084
                "\" too short: length must be at least 3");
4d33af5975a5 8048840: File.createTempFile has uninformative failure message
jmanson
parents: 24865
diff changeset
  2085
        }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2086
        if (suffix == null)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2087
            suffix = ".tmp";
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2088
18157
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents: 17430
diff changeset
  2089
        File tmpdir = (directory != null) ? directory
ee3bda8e26c6 8013827: File.createTempFile hangs with temp file starting with 'com1.4'
dxu
parents: 17430
diff changeset
  2090
                                          : TempDirectory.location();
18807
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2091
        SecurityManager sm = System.getSecurityManager();
2601
39743edb9b8b 6721753: File.createTempFile produces guessable file names
alanb
parents: 681
diff changeset
  2092
        File f;
18807
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2093
        do {
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2094
            f = TempDirectory.generateFile(prefix, suffix, tmpdir);
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2095
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2096
            if (sm != null) {
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2097
                try {
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2098
                    sm.checkWrite(f.getPath());
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2099
                } catch (SecurityException se) {
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2100
                    // don't reveal temporary directory location
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2101
                    if (directory == null)
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2102
                        throw new SecurityException("Unable to create temporary file");
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2103
                    throw se;
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2104
                }
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2105
            }
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2106
        } while ((fs.getBooleanAttributes(f) & FileSystem.BA_EXISTS) != 0);
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2107
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2108
        if (!fs.createFileExclusively(f.getPath()))
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2109
            throw new IOException("Unable to create temporary file");
7d65f90d7348 8017212: File.createTempFile requires unnecessary "read" permission
dxu
parents: 18157
diff changeset
  2110
2601
39743edb9b8b 6721753: File.createTempFile produces guessable file names
alanb
parents: 681
diff changeset
  2111
        return f;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * Creates an empty file in the default temporary-file directory, using
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2116
     * the given prefix and suffix to generate its name. Invoking this method
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  2117
     * is equivalent to invoking {@link #createTempFile(java.lang.String,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * java.lang.String, java.io.File)
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  2119
     * createTempFile(prefix,&nbsp;suffix,&nbsp;null)}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     *
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  2121
     * <p> The {@link
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  2122
     * java.nio.file.Files#createTempFile(String,String,java.nio.file.attribute.FileAttribute[])
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  2123
     * Files.createTempFile} method provides an alternative method to create an
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  2124
     * empty file in the temporary-file directory. Files created by that method
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  2125
     * may have more restrictive access permissions to files created by this
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  2126
     * method and so may be more suited to security-sensitive applications.
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2127
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     * @param  prefix     The prefix string to be used in generating the file's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     *                    name; must be at least three characters long
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * @param  suffix     The suffix string to be used in generating the file's
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2132
     *                    name; may be {@code null}, in which case the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2133
     *                    suffix {@code ".tmp"} will be used
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * @return  An abstract pathname denoting a newly-created empty file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     * @throws  IllegalArgumentException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2138
     *          If the {@code prefix} argument contains fewer than three
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     *          characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * @throws  IOException  If a file could not be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * @throws  SecurityException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  2144
     *          If a security manager exists and its {@link
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  2145
     *          java.lang.SecurityManager#checkWrite(java.lang.String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     *          method does not allow a file to be created
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * @since 1.2
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  2149
     * @see java.nio.file.Files#createTempDirectory(String,FileAttribute[])
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
    public static File createTempFile(String prefix, String suffix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
        return createTempFile(prefix, suffix, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
    /* -- Basic infrastructure -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     * Compares two abstract pathnames lexicographically.  The ordering
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * defined by this method depends upon the underlying system.  On UNIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     * systems it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
     * @param   pathname  The abstract pathname to be compared to this abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
     *                    pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
     * @return  Zero if the argument is equal to this abstract pathname, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     *          value less than zero if this abstract pathname is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     *          lexicographically less than the argument, or a value greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     *          than zero if this abstract pathname is lexicographically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     *          greater than the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
    public int compareTo(File pathname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        return fs.compare(this, pathname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     * Tests this abstract pathname for equality with the given object.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2182
     * Returns {@code true} if and only if the argument is not
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2183
     * {@code null} and is an abstract pathname that denotes the same file
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
     * or directory as this abstract pathname.  Whether or not two abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
     * pathnames are equal depends upon the underlying system.  On UNIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     * systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
     * systems it is not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * @param   obj   The object to be compared with this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2191
     * @return  {@code true} if and only if the objects are the same;
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2192
     *          {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
        if ((obj != null) && (obj instanceof File)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            return compareTo((File)obj) == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     * Computes a hash code for this abstract pathname.  Because equality of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * abstract pathnames is inherently system-dependent, so is the computation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     * of their hash codes.  On UNIX systems, the hash code of an abstract
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
     * pathname is equal to the exclusive <em>or</em> of the hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
     * of its pathname string and the decimal value
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2207
     * {@code 1234321}.  On Microsoft Windows systems, the hash
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     * code is equal to the exclusive <em>or</em> of the hash code of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     * its pathname string converted to lower case and the decimal
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 57956
diff changeset
  2210
     * value {@code 1234321}.  Locale is not taken into account on
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * lowercasing the pathname string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     * @return  A hash code for this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        return fs.hashCode(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * Returns the pathname string of this abstract pathname.  This is just the
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 31061
diff changeset
  2221
     * string returned by the {@link #getPath} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     * @return  The string form of this abstract pathname
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        return getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * WriteObject is called to save this filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
     * The separator character is saved also so it can be replaced
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * in case the path is reconstituted on a different host type.
31061
fead7d86d75f 8081517: minor cleanup for docs
avstepan
parents: 27263
diff changeset
  2233
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     * @serialData  Default fields followed by separator character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57816
diff changeset
  2236
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
    private synchronized void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
        s.defaultWriteObject();
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 9538
diff changeset
  2241
        s.writeChar(separatorChar); // Add the separator character
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
     * readObject is called to restore this filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     * The original separator character is read.  If it is different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
     * than the separator character on this system, then the old separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
     * is replaced by the local separator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57816
diff changeset
  2250
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
    private synchronized void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
         throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
    {
5172
3e3db347f963 6736390: File TOCTOU deserialization vulnerability
alanb
parents: 3065
diff changeset
  2254
        ObjectInputStream.GetField fields = s.readFields();
3e3db347f963 6736390: File TOCTOU deserialization vulnerability
alanb
parents: 3065
diff changeset
  2255
        String pathField = (String)fields.get("path", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        char sep = s.readChar(); // read the previous separator char
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        if (sep != separatorChar)
5172
3e3db347f963 6736390: File TOCTOU deserialization vulnerability
alanb
parents: 3065
diff changeset
  2258
            pathField = pathField.replace(sep, separatorChar);
11907
c021db66251d 7146152: File.path should be final
forax
parents: 11117
diff changeset
  2259
        String path = fs.normalize(pathField);
52220
9c260a6b6471 8207146: Rename jdk.internal.misc.Unsafe::xxxObject to xxxReference
mchung
parents: 48435
diff changeset
  2260
        UNSAFE.putReference(this, PATH_OFFSET, path);
11907
c021db66251d 7146152: File.path should be final
forax
parents: 11117
diff changeset
  2261
        UNSAFE.putIntVolatile(this, PREFIX_LENGTH_OFFSET, fs.prefixLength(path));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
46873
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45251
diff changeset
  2264
    private static final jdk.internal.misc.Unsafe UNSAFE
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45251
diff changeset
  2265
            = jdk.internal.misc.Unsafe.getUnsafe();
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45251
diff changeset
  2266
    private static final long PATH_OFFSET
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45251
diff changeset
  2267
            = UNSAFE.objectFieldOffset(File.class, "path");
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45251
diff changeset
  2268
    private static final long PREFIX_LENGTH_OFFSET
7ac2f551b0d6 8182487: Add Unsafe.objectFieldOffset(Class, String)
redestad
parents: 45251
diff changeset
  2269
            = UNSAFE.objectFieldOffset(File.class, "prefixLength");
11907
c021db66251d 7146152: File.path should be final
forax
parents: 11117
diff changeset
  2270
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
57956
e0b8b019d2f5 8229997: Apply java.io.Serial annotations in java.base
darcy
parents: 57816
diff changeset
  2272
    @java.io.Serial
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
    private static final long serialVersionUID = 301077366599181567L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2275
    // -- Integration with java.nio.file --
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 32033
diff changeset
  2277
    private transient volatile Path filePath;
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2278
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2279
    /**
48435
20fe8cd3179d 8193861: Typos in API documentation of File.toPath() and InetSocketAddress.getAddress()
bpb
parents: 47216
diff changeset
  2280
     * Returns a {@link Path java.nio.file.Path} object constructed from
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2281
     * this abstract path. The resulting {@code Path} is associated with the
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2282
     * {@link java.nio.file.FileSystems#getDefault default-filesystem}.
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2283
     *
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2284
     * <p> The first invocation of this method works as if invoking it were
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2285
     * equivalent to evaluating the expression:
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2286
     * <blockquote><pre>
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2287
     * {@link java.nio.file.FileSystems#getDefault FileSystems.getDefault}().{@link
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2288
     * java.nio.file.FileSystem#getPath getPath}(this.{@link #getPath getPath}());
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2289
     * </pre></blockquote>
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2290
     * Subsequent invocations of this method return the same {@code Path}.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2291
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2292
     * <p> If this abstract pathname is the empty abstract pathname then this
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2293
     * method returns a {@code Path} that may be used to access the current
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2294
     * user directory.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2295
     *
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2296
     * @return  a {@code Path} constructed from this abstract path
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2297
     *
8539
eeb9fc5a68c1 7020888: (file) Miscellaneous and trivial clean-ups (typos and opportunities to use suppressed exceptions)
alanb
parents: 8158
diff changeset
  2298
     * @throws  java.nio.file.InvalidPathException
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2299
     *          if a {@code Path} object cannot be constructed from the abstract
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2300
     *          path (see {@link java.nio.file.FileSystem#getPath FileSystem.getPath})
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2301
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2302
     * @since   1.7
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  2303
     * @see Path#toFile
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2304
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2305
    public Path toPath() {
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2306
        Path result = filePath;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2307
        if (result == null) {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2308
            synchronized (this) {
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2309
                result = filePath;
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2310
                if (result == null) {
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 7803
diff changeset
  2311
                    result = FileSystems.getDefault().getPath(path);
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2312
                    filePath = result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            }
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 715
diff changeset
  2315
        }
3065
452aaa2899fc 6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents: 2624
diff changeset
  2316
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
}