An asynchronous file channel is created when a file is opened by invoking
one of the open methods defined by this class. The file contains
a variable-length sequence of bytes that can be read and written and whose
current size can be queried. The size of the file increases
when bytes are written beyond its current size; the size of the file decreases
when it is truncated.
An asynchronous file channel does not have a current position
within the file. Instead, the file position is specified to each read and
write method that initiates asynchronous operations. A CompletionHandler
is specified as a parameter and is invoked to consume the result of the I/O
operation. This class also defines read and write methods that initiate
asynchronous operations, returning a Future to represent the pending
result of the operation. The Future may be used to check if the
operation has completed, wait for its completion, and retrieve the result.
In addition to read and write operations, this class defines the following operations:
Updates made to a file may be forced
out to the underlying storage device, ensuring that data are not
lost in the event of a system crash.
A region of a file may be locked against
access by other programs.
An AsynchronousFileChannel is associated with a thread pool to
which tasks are submitted to handle I/O events and dispatch to completion
handlers that consume the results of I/O operations on the channel. The
completion handler for an I/O operation initiated on a channel is guaranteed
to be invoked by one of the threads in the thread pool (This ensures that the
completion handler is run by a thread with the expected identity).
Where an I/O operation completes immediately, and the initiating thread is
itself a thread in the thread pool, then the completion handler may be invoked
directly by the initiating thread. When an AsynchronousFileChannel is
created without specifying a thread pool then the channel is associated with
a system-dependent default thread pool that may be shared with other
channels. The default thread pool is configured by the system properties
defined by the AsynchronousChannelGroup class.
Channels of this type are safe for use by multiple concurrent threads. The
close method may be invoked at any time, as specified
by the Channel interface. This causes all outstanding asynchronous
operations on the channel to complete with the exception AsynchronousCloseException. Multiple read and write operations may be
outstanding at the same time. When multiple read and write operations are
outstanding then the ordering of the I/O operations, and the order that the
completion handlers are invoked, is not specified; they are not, in particular,
guaranteed to execute in the order that the operations were initiated. The
ByteBuffers used when reading or writing are not
safe for use by multiple concurrent I/O operations. Furthermore, after an I/O
operation is initiated then care should be taken to ensure that the buffer is
not accessed until after the operation has completed.
As with FileChannel, the view of a file provided by an instance of
this class is guaranteed to be consistent with other views of the same file
provided by other instances in the same program. The view provided by an
instance of this class may or may not, however, be consistent with the views
seen by other concurrently-running programs due to caching performed by the
underlying operating system and delays induced by network-filesystem protocols.
This is true regardless of the language in which these other programs are
written, and whether they are running on the same machine or on some other
machine. The exact nature of any such inconsistencies are system-dependent
and are therefore unspecified.
| Modifier and Type | Field and Description |
|---|---|
| private static final FileAttribute |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public abstract void | force(boolean
If metaData)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
writtenForces any updates to this channel's file to be written to the storage device that contains it. |
| public abstract < The type of the attachment A> void | lock(long
The position at which the locked region is to start; must be
non-negative position, long The size of the locked region; must be non-negative, and the sum
size, boolean position + size must be non-negative.
A value of zero means to lock all bytes from the specified
starting position to the end of the file, regardless of whether
the file is subsequently extended or truncatedtrue to request a shared lock, in which case this
channel must be open for reading (and possibly writing);
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 attachment, CompletionHandler<FileLock, ? super A> nullThe handler for consuming the result handler)Acquires a lock on the given region of this channel's file. |
| public final < The type of the attachment A> void | lock(A
The object to attach to the I/O operation; can be attachment, CompletionHandler<FileLock, ? super A> nullThe handler for consuming the result handler)Acquires an exclusive lock on this channel's file. |
| public abstract Future | Returns: aFuture object representing the pending resultThe position at which the locked region is to start; must be
non-negative position, long The size of the locked region; must be non-negative, and the sum
size, boolean position + size must be non-negative.
A value of zero means to lock all bytes from the specified
starting position to the end of the file, regardless of whether
the file is subsequently extended or truncatedtrue to request a shared lock, in which case this
channel must be open for reading (and possibly writing);
false to request an exclusive lock, in which case this
channel must be open for writing (and possibly reading)Acquires a lock on the given region of this channel's file. |
| public final Future | Returns: aFuture object representing the pending resultAcquires an exclusive lock on this channel's file. |
| public static AsynchronousFileChannel | Returns: A new asynchronous file channelThe path of the file to open or create file, Set<? extends OpenOption> Options specifying how the file is opened options, ExecutorService The thread pool or executor, FileAttribute<?>... null to associate the channel with
the default thread poolAn optional list of file attributes to set atomically when
creating the file attrs)Opens or creates a file for reading and/or writing, returning an asynchronous file channel to access the file. |
| public static AsynchronousFileChannel | Returns: A new asynchronous file channelThe path of the file to open or create file, OpenOption... Options specifying how the file is opened options)Opens or creates a file for reading and/or writing, returning an asynchronous file channel to access the file. |
| public abstract < The type of the attachment A> void | read(ByteBuffer
The buffer into which bytes are to be transferred dst, long The file position at which the transfer is to begin;
must be non-negative position, A The object to attach to the I/O operation; can be attachment, CompletionHandler<Integer, ? super A> nullThe handler for consuming the result handler)Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. |
| public abstract Future | Returns: AFuture object representing the pending resultThe buffer into which bytes are to be transferred dst, long The file position at which the transfer is to begin;
must be non-negative position)Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. |
| public abstract long | Returns: The current size of this channel's file, measured in bytesReturns the current size of this channel's file. |
| public abstract AsynchronousFileChannel | Returns: This file channelThe new size, a non-negative byte count size)Truncates this channel's file to the given size. |
| public abstract FileLock | Returns: A lock object representing the newly-acquired lock, ornull if the lock could not be acquired
because another program holds an overlapping lockThe position at which the locked region is to start; must be
non-negative position, long The size of the locked region; must be non-negative, and the sum
size, boolean position + size must be non-negative.
A value of zero means to lock all bytes from the specified
starting position to the end of the file, regardless of whether
the file is subsequently extended or truncatedtrue to request a shared lock,
false to request an exclusive lockAttempts to acquire a lock on the given region of this channel's file. |
| public final FileLock | Returns: A lock object representing the newly-acquired lock, ornull if the lock could not be acquired
because another program holds an overlapping lockAttempts to acquire an exclusive lock on this channel's file. |
| public abstract < The type of the attachment A> void | write(ByteBuffer
The buffer from which bytes are to be transferred src, long The file position at which the transfer is to begin;
must be non-negative position, A The object to attach to the I/O operation; can be attachment, CompletionHandler<Integer, ? super A> nullThe handler for consuming the result handler)Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. |
| public abstract Future | Returns: AFuture object representing the pending resultThe buffer from which bytes are to be transferred src, long The file position at which the transfer is to begin;
must be non-negative position)Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. |
| NO_ATTRIBUTES | back to summary |
|---|---|
private static final FileAttribute<?>[] NO_ATTRIBUTES
| |
| AsynchronousFileChannel | back to summary |
|---|---|
| protected AsynchronousFileChannel() Initializes a new instance of this class. | |
| force | back to summary |
|---|---|
| public abstract void force(boolean metaData) throws IOException Forces any updates to this channel's file to be written to the storage device that contains it. If this channel's file resides on a local storage device then when this method returns it is guaranteed that all changes made to the file since this channel was created, or since this method was last invoked, will have been written to that device. This is useful for ensuring that critical information is not lost in the event of a system crash. If the file does not reside on a local device then no such guarantee is made. The 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. This method is only guaranteed to force changes that were made to this channel's file via the methods defined in this class.
| |
| lock | back to summary |
|---|---|
| public abstract <A> void lock(long position, long size, boolean shared, A attachment, CompletionHandler<FileLock, ? super A> handler) Acquires a lock on the given region of this channel's file. This method initiates an operation to acquire a lock on the given
region of this channel's file. The The region specified by the 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 indefinite periods. 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 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.
| |
| lock | back to summary |
|---|---|
| public final <A> void lock(A attachment, CompletionHandler<FileLock, ? super A> handler) Acquires an exclusive lock on this channel's file. This method initiates an operation to acquire a lock on the given
region of this channel's file. The An invocation of this method of the form ch.lock(0L, Long.MAX_VALUE, false, att, handler)
| |
| lock | back to summary |
|---|---|
| public abstract Future Acquires a lock on the given region of this channel's file. 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
| |
| lock | back to summary |
|---|---|
| public final Future Acquires an exclusive lock on this channel's file. This method initiates an operation to acquire an exclusive lock on this
channel's file. The method returns a An invocation of this method behaves in exactly the same way as the invocation ch.lock(0L, Long.MAX_VALUE, false)
| |
| open | back to summary | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| public static AsynchronousFileChannel open(Path file, Set<? extends OpenOption> options, ExecutorService executor, FileAttribute<?>... attrs) throws IOException Opens or creates a file for reading and/or writing, returning an asynchronous file channel to access the file. The In addition to
An implementation may also support additional options. The The The new channel is created by invoking the
| |||||||||||||||||
| open | back to summary |
|---|---|
| public static AsynchronousFileChannel open(Path file, OpenOption... options) throws IOException Opens or creates a file for reading and/or writing, returning an asynchronous file channel to access the file. An invocation of this method behaves in exactly the same way as the invocation ch.open(file, opts, null, new FileAttribute<?>[0]); opts is a Set containing the options specified to
this method.
The resulting channel is associated with default thread pool to which tasks are submitted to handle I/O events and dispatch to completion handlers that consume the result of asynchronous operations performed on the resulting channel.
| |
| read | back to summary |
|---|---|
| public abstract <A> void read(ByteBuffer dst, long position, A attachment, CompletionHandler<Integer, ? super A> handler) Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. 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 This method works in the same manner as the
| |
| read | back to summary |
|---|---|
| public abstract Future Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. 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 This method works in the same manner as the
| |
| size | back to summary |
|---|---|
| public abstract long size() throws IOException Returns the current size of this channel's file.
| |
| truncate | back to summary |
|---|---|
| public abstract AsynchronousFileChannel truncate(long size) throws IOException Truncates this channel's file to the given size. If the given size is less than the file's current size then the file is truncated, discarding any bytes beyond the new end of the file. If the given size is greater than or equal to the file's current size then the file is not modified.
| |
| tryLock | back to summary |
|---|---|
| public abstract FileLock tryLock(long position, long size, boolean shared) throws IOException Attempts to acquire a lock on the given region of this channel's file. 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
| |
| tryLock | back to summary |
|---|---|
| public final FileLock tryLock() throws IOException Attempts to acquire an exclusive lock on this channel's file. An invocation of this method of the form ch.tryLock(0L, Long.MAX_VALUE, false)
| |
| write | back to summary |
|---|---|
| public abstract <A> void write(ByteBuffer src, long position, A attachment, CompletionHandler<Integer, ? super A> handler) Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. This method works in the same manner as the
| |
| write | back to summary |
|---|---|
| public abstract Future Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. 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 This method works in the same manner as the
| |