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