FileRef.java 14.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * 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.*;
29 30 31
import java.util.Map;
import java.io.InputStream;
import java.io.OutputStream;
32 33 34 35 36 37
import java.io.IOException;

/**
 * A reference to a file.
 *
 * <p> A {@code FileRef} is an object that locates a file and defines methods to
38 39
 * open the file for reading or writing. It also provides access to associated
 * metadata or file attributes.
40 41
 *
 * @since 1.7
42 43
 * @see java.nio.file.attribute.Attributes
 * @see java.io.File#toPath
44 45 46 47 48
 */

public interface FileRef {

    /**
49 50 51 52 53
     * Opens the file referenced by this object, returning an input stream to
     * read from the file. The stream will not be buffered, and is not required
     * to support the {@link InputStream#mark mark} or {@link InputStream#reset
     * reset} methods. The stream will be safe for access by multiple concurrent
     * threads. Reading commences at the beginning of the file.
54 55
     *
     * <p> The {@code options} parameter determines how the file is opened.
56 57 58 59
     * If no options are present then it is equivalent to opening the file with
     * the {@link StandardOpenOption#READ READ} option. In addition to the {@code
     * READ} option, an implementation may also support additional implementation
     * specific options.
60
     *
61
     * @return  an input stream to read bytes from the file
62 63
     *
     * @throws  IllegalArgumentException
64
     *          if an invalid combination of options is specified
65
     * @throws  UnsupportedOperationException
66
     *          if an unsupported option is specified
67
     * @throws  IOException
68
     *          if an I/O error occurs
69 70 71
     * @throws  SecurityException
     *          In the case of the default provider, and a security manager is
     *          installed, the {@link SecurityManager#checkRead(String) checkRead}
72
     *          method is invoked to check read access to the file.
73
     */
74
    InputStream newInputStream(OpenOption... options) throws IOException;
75 76

    /**
77 78
     * Opens or creates the file located by this object for writing, returning
     * an output stream to write bytes to the file.
79
     *
80 81 82 83 84
     * <p> The {@code options} parameter determines how the file is opened.
     * If no options are present then this method creates a new file for writing
     * or truncates an existing file. In addition to the {@link StandardOpenOption
     * standard} options, an implementation may also support additional
     * implementation specific options.
85
     *
86 87
     * <p> The resulting stream will not be buffered. The stream will be safe
     * for access by multiple concurrent threads.
88
     *
89 90
     * @param   options
     *          options specifying how the file is opened
91
     *
92
     * @return  a new output stream
93
     *
94 95
     * @throws  IllegalArgumentException
     *          if {@code options} contains an invalid combination of options
96
     * @throws  UnsupportedOperationException
97
     *          if an unsupported option is specified
98
     * @throws  IOException
99
     *          if an I/O error occurs
100 101
     * @throws  SecurityException
     *          In the case of the default provider, and a security manager is
102 103
     *          installed, the {@link SecurityManager#checkWrite(String) checkWrite}
     *          method is invoked to check write access to the file.
104
     */
105
    OutputStream newOutputStream(OpenOption... options) throws IOException;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

    /**
     * Returns a file attribute view of a given type.
     *
     * <p> 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.
     *
     * <p> 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
127
     *          the {@code Class} object corresponding to the file attribute view
128
     * @param   options
129
     *          options indicating how symbolic links are handled
130
     *
131
     * @return  a file attribute view of the specified type, or {@code null} if
132 133 134 135 136 137 138 139 140
     *          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
     */
141 142
    <V extends FileAttributeView> V getFileAttributeView(Class<V> type,
                                                         LinkOption... options);
143 144

    /**
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
     * Sets the value of a file attribute.
     *
     * <p> The {@code attribute} parameter identifies the attribute to be set
     * and takes the form:
     * <blockquote>
     * [<i>view-name</i><b>:</b>]<i>attribute-name</i>
     * </blockquote>
     * where square brackets [...] delineate an optional component and the
     * character {@code ':'} stands for itself.
     *
     * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link
     * FileAttributeView} that identifies a set of file attributes. If not
     * specified then it defaults to {@code "basic"}, the name of the file
     * attribute view that identifies the basic set of file attributes common to
     * many file systems. <i>attribute-name</i> is the name of the attribute
     * within the set.
     *
     * <p> <b>Usage Example:</b>
     * Suppose we want to set the DOS "hidden" attribute:
     * <pre>
     *    file.setAttribute("dos:hidden", true);
     * </pre>
     *
     * @param   attribute
     *          the attribute to set
     * @param   value
     *          the attribute value
172
     * @param   options
173
     *          options indicating how symbolic links are handled
174 175
     *
     * @throws  UnsupportedOperationException
176 177 178 179 180 181 182 183 184
     *          if the attribute view is not available or it does not support
     *          updating the attribute
     * @throws  IllegalArgumentException
     *          if the attribute value is of the correct type but has an
     *          inappropriate value
     * @throws  ClassCastException
     *          If the attribute value is not of the expected type or is a
     *          collection containing elements that are not of the expected
     *          type
185 186 187 188
     * @throws  IOException
     *          If an I/O error occurs
     * @throws  SecurityException
     *          In the case of the default provider, and a security manager is
189 190 191 192
     *          installed, its {@link SecurityManager#checkWrite(String) checkWrite}
     *          method denies write access to the file. If this method is invoked
     *          to set security sensitive attributes then the security manager
     *          may be invoked to check for additional permissions.
193
     */
194 195
    void setAttribute(String attribute, Object value, LinkOption... options)
        throws IOException;
196 197

    /**
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
     * Reads the value of a file attribute.
     *
     * <p> The {@code attribute} parameter identifies the attribute to be read
     * and takes the form:
     * <blockquote>
     * [<i>view-name</i><b>:</b>]<i>attribute-name</i>
     * </blockquote>
     * where square brackets [...] delineate an optional component and the
     * character {@code ':'} stands for itself.
     *
     * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link
     * FileAttributeView} that identifies a set of file attributes. If not
     * specified then it defaults to {@code "basic"}, the name of the file
     * attribute view that identifies the basic set of file attributes common to
     * many file systems. <i>attribute-name</i> is the name of the attribute.
     *
     * <p> The {@code options} array may be used to indicate how symbolic links
     * are handled for the case that the file is a symbolic link. By default,
     * symbolic links are followed and the file attribute of the final target
     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
     * NOFOLLOW_LINKS} is present then symbolic links are not followed and so
     * the method returns the file attribute of the symbolic link.
     *
     * <p> <b>Usage Example:</b>
     * Suppose we require the user ID of the file owner on a system that
     * supports a "{@code unix}" view:
     * <pre>
     *    int uid = (Integer)file.getAttribute("unix:uid");
     * </pre>
     *
     * @param   attribute
     *          the attribute to read
     * @param   options
     *          options indicating how symbolic links are handled
     * @return  the attribute value or {@code null} if the attribute view
     *          is not available or it does not support reading the attribute
     *
     *          reading the attribute
236
     * @throws  IOException
237
     *          if an I/O error occurs
238 239
     * @throws  SecurityException
     *          In the case of the default provider, and a security manager is
240 241 242 243
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
     *          method denies read access to the file. If this method is invoked
     *          to read security sensitive attributes then the security manager
     *          may be invoked to check for additional permissions.
244
     */
245
    Object getAttribute(String attribute, LinkOption... options) throws IOException;
246 247

    /**
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
     * Reads a set of file attributes as a bulk operation.
     *
     * <p> The {@code attributes} parameter identifies the attributes to be read
     * and takes the form:
     * <blockquote>
     * [<i>view-name</i><b>:</b>]<i>attribute-list</i>
     * </blockquote>
     * where square brackets [...] delineate an optional component and the
     * character {@code ':'} stands for itself.
     *
     * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link
     * FileAttributeView} that identifies a set of file attributes. If not
     * specified then it defaults to {@code "basic"}, the name of the file
     * attribute view that identifies the basic set of file attributes common to
     * many file systems.
     *
     * <p> The <i>attribute-list</i> component is a comma separated list of
     * zero or more names of attributes to read. If the list contains the value
     * {@code "*"} then all attributes are read. Attributes that are not supported
     * are ignored and will not be present in the returned map. It is
     * implementation specific if all attributes are read as an atomic operation
     * with respect to other file system operations.
     *
     * <p> The following examples demonstrate possible values for the {@code
     * attributes} parameter:
     *
     * <blockquote>
     * <table border="0">
     * <tr>
     *   <td> {@code "*"} </td>
     *   <td> Read all {@link BasicFileAttributes basic-file-attributes}. </td>
     * </tr>
     * <tr>
     *   <td> {@code "size,lastModifiedTime,lastAccessTime"} </td>
     *   <td> Reads the file size, last modified time, and last access time
     *     attributes. </td>
     * </tr>
     * <tr>
     *   <td> {@code "posix:*"} </td>
     *   <td> Read all {@link PosixFileAttributes POSIX-file-attributes}.. </td>
     * </tr>
     * <tr>
     *   <td> {@code "posix:permissions,owner,size"} </td>
     *   <td> Reads the POSX file permissions, owner, and file size. </td>
     * </tr>
     * </table>
     * </blockquote>
295
     *
296 297 298 299 300 301 302 303 304 305 306
     * <p> The {@code options} array may be used to indicate how symbolic links
     * are handled for the case that the file is a symbolic link. By default,
     * symbolic links are followed and the file attribute of the final target
     * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS
     * NOFOLLOW_LINKS} is present then symbolic links are not followed and so
     * the method returns the file attribute of the symbolic link.
     *
     * @param   attributes
     *          The attributes to read
     * @param   options
     *          Options indicating how symbolic links are handled
307
     *
308 309
     * @return  A map of the attributes returned; may be empty. The map's keys
     *          are the attribute names, its values are the attribute values
310
     *
311 312 313 314 315 316 317 318
     * @throws  IOException
     *          If an I/O error occurs
     * @throws  SecurityException
     *          In the case of the default provider, and a security manager is
     *          installed, its {@link SecurityManager#checkRead(String) checkRead}
     *          method denies read access to the file. If this method is invoked
     *          to read security sensitive attributes then the security manager
     *          may be invoke to check for additional permissions.
319
     */
320 321
    Map<String,?> readAttributes(String attributes, LinkOption... options)
        throws IOException;
322
}