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