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