231 * integrity</a>). </td> 232 * <tr> 233 * <tr> 234 * <td> {@link StandardOpenOption#DSYNC DSYNC} </td> 235 * <td> Requires that every update to the file's content be written 236 * synchronously to the underlying storage device. (see <a 237 * href="../file/package-summary.html#integrity"> Synchronized I/O file 238 * integrity</a>). </td> 239 * </tr> 240 * </table> 241 * 242 * <p> An implementation may also support additional options. 243 * 244 * <p> The {@code attrs} parameter is an optional array of file {@link 245 * FileAttribute file-attributes} to set atomically when creating the file. 246 * 247 * <p> The new channel is created by invoking the {@link 248 * FileSystemProvider#newFileChannel newFileChannel} method on the 249 * provider that created the {@code Path}. 250 * 251 * @param file 252 * The path of the file to open or create 253 * @param options 254 * Options specifying how the file is opened 255 * @param attrs 256 * An optional list of file attributes to set atomically when 257 * creating the file 258 * 259 * @return A new file channel 260 * 261 * @throws IllegalArgumentException 262 * If the set contains an invalid combination of options 263 * @throws UnsupportedOperationException 264 * If the {@code file} is associated with a provider that does not 265 * support creating file channels, or an unsupported open option is 266 * specified, or the array contains an attribute that cannot be set 267 * atomically when creating the file 268 * @throws IOException 269 * If an I/O error occurs 270 * @throws SecurityException 271 * If a security manager is installed and it denies an 272 * unspecified permission required by the implementation. 273 * In the case of the default provider, the {@link 274 * SecurityManager#checkRead(String)} method is invoked to check 275 * read access if the file is opened for reading. The {@link 276 * SecurityManager#checkWrite(String)} method is invoked to check 277 * write access if the file is opened for writing 278 * 279 * @since 1.7 280 */ 281 public static FileChannel open(Path file, 282 Set<? extends OpenOption> options, 283 FileAttribute<?>... attrs) 284 throws IOException 285 { 286 FileSystemProvider provider = file.getFileSystem().provider(); 287 return provider.newFileChannel(file, options, attrs); 288 } 289 290 private static final FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0]; 291 292 /** 293 * Opens or creates a file, returning a file channel to access the file. 294 * 295 * <p> An invocation of this method behaves in exactly the same way as the 296 * invocation 297 * <pre> 298 * fc.{@link #open(Path,Set,FileAttribute[]) open}(file, options, new FileAttribute<?>[0]); 299 * </pre> 300 * 301 * @param file 302 * The path of the file to open or create 303 * @param options 304 * Options specifying how the file is opened 305 * 306 * @return A new file channel 307 * 308 * @throws IllegalArgumentException 309 * If the set contains an invalid combination of options 310 * @throws UnsupportedOperationException 311 * If the {@code file} is associated with a provider that does not 312 * support creating file channels, or an unsupported open option is 313 * specified 314 * @throws IOException 315 * If an I/O error occurs 316 * @throws SecurityException 317 * If a security manager is installed and it denies an 318 * unspecified permission required by the implementation. 319 * In the case of the default provider, the {@link 320 * SecurityManager#checkRead(String)} method is invoked to check 321 * read access if the file is opened for reading. The {@link 322 * SecurityManager#checkWrite(String)} method is invoked to check 323 * write access if the file is opened for writing 324 * 325 * @since 1.7 326 */ 327 public static FileChannel open(Path file, OpenOption... options) 328 throws IOException 329 { 330 Set<OpenOption> set = new HashSet<OpenOption>(options.length); 331 Collections.addAll(set, options); 332 return open(file, set, NO_ATTRIBUTES); 333 } 334 335 // -- Channel operations -- 336 337 /** 338 * Reads a sequence of bytes from this channel into the given buffer. 339 * 340 * <p> Bytes are read starting at this channel's current file position, and 341 * then the file position is updated with the number of bytes actually 342 * read. Otherwise this method behaves exactly as specified in the {@link 343 * ReadableByteChannel} interface. </p> 344 */ 345 public abstract int read(ByteBuffer dst) throws IOException; 346 347 /** 348 * Reads a sequence of bytes from this channel into a subsequence of the 349 * given buffers. 350 * 351 * <p> Bytes are read starting at this channel's current file position, and 352 * then the file position is updated with the number of bytes actually | 231 * integrity</a>). </td> 232 * <tr> 233 * <tr> 234 * <td> {@link StandardOpenOption#DSYNC DSYNC} </td> 235 * <td> Requires that every update to the file's content be written 236 * synchronously to the underlying storage device. (see <a 237 * href="../file/package-summary.html#integrity"> Synchronized I/O file 238 * integrity</a>). </td> 239 * </tr> 240 * </table> 241 * 242 * <p> An implementation may also support additional options. 243 * 244 * <p> The {@code attrs} parameter is an optional array of file {@link 245 * FileAttribute file-attributes} to set atomically when creating the file. 246 * 247 * <p> The new channel is created by invoking the {@link 248 * FileSystemProvider#newFileChannel newFileChannel} method on the 249 * provider that created the {@code Path}. 250 * 251 * @param path 252 * The path of the file to open or create 253 * @param options 254 * Options specifying how the file is opened 255 * @param attrs 256 * An optional list of file attributes to set atomically when 257 * creating the file 258 * 259 * @return A new file channel 260 * 261 * @throws IllegalArgumentException 262 * If the set contains an invalid combination of options 263 * @throws UnsupportedOperationException 264 * If the {@code path} is associated with a provider that does not 265 * support creating file channels, or an unsupported open option is 266 * specified, or the array contains an attribute that cannot be set 267 * atomically when creating the file 268 * @throws IOException 269 * If an I/O error occurs 270 * @throws SecurityException 271 * If a security manager is installed and it denies an 272 * unspecified permission required by the implementation. 273 * In the case of the default provider, the {@link 274 * SecurityManager#checkRead(String)} method is invoked to check 275 * read access if the file is opened for reading. The {@link 276 * SecurityManager#checkWrite(String)} method is invoked to check 277 * write access if the file is opened for writing 278 * 279 * @since 1.7 280 */ 281 public static FileChannel open(Path path, 282 Set<? extends OpenOption> options, 283 FileAttribute<?>... attrs) 284 throws IOException 285 { 286 FileSystemProvider provider = path.getFileSystem().provider(); 287 return provider.newFileChannel(path, options, attrs); 288 } 289 290 private static final FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0]; 291 292 /** 293 * Opens or creates a file, returning a file channel to access the file. 294 * 295 * <p> An invocation of this method behaves in exactly the same way as the 296 * invocation 297 * <pre> 298 * fc.{@link #open(Path,Set,FileAttribute[]) open}(file, opts, new FileAttribute<?>[0]); 299 * </pre> 300 * where {@code opts} is a set of the options specified in the {@code 301 * options} array. 302 * 303 * @param path 304 * The path of the file to open or create 305 * @param options 306 * Options specifying how the file is opened 307 * 308 * @return A new file channel 309 * 310 * @throws IllegalArgumentException 311 * If the set contains an invalid combination of options 312 * @throws UnsupportedOperationException 313 * If the {@code path} is associated with a provider that does not 314 * support creating file channels, or an unsupported open option is 315 * specified 316 * @throws IOException 317 * If an I/O error occurs 318 * @throws SecurityException 319 * If a security manager is installed and it denies an 320 * unspecified permission required by the implementation. 321 * In the case of the default provider, the {@link 322 * SecurityManager#checkRead(String)} method is invoked to check 323 * read access if the file is opened for reading. The {@link 324 * SecurityManager#checkWrite(String)} method is invoked to check 325 * write access if the file is opened for writing 326 * 327 * @since 1.7 328 */ 329 public static FileChannel open(Path path, OpenOption... options) 330 throws IOException 331 { 332 Set<OpenOption> set = new HashSet<OpenOption>(options.length); 333 Collections.addAll(set, options); 334 return open(path, set, NO_ATTRIBUTES); 335 } 336 337 // -- Channel operations -- 338 339 /** 340 * Reads a sequence of bytes from this channel into the given buffer. 341 * 342 * <p> Bytes are read starting at this channel's current file position, and 343 * then the file position is updated with the number of bytes actually 344 * read. Otherwise this method behaves exactly as specified in the {@link 345 * ReadableByteChannel} interface. </p> 346 */ 347 public abstract int read(ByteBuffer dst) throws IOException; 348 349 /** 350 * Reads a sequence of bytes from this channel into a subsequence of the 351 * given buffers. 352 * 353 * <p> Bytes are read starting at this channel's current file position, and 354 * then the file position is updated with the number of bytes actually |