/* * Copyright 2007-2009 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ package java.nio.file; import java.nio.file.attribute.*; import java.nio.channels.SeekableByteChannel; import java.io.IOException; /** * A reference to a file. * *
A {@code FileRef} is an object that locates a file and defines methods to * access the file. The means by which the file is located depends on the * implementation. In many cases, a file is located by a {@link Path} but it may * be located by other means such as a file-system identifier. * *
This interface defines the following operations: *
The {@link #newByteChannel newByteChannel} method * may be used to open a file and obtain a byte channel for reading or * writing.
The {@link #delete delete} method may be used to delete a file. *
The {@link #checkAccess checkAccess} method may be used to check * the existence or accessibility of a file.
The {@link #isSameFile isSameFile} method may be used to test if * two file references locate the same file.
The {@link #getFileStore getFileStore} method may be used to * obtain the {@link FileStore} representing the storage where a file is * located.
Access to associated metadata or file attributes requires an appropriate * {@link FileAttributeView FileAttributeView}. The {@link * #getFileAttributeView(Class,LinkOption[]) getFileAttributeView(Class,LinkOption[])} * method may be used to obtain a file attribute view that defines type-safe * methods to read or update file attributes. The {@link * #getFileAttributeView(String,LinkOption[]) getFileAttributeView(String,LinkOption[])} * method may be used to obtain a file attribute view where dynamic access to * file attributes where required. * *
A {@code FileRef} is immutable and safe for use by multiple concurrent * threads. * * @since 1.7 */ public interface FileRef { /** * Opens the file referenced by this object, returning a seekable byte * channel to access the file. * *
The {@code options} parameter determines how the file is opened. * The {@link StandardOpenOption#READ READ} and {@link StandardOpenOption#WRITE * WRITE} options determine if the file should be opened for reading and/or * writing. If neither option (or the {@link StandardOpenOption#APPEND APPEND} * option) is contained in the array then the file is opened for reading. * By default reading or writing commences at the beginning of the file. * *
In the addition to {@code READ} and {@code WRITE}, the following * options may be present: * *
| Option | Description |
|---|---|
| {@link StandardOpenOption#APPEND APPEND} | *If this option is present then the file is opened for writing and * each invocation of the channel's {@code write} method first advances * the position to the end of the file and then writes the requested * data. Whether the advancement of the position and the writing of the * data are done in a single atomic operation is system-dependent and * therefore unspecified. This option may not be used in conjunction * with the {@code READ} or {@code TRUNCATE_EXISTING} options. | *
| {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} | *If this option is present then the existing file is truncated to * a size of 0 bytes. This option is ignored when the file is opened only * for reading. | *
| {@link StandardOpenOption#SYNC SYNC} | *Requires that every update to the file's content or metadata be * written synchronously to the underlying storage device. (see Synchronized I/O file * integrity). | *
| {@link StandardOpenOption#DSYNC DSYNC} | *Requires that every update to the file's content be written * synchronously to the underlying storage device. (see Synchronized I/O file * integrity). | *
An implementation of this interface may support additional options * defined by the {@link StandardOpenOption} enumeration type or other * implementation specific options. * *
The {@link java.nio.channels.Channels} utility classes defines methods * to construct input and output streams where inter-operation with the * {@link java.io} package is required. * * @param options * Options specifying how the file is opened * * @return a new seekable byte channel * * @throws IllegalArgumentException * If an invalid combination of options is specified * @throws UnsupportedOperationException * If an unsupported open option is specified * @throws IOException * If an I/O error occurs * @throws SecurityException * In the case of the default provider, and a security manager is * installed, the {@link SecurityManager#checkRead(String) checkRead} * method is invoked to check read access to the path if the file is * opened for reading. The {@link SecurityManager#checkWrite(String) * checkWrite} method is invoked to check write access to the path * if the file is opened for writing. */ SeekableByteChannel newByteChannel(OpenOption... options) throws IOException; /** * Returns the {@link FileStore} representing the file store where the file * referenced by this object is stored. * *
Once a reference to the {@code FileStore} is obtained it is * implementation specific if operations on the returned {@code FileStore}, * or {@link FileStoreAttributeView} objects obtained from it, continue * to depend on the existence of the file. In particular the behavior is not * defined for the case that the file is deleted or moved to a different * file store. * * @return The file store where the file is stored * * @throws IOException * If an I/O error occurs * @throws SecurityException * In the case of the default provider, and a security manager is * installed, the {@link SecurityManager#checkRead(String) checkRead} * method is invoked to check read access to the file, and in * addition it checks {@link RuntimePermission} * ("getFileStoreAttributes") */ FileStore getFileStore() throws IOException; /** * Checks the existence and optionally the accessibility of the file * referenced by this object. * *
This method checks the existence of a file and that this Java virtual * machine has appropriate privileges that would allow it access the file * according to all of access modes specified in the {@code modes} parameter * as follows: * *
| Value | Description |
|---|---|
| {@link AccessMode#READ READ} | *Checks that the file exists and that the Java virtual machine has * permission to read the file. | *
| {@link AccessMode#WRITE WRITE} | *Checks that the file exists and that the Java virtual machine has * permission to write to the file, | *
| {@link AccessMode#EXECUTE EXECUTE} | *Checks that the file exists and that the Java virtual machine has * permission to {@link Runtime#exec execute} the file. The semantics * may differ when checking access to a directory. For example, on UNIX * systems, checking for {@code EXECUTE} access checks that the Java * virtual machine has permission to search the directory in order to * access file or subdirectories. | *
If the {@code modes} parameter is of length zero, then the existence * of the file is checked. * *
This method follows symbolic links if the file referenced by this * object is a symbolic link. Depending on the implementation, this method * may require to read file permissions, access control lists, or other * file attributes in order to check the effective access to the file. To * determine the effective access to a file may require access to several * attributes and so in some implementations this method may not be atomic * with respect to other file system operations. Furthermore, as the result * of this method is immediately outdated, there is no guarantee that a * subsequence access will succeed (or even that it will access the same * file). Care should be taken when using this method in security sensitive * applications. * * @param modes * The access modes to check; may have zero elements * * @throws UnsupportedOperationException * An implementation is required to support checking for * {@code READ}, {@code WRITE}, and {@code EXECUTE} access. This * exception is specified to allow for the {@code Access} enum to * be extended in future releases. * @throws NoSuchFileException * If a file does not exist (optional specific exception) * @throws AccessDeniedException * The requested access would be denied or the access cannot be * determined because the Java virtual machine has insufficient * privileges or other reasons. (optional specific exception) * @throws IOException * If an I/O error occurs * @throws SecurityException * In the case of the default provider, and a security manager is * installed, the {@link SecurityManager#checkRead(String) checkRead} * is invoked when checking read access to the file or only the * existence of the file, the {@link SecurityManager#checkWrite(String) * checkWrite} is invoked when checking write access to the file, * and {@link SecurityManager#checkExec(String) checkExec} is invoked * when checking execute access. */ void checkAccess(AccessMode... modes) throws IOException; /** * Returns a file attribute view of a given type. * *
A file attribute view provides a read-only or updatable view of a * set of file attributes. This method is intended to be used where the file * attribute view defines type-safe methods to read or update the file * attributes. The {@code type} parameter is the type of the attribute view * required and the method returns an instance of that type if supported. * The {@link BasicFileAttributeView} type supports access to the basic * attributes of a file. Invoking this method to select a file attribute * view of that type will always return an instance of that class. * *
The {@code options} array may be used to indicate how symbolic links
* are handled by the resulting file attribute view for the case that the
* file is a symbolic link. By default, symbolic links are followed. If the
* option {@link LinkOption#NOFOLLOW_LINKS NOFOLLOW_LINKS} is present then
* symbolic links are not followed. This option is ignored by implementations
* that do not support symbolic links.
*
* @param type
* The {@code Class} object corresponding to the file attribute view
* @param options
* Options indicating how symbolic links are handled
*
* @return A file attribute view of the specified type, or {@code null} if
* the attribute view type is not available
*
* @throws UnsupportedOperationException
* If options contains an unsupported option. This exception is
* specified to allow the {@code LinkOption} enum be extended
* in future releases.
*
* @see Attributes#readBasicFileAttributes
*/
A file attribute view provides a read-only or updatable view of a
* set of file attributes. This method is intended to be used where
* dynamic access to the file attributes is required. The {@code
* name} parameter specifies the {@link FileAttributeView#name name} of the
* file attribute view and this method returns an instance of that view if
* supported. The {@link BasicFileAttributeView} type supports access to the
* basic attributes of a file and is name {@code "basic"}. Invoking this
* method to select a file attribute view named {@code "basic"} will always
* return an instance of that class.
*
* The {@code options} array may be used to indicate how symbolic links
* are handled by the resulting file attribute view for the case that the
* file is a symbolic link. By default, symbolic links are followed. If the
* option {@link LinkOption#NOFOLLOW_LINKS NOFOLLOW_LINKS} is present then
* symbolic links are not followed. This option is ignored by implementations
* that do not support symbolic links.
*
* @param name
* The name of the file attribute view
* @param options
* Options indicating how symbolic links are handled
*
* @return A file attribute view of the given name, or {@code null} if
* the attribute view is not available
*
* @throws UnsupportedOperationException
* If options contains an unsupported option. This exception is
* specified to allow the {@code LinkOption} enum be extended
* in future releases.
*/
FileAttributeView getFileAttributeView(String name, LinkOption... options);
/**
* Tests if the file referenced by this object is the same file referenced
* by another object.
*
* If this {@code FileRef} and the given {@code FileRef} are {@link
* #equals(Object) equal} then this method returns {@code true} without checking
* if the file exists. If the {@code FileRef} and the given {@code FileRef}
* are associated with different providers, or the given {@code FileRef} is
* {@code null} then this method returns {@code false}. Otherwise, this method
* checks if both {@code FileRefs} locate the same file, and depending on the
* implementation, may require to open or access both files.
*
* If the file system and files remain static, then this method implements
* an equivalence relation for non-null {@code FileRefs}.
* An implementation may require to examine the file to determine if the
* file is a directory. Consequently this method may not be atomic with respect
* to other file system operations. If the file is a symbolic-link then the
* link is deleted and not the final target of the link.
*
* If the file is a directory then the directory must be empty. In some
* implementations a directory has entries for special files or links that
* are created when the directory is created. In such implementations a
* directory is considered empty when only the special entries exist.
*
* On some operating systems it may not be possible to remove a file when
* it is open and in use by this Java virtual machine or other programs.
*
* @throws NoSuchFileException
* If the file does not exist (optional specific exception)
* @throws DirectoryNotEmptyException
* If the file is a directory and could not otherwise be deleted
* because the directory is not empty (optional specific
* exception)
* @throws IOException
* If an I/O error occurs
* @throws SecurityException
* In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkDelete(String)} method
* is invoked to check delete access to the file
*/
void delete() throws IOException;
/**
* Tests this object for equality with another object.
*
* If the given object is not a {@code FileRef} then this method
* immediately returns {@code false}.
*
* For two file references to be considered equal requires that they
* are both the same type of {@code FileRef} and encapsulate components
* to locate the same file. This method does not access the file system and
* the file may not exist.
*
* This method satisfies the general contract of the {@link
* java.lang.Object#equals(Object) Object.equals} method. This method satisfies the general contract of the
* {@link java.lang.Object#hashCode() Object.hashCode} method.
*/
int hashCode();
}
*
*
* @param other
* The other file reference
*
* @return {@code true} if, and only if, this object and the given object
* locate the same file
*
* @throws IOException
* If an I/O error occurs
* @throws SecurityException
* In the case of the default provider, and a security manager is
* installed, the {@link SecurityManager#checkRead(String) checkRead}
* method is invoked to check read access to both files.
*
* @see java.nio.file.attribute.BasicFileAttributes#fileKey
*/
boolean isSameFile(FileRef other) throws IOException;
/**
* Deletes the file referenced by this object.
*
*