2362N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Oracle designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Oracle in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 2362N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 0N/A * or visit www.oracle.com if you need additional information or have any 0N/A * An asynchronous channel for reading, writing, and manipulating a file. 0N/A * <p> An asynchronous file channel is created when a file is opened by invoking 0N/A * one of the {@link #open open} methods defined by this class. The file contains 0N/A * a variable-length sequence of bytes that can be read and written and whose 0N/A * current size can be {@link #size() queried}. The size of the file increases 0N/A * when bytes are written beyond its current size; the size of the file decreases 0N/A * when it is {@link #truncate truncated}. 0N/A * <p> An asynchronous file channel does not have a <i>current position</i> 0N/A * within the file. Instead, the file position is specified to each read and 0N/A * write method that initiates asynchronous operations. A {@link CompletionHandler} 0N/A * is specified as a parameter and is invoked to consume the result of the I/O 0N/A * operation. This class also defines read and write methods that initiate 0N/A * asynchronous operations, returning a {@link Future} to represent the pending 0N/A * result of the operation. The {@code Future} may be used to check if the 0N/A * operation has completed, wait for its completion, and retrieve the result. 0N/A * <p> In addition to read and write operations, this class defines the 0N/A * following operations: </p> 0N/A * <li><p> Updates made to a file may be {@link #force <i>forced 0N/A * out</i>} to the underlying storage device, ensuring that data are not 0N/A * lost in the event of a system crash. </p></li> 0N/A * <li><p> A region of a file may be {@link #lock <i>locked</i>} against 0N/A * access by other programs. </p></li> 0N/A * <p> An {@code AsynchronousFileChannel} is associated with a thread pool to 0N/A * which tasks are submitted to handle I/O events and dispatch to completion 0N/A * handlers that consume the results of I/O operations on the channel. The 0N/A * completion handler for an I/O operation initiated on a channel is guaranteed 0N/A * to be invoked by one of the threads in the thread pool (This ensures that the 0N/A * completion handler is run by a thread with the expected <em>identity</em>). 0N/A * Where an I/O operation completes immediately, and the initiating thread is 0N/A * itself a thread in the thread pool, then the completion handler may be invoked 0N/A * directly by the initiating thread. When an {@code AsynchronousFileChannel} is 0N/A * created without specifying a thread pool then the channel is associated with 0N/A * a system-dependent default thread pool that may be shared with other 0N/A * channels. The default thread pool is configured by the system properties 0N/A * defined by the {@link AsynchronousChannelGroup} class. 0N/A * <p> Channels of this type are safe for use by multiple concurrent threads. The 0N/A * {@link Channel#close close} method may be invoked at any time, as specified 0N/A * by the {@link Channel} interface. This causes all outstanding asynchronous 0N/A * operations on the channel to complete with the exception {@link 0N/A * AsynchronousCloseException}. Multiple read and write operations may be 0N/A * outstanding at the same time. When multiple read and write operations are 0N/A * outstanding then the ordering of the I/O operations, and the order that the 0N/A * completion handlers are invoked, is not specified; they are not, in particular, 0N/A * guaranteed to execute in the order that the operations were initiated. The 0N/A * {@link java.nio.ByteBuffer ByteBuffers} used when reading or writing are not 0N/A * safe for use by multiple concurrent I/O operations. Furthermore, after an I/O 0N/A * operation is initiated then care should be taken to ensure that the buffer is 0N/A * not accessed until after the operation has completed. 0N/A * <p> As with {@link FileChannel}, the view of a file provided by an instance of 0N/A * this class is guaranteed to be consistent with other views of the same file 0N/A * provided by other instances in the same program. The view provided by an 0N/A * instance of this class may or may not, however, be consistent with the views 0N/A * seen by other concurrently-running programs due to caching performed by the 0N/A * underlying operating system and delays induced by network-filesystem protocols. 0N/A * This is true regardless of the language in which these other programs are 0N/A * written, and whether they are running on the same machine or on some other 0N/A * machine. The exact nature of any such inconsistencies are system-dependent 0N/A * and are therefore unspecified. 0N/A * Initializes a new instance of this class. 0N/A * Opens or creates a file for reading and/or writing, returning an 0N/A * asynchronous file channel to access the file. 0N/A * <p> The {@code options} parameter determines how the file is opened. 0N/A * The {@link StandardOpenOption#READ READ} and {@link StandardOpenOption#WRITE 0N/A * WRITE} options determines if the file should be opened for reading and/or 0N/A * writing. If neither option is contained in the array then an existing file 0N/A * is opened for reading. 0N/A * <p> In addition to {@code READ} and {@code WRITE}, the following options 0N/A * <table border=1 cellpadding=5 summary=""> 0N/A * <tr> <th>Option</th> <th>Description</th> </tr> 0N/A * <td> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </td> 0N/A * <td> When opening an existing file, the file is first truncated to a 0N/A * size of 0 bytes. This option is ignored when the file is opened only 0N/A * <td> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </td> 0N/A * <td> If this option is present then a new file is created, failing if 0N/A * the file already exists. When creating a file the check for the 0N/A * existence of the file and the creation of the file if it does not exist 0N/A * is atomic with respect to other file system operations. This option is 0N/A * ignored when the file is opened only for reading. </td> 0N/A * <td > {@link StandardOpenOption#CREATE CREATE} </td> 0N/A * <td> If this option is present then an existing file is opened if it 0N/A * exists, otherwise a new file is created. When creating a file the check 0N/A * for the existence of the file and the creation of the file if it does 0N/A * not exist is atomic with respect to other file system operations. This 0N/A * option is ignored if the {@code CREATE_NEW} option is also present or 0N/A * the file is opened only for reading. </td> 0N/A * <td > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </td> 0N/A * <td> When this option is present then the implementation makes a 0N/A * <em>best effort</em> attempt to delete the file when closed by the 0N/A * the {@link #close close} method. If the {@code close} method is not 0N/A * invoked then a <em>best effort</em> attempt is made to delete the file 0N/A * when the Java virtual machine terminates. </td> 0N/A * <td>{@link StandardOpenOption#SPARSE SPARSE} </td> 0N/A * <td> When creating a new file this option is a <em>hint</em> that the 0N/A * new file will be sparse. This option is ignored when not creating 0N/A * <td> {@link StandardOpenOption#SYNC SYNC} </td> 0N/A * <td> Requires that every update to the file's content or metadata be 0N/A * written synchronously to the underlying storage device. (see <a 0N/A * integrity</a>). </td> 0N/A * <td> {@link StandardOpenOption#DSYNC DSYNC} </td> 0N/A * <td> Requires that every update to the file's content be written 0N/A * synchronously to the underlying storage device. (see <a 0N/A * integrity</a>). </td> 0N/A * <p> An implementation may also support additional options. 0N/A * <p> The {@code executor} parameter is the {@link ExecutorService} to 0N/A * which tasks are submitted to handle I/O events and dispatch completion 0N/A * results for operations initiated on resulting channel. 0N/A * The nature of these tasks is highly implementation specific and so care 0N/A * should be taken when configuring the {@code Executor}. Minimally it 0N/A * should support an unbounded work queue and should not run tasks on the 0N/A * caller thread of the {@link ExecutorService#execute execute} method. 0N/A * Shutting down the executor service while the channel is open results in 0N/A * unspecified behavior. 0N/A * <p> The {@code attrs} parameter is an optional array of file {@link 0N/A * FileAttribute file-attributes} to set atomically when creating the file. 0N/A * <p> The new channel is created by invoking the {@link 0N/A * FileSystemProvider#newFileChannel newFileChannel} method on the 0N/A * provider that created the {@code Path}. 0N/A * The path of the file to open or create 0N/A * Options specifying how the file is opened 0N/A * The thread pool or {@code null} to associate the channel with 0N/A * the default thread pool 0N/A * An optional list of file attributes to set atomically when 0N/A * @return A new asynchronous file channel 0N/A * @throws IllegalArgumentException 0N/A * If the set contains an invalid combination of options 0N/A * @throws UnsupportedOperationException 0N/A * If the {@code file} is associated with a provider that does not 0N/A * support creating asynchronous file channels, or an unsupported 0N/A * open option is specified, or the array contains an attribute that 0N/A * cannot be set atomically when creating the file 0N/A * @throws IOException 0N/A * If an I/O error occurs 0N/A * @throws SecurityException 0N/A * If a security manager is installed and it denies an 0N/A * unspecified permission required by the implementation. 0N/A * In the case of the default provider, the {@link 0N/A * SecurityManager#checkRead(String)} method is invoked to check 0N/A * read access if the file is opened for reading. The {@link 0N/A * SecurityManager#checkWrite(String)} method is invoked to check 0N/A * write access if the file is opened for writing 0N/A * Opens or creates a file for reading and/or writing, returning an 0N/A * asynchronous file channel to access the file. 0N/A * <p> An invocation of this method behaves in exactly the same way as the 0N/A * ch.{@link #open(Path,Set,ExecutorService,FileAttribute[]) 0N/A * open}(file, opts, null, new FileAttribute<?>[0]); 0N/A * where {@code opts} is a {@code Set} containing the options specified to 0N/A * <p> The resulting channel is associated with default thread pool to which 0N/A * tasks are submitted to handle I/O events and dispatch to completion 0N/A * handlers that consume the result of asynchronous operations performed on 0N/A * the resulting channel. 0N/A * The path of the file to open or create 0N/A * Options specifying how the file is opened 0N/A * @return A new asynchronous file channel 0N/A * @throws IllegalArgumentException 0N/A * If the set contains an invalid combination of options 0N/A * @throws UnsupportedOperationException 0N/A * If the {@code file} is associated with a provider that does not 0N/A * support creating file channels, or an unsupported open option is 0N/A * @throws IOException 0N/A * If an I/O error occurs 0N/A * @throws SecurityException 0N/A * If a security manager is installed and it denies an 0N/A * unspecified permission required by the implementation. 0N/A * In the case of the default provider, the {@link 0N/A * SecurityManager#checkRead(String)} method is invoked to check 0N/A * read access if the file is opened for reading. The {@link 0N/A * SecurityManager#checkWrite(String)} method is invoked to check 0N/A * write access if the file is opened for writing 0N/A * Returns the current size of this channel's file. 0N/A * @return The current size of this channel's file, measured in bytes 0N/A * @throws ClosedChannelException 0N/A * If this channel is closed 0N/A * @throws IOException 0N/A * If some other I/O error occurs 0N/A * Truncates this channel's file to the given size. 0N/A * <p> If the given size is less than the file's current size then the file 0N/A * is truncated, discarding any bytes beyond the new end of the file. If 0N/A * the given size is greater than or equal to the file's current size then 0N/A * the file is not modified. </p> 0N/A * The new size, a non-negative byte count 0N/A * @return This file channel 0N/A * @throws NonWritableChannelException 0N/A * If this channel was not opened for writing 0N/A * @throws ClosedChannelException 0N/A * If this channel is closed 0N/A * @throws IllegalArgumentException 0N/A * If the new size is negative 0N/A * @throws IOException 0N/A * If some other I/O error occurs 0N/A * Forces any updates to this channel's file to be written to the storage 0N/A * device that contains it. 0N/A * <p> If this channel's file resides on a local storage device then when 0N/A * this method returns it is guaranteed that all changes made to the file 0N/A * since this channel was created, or since this method was last invoked, 0N/A * will have been written to that device. This is useful for ensuring that 0N/A * critical information is not lost in the event of a system crash. 0N/A * <p> If the file does not reside on a local device then no such guarantee 0N/A * <p> The {@code metaData} parameter can be used to limit the number of * I/O operations that this method is required to perform. Passing * {@code false} for this parameter indicates that only updates to the * file's content need be written to storage; passing {@code true} * indicates that updates to both the file's content and metadata must be * written, which generally requires at least one more I/O operation. * Whether this parameter actually has any effect is dependent upon the * underlying operating system and is therefore unspecified. * <p> Invoking this method may cause an I/O operation to occur even if the * channel was only opened for reading. Some operating systems, for * example, maintain a last-access time as part of a file's metadata, and * this time is updated whenever the file is read. Whether or not this is * actually done is system-dependent and is therefore unspecified. * <p> This method is only guaranteed to force changes that were made to * this channel's file via the methods defined in this class. * If {@code true} then this method is required to force changes * to both the file's content and metadata to be written to * storage; otherwise, it need only force content changes to be * @throws ClosedChannelException * If this channel is closed * If some other I/O error occurs * Acquires a lock on the given region of this channel's file. * <p> This method initiates an operation to acquire a lock on the given * region of this channel's file. The {@code handler} parameter is a * completion handler that is invoked when the lock is acquired (or the * operation fails). The result passed to the completion handler is the * resulting {@code FileLock}. * <p> The region specified by the {@code position} and {@code size} * parameters need not be contained within, or even overlap, the actual * underlying file. Lock regions are fixed in size; if a locked region * initially contains the end of the file and the file grows beyond the * region then the new portion of the file will not be covered by the lock. * If a file is expected to grow in size and a lock on the entire file is * required then a region starting at zero, and no smaller than the * expected maximum size of the file, should be locked. The two-argument * {@link #lock(Object,CompletionHandler)} method simply locks a region * of size {@link Long#MAX_VALUE}. If a lock that overlaps the requested * region is already held by this Java virtual machine, or this method has * been invoked to lock an overlapping region and that operation has not * completed, then this method throws {@link OverlappingFileLockException}. * <p> Some operating systems do not support a mechanism to acquire a file * lock in an asynchronous manner. Consequently an implementation may * acquire the file lock in a background thread or from a task executed by * a thread in the associated thread pool. If there are many lock operations * outstanding then it may consume threads in the Java virtual machine for * <p> Some operating systems do not support shared locks, in which case a * request for a shared lock is automatically converted into a request for * an exclusive lock. Whether the newly-acquired lock is shared or * exclusive may be tested by invoking the resulting lock object's {@link * FileLock#isShared() isShared} method. * <p> File locks are held on behalf of the entire Java virtual machine. * They are not suitable for controlling access to a file by multiple * threads within the same virtual machine. * The position at which the locked region is to start; must be * The size of the locked region; must be non-negative, and the sum * {@code position} + {@code size} must be non-negative * {@code true} to request a shared lock, in which case this * channel must be open for reading (and possibly writing); * {@code false} to request an exclusive lock, in which case this * channel must be open for writing (and possibly reading) * The object to attach to the I/O operation; can be {@code null} * The handler for consuming the result * @throws OverlappingFileLockException * If a lock that overlaps the requested region is already held by * this Java virtual machine, or there is already a pending attempt * to lock an overlapping region * @throws IllegalArgumentException * If the preconditions on the parameters do not hold * @throws NonReadableChannelException * If {@code shared} is true but this channel was not opened for reading * @throws NonWritableChannelException * If {@code shared} is false but this channel was not opened for writing * Acquires an exclusive lock on this channel's file. * <p> This method initiates an operation to acquire a lock on the given * region of this channel's file. The {@code handler} parameter is a * completion handler that is invoked when the lock is acquired (or the * operation fails). The result passed to the completion handler is the * resulting {@code FileLock}. * <p> An invocation of this method of the form {@code ch.lock(att,handler)} * behaves in exactly the same way as the invocation * ch.{@link #lock(long,long,boolean,Object,CompletionHandler) lock}(0L, Long.MAX_VALUE, false, att, handler) * The object to attach to the I/O operation; can be {@code null} * The handler for consuming the result * @throws OverlappingFileLockException * If a lock is already held by this Java virtual machine, or there * is already a pending attempt to lock a region * @throws NonWritableChannelException * If this channel was not opened for writing * Acquires a lock on the given region of this channel's file. * <p> This method initiates an operation to acquire a lock on the given * region of this channel's file. The method behaves in exactly the same * manner as the {@link #lock(long, long, boolean, Object, CompletionHandler)} * method except that instead of specifying a completion handler, this * method returns a {@code Future} representing the pending result. The * {@code Future}'s {@link Future#get() get} method returns the {@link * FileLock} on successful completion. * The position at which the locked region is to start; must be * The size of the locked region; must be non-negative, and the sum * {@code position} + {@code size} must be non-negative * {@code true} to request a shared lock, in which case this * channel must be open for reading (and possibly writing); * {@code false} to request an exclusive lock, in which case this * channel must be open for writing (and possibly reading) * @return a {@code Future} object representing the pending result * @throws OverlappingFileLockException * If a lock is already held by this Java virtual machine, or there * is already a pending attempt to lock a region * @throws IllegalArgumentException * If the preconditions on the parameters do not hold * @throws NonReadableChannelException * If {@code shared} is true but this channel was not opened for reading * @throws NonWritableChannelException * If {@code shared} is false but this channel was not opened for writing * Acquires an exclusive lock on this channel's file. * <p> This method initiates an operation to acquire an exclusive lock on this * channel's file. The method returns a {@code Future} representing the * pending result of the operation. The {@code Future}'s {@link Future#get() * get} method returns the {@link FileLock} on successful completion. * <p> An invocation of this method behaves in exactly the same way as the * ch.{@link #lock(long,long,boolean) lock}(0L, Long.MAX_VALUE, false) * @return a {@code Future} object representing the pending result * @throws OverlappingFileLockException * If a lock is already held by this Java virtual machine, or there * is already a pending attempt to lock a region * @throws NonWritableChannelException * If this channel was not opened for writing * Attempts to acquire a lock on the given region of this channel's file. * <p> This method does not block. An invocation always returns immediately, * either having acquired a lock on the requested region or having failed to * do so. If it fails to acquire a lock because an overlapping lock is held * by another program then it returns {@code null}. If it fails to acquire * a lock for any other reason then an appropriate exception is thrown. * The position at which the locked region is to start; must be * The size of the locked region; must be non-negative, and the sum * {@code position} + {@code size} must be non-negative * {@code true} to request a shared lock, * {@code false} to request an exclusive lock * @return A lock object representing the newly-acquired lock, * or {@code null} if the lock could not be acquired * because another program holds an overlapping lock * @throws IllegalArgumentException * If the preconditions on the parameters do not hold * @throws ClosedChannelException * If this channel is closed * @throws OverlappingFileLockException * If a lock that overlaps the requested region is already held by * this Java virtual machine, or if another thread is already * blocked in this method and is attempting to lock an overlapping * region of the same file * @throws NonReadableChannelException * If {@code shared} is true but this channel was not opened for reading * @throws NonWritableChannelException * If {@code shared} is false but this channel was not opened for writing * If some other I/O error occurs * @see #lock(Object,CompletionHandler) * @see #lock(long,long,boolean,Object,CompletionHandler) * Attempts to acquire an exclusive lock on this channel's file. * <p> An invocation of this method of the form {@code ch.tryLock()} * behaves in exactly the same way as the invocation * ch.{@link #tryLock(long,long,boolean) tryLock}(0L, Long.MAX_VALUE, false) </pre> * @return A lock object representing the newly-acquired lock, * or {@code null} if the lock could not be acquired * because another program holds an overlapping lock * @throws ClosedChannelException * If this channel is closed * @throws OverlappingFileLockException * If a lock that overlaps the requested region is already held by * this Java virtual machine, or if another thread is already * blocked in this method and is attempting to lock an overlapping * @throws NonWritableChannelException * If {@code shared} is false but this channel was not opened for writing * If some other I/O error occurs * @see #lock(Object,CompletionHandler) * @see #lock(long,long,boolean,Object,CompletionHandler) * @see #tryLock(long,long,boolean) * Reads a sequence of bytes from this channel into the given buffer, * starting at the given file position. * <p> This method initiates the reading of a sequence of bytes from this * channel into the given buffer, starting at the given file position. The * result of the read is the number of bytes read or {@code -1} if the given * position is greater than or equal to the file's size at the time that the * <p> This method works in the same manner as the {@link * AsynchronousByteChannel#read(ByteBuffer,Object,CompletionHandler)} * method, except that bytes are read starting at the given file position. * If the given file position is greater than the file's size at the time * that the read is attempted then no bytes are read. * The buffer into which bytes are to be transferred * The file position at which the transfer is to begin; * The object to attach to the I/O operation; can be {@code null} * The handler for consuming the result * @throws IllegalArgumentException * If the position is negative or the buffer is read-only * @throws NonReadableChannelException * If this channel was not opened for reading * Reads a sequence of bytes from this channel into the given buffer, * starting at the given file position. * <p> This method initiates the reading of a sequence of bytes from this * channel into the given buffer, starting at the given file position. This * method returns a {@code Future} representing the pending result of the * operation. The {@code Future}'s {@link Future#get() get} method returns * the number of bytes read or {@code -1} if the given position is greater * than or equal to the file's size at the time that the read is attempted. * <p> This method works in the same manner as the {@link * AsynchronousByteChannel#read(ByteBuffer)} method, except that bytes are * read starting at the given file position. If the given file position is * greater than the file's size at the time that the read is attempted then * The buffer into which bytes are to be transferred * The file position at which the transfer is to begin; * @return A {@code Future} object representing the pending result * @throws IllegalArgumentException * If the position is negative or the buffer is read-only * @throws NonReadableChannelException * If this channel was not opened for reading * Writes a sequence of bytes to this channel from the given buffer, starting * at the given file position. * <p> This method works in the same manner as the {@link * AsynchronousByteChannel#write(ByteBuffer,Object,CompletionHandler)} * method, except that bytes are written starting at the given file position. * If the given position is greater than the file's size, at the time that * the write is attempted, then the file will be grown to accommodate the new * bytes; the values of any bytes between the previous end-of-file and the * newly-written bytes are unspecified. * The buffer from which bytes are to be transferred * The file position at which the transfer is to begin; * The object to attach to the I/O operation; can be {@code null} * The handler for consuming the result * @throws IllegalArgumentException * If the position is negative * @throws NonWritableChannelException * If this channel was not opened for writing * Writes a sequence of bytes to this channel from the given buffer, starting * at the given file position. * <p> This method initiates the writing of a sequence of bytes to this * channel from the given buffer, starting at the given file position. The * method returns a {@code Future} representing the pending result of the * write operation. The {@code Future}'s {@link Future#get() get} method * returns the number of bytes written. * <p> This method works in the same manner as the {@link * AsynchronousByteChannel#write(ByteBuffer)} method, except that bytes are * written starting at the given file position. If the given position is * greater than the file's size, at the time that the write is attempted, * then the file will be grown to accommodate the new bytes; the values of * any bytes between the previous end-of-file and the newly-written bytes * The buffer from which bytes are to be transferred * The file position at which the transfer is to begin; * @return A {@code Future} object representing the pending result * @throws IllegalArgumentException * If the position is negative * @throws NonWritableChannelException * If this channel was not opened for writing