# AsynchronousFileChannel

public abstract class **AsynchronousFileChannel** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
implements [AsynchronousChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousChannel.html) An asynchronous channel for reading, writing, and manipulating a file.

An asynchronous file channel is created when a file is opened by invoking
one of the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#open(java.nio.file.Path,%20java.nio.file.OpenOption...)` 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#size()`. The size of the file increases
when bytes are written beyond its current size; the size of the file decreases
when it is `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#truncate(long)`.

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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html`
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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html` 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#force(boolean)` 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock()` 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousChannelGroup.html` class.

Channels of this type are safe for use by multiple concurrent threads. The
`https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html#close()` method may be invoked at any time, as specified
by the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html` interface. This causes all outstanding asynchronous
operations on the channel to complete with the exception `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousCloseException.html`. 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
`https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html` 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html`, 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.

### Protected Constructor Summary

|---|---|
|   | [AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#AsynchronousFileChannel())() Initializes a new instance of this class. |

### Public Method Summary

|---|---|
| abstract void | [force](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#force(boolean))(boolean metaData) Forces any updates to this channel's file to be written to the storage device that contains it. |
| abstract \<A\> void | [lock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(long,%20long,%20boolean,%20A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>))(long position, long size, boolean shared, A attachment, [CompletionHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html)\<[FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html), ? super A\> handler) Acquires a lock on the given region of this channel's file. |
| abstract [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<[FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html)\> | [lock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(long,%20long,%20boolean))(long position, long size, boolean shared) Acquires a lock on the given region of this channel's file. |
| final \<A\> void | [lock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>))(A attachment, [CompletionHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html)\<[FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html), ? super A\> handler) Acquires an exclusive lock on this channel's file. |
| final [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<[FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html)\> | [lock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock())() Acquires an exclusive lock on this channel's file. |
| static [AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html) | [open](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#open(java.nio.file.Path,%20java.nio.file.OpenOption...))([Path](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Path.html) file, [OpenOption...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/OpenOption.html) options) Opens or creates a file for reading and/or writing, returning an asynchronous file channel to access the file. |
| static [AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html) | [open](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#open(java.nio.file.Path,%20java.util.Set<?%20extends%20java.nio.file.OpenOption>,%20java.util.concurrent.ExecutorService,%20java.nio.file.attribute.FileAttribute<?>...))([Path](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Path.html) file, [Set](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html)\<? extends [OpenOption](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/OpenOption.html)\> options, [ExecutorService](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html) executor, [FileAttribute...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/attribute/FileAttribute.html)\<?\> attrs) Opens or creates a file for reading and/or writing, returning an asynchronous file channel to access the file. |
| abstract \<A\> void | [read](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#read(java.nio.ByteBuffer,%20long,%20A,%20java.nio.channels.CompletionHandler<java.lang.Integer,%20?%20super%20A>))([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) dst, long position, A attachment, [CompletionHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html)\<[Integer](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Integer.html), ? super A\> handler) Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. |
| abstract [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<[Integer](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Integer.html)\> | [read](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#read(java.nio.ByteBuffer,%20long))([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) dst, long position) Reads a sequence of bytes from this channel into the given buffer, starting at the given file position. |
| abstract long | [size](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#size())() Returns the current size of this channel's file. |
| abstract [AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html) | [truncate](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#truncate(long))(long size) Truncates this channel's file to the given size. |
| abstract [FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html) | [tryLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#tryLock(long,%20long,%20boolean))(long position, long size, boolean shared) Attempts to acquire a lock on the given region of this channel's file. |
| final [FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html) | [tryLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#tryLock())() Attempts to acquire an exclusive lock on this channel's file. |
| abstract [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<[Integer](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Integer.html)\> | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#write(java.nio.ByteBuffer,%20long))([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) src, long position) Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. |
| abstract \<A\> void | [write](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#write(java.nio.ByteBuffer,%20long,%20A,%20java.nio.channels.CompletionHandler<java.lang.Integer,%20?%20super%20A>))([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) src, long position, A attachment, [CompletionHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html)\<[Integer](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Integer.html), ? super A\> handler) Writes a sequence of bytes to this channel from the given buffer, starting at the given file position. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

From interface [java.nio.channels.AsynchronousChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousChannel.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousChannel.html#close())() Closes this channel. |

From interface [java.nio.channels.Channel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html#close())() Closes this channel. |
| abstract boolean | [isOpen](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html#isOpen())() Tells whether or not this channel is open. |

From interface [java.io.Closeable](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/Closeable.html#close())() Closes this stream and releases any system resources associated with it. |

From interface [java.lang.AutoCloseable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html#close())() Closes this resource, relinquishing any underlying resources. |

## Protected Constructors

#### protected
**AsynchronousFileChannel**
()

Initializes a new instance of this class.

## Public Methods

#### public abstract void
**force**
(boolean metaData)

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 `metaData` parameter can be used to limit the number of
I/O operations that this method is required to perform. Passing
`false` for this parameter indicates that only updates to the
file's content need be written to storage; passing `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.

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.

##### Parameters

| metaData | If `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 written |
|---|---|

##### Throws

| [ClosedChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ClosedChannelException.html) | If this channel is closed |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If some other I/O error occurs |
|---|---|

#### public abstract void
**lock**
(long position, long size, boolean shared, A attachment, [CompletionHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html)\<[FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html), ? 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 `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 `FileLock`.

The region specified by the `position` and `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
`https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>)` method simply locks a region
of size `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Long.html#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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/OverlappingFileLockException.html`.

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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#isShared()` method.

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.

##### Parameters

| position | The position at which the locked region is to start; must be non-negative |
| size | The size of the locked region; must be non-negative, and the sum `position` + `size` must be non-negative |
| shared | `true` 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) |
| attachment | The object to attach to the I/O operation; can be `null` |
| handler | The handler for consuming the result |
|---|---|

##### Throws

| [OverlappingFileLockException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/OverlappingFileLockException.html) | 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 |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the preconditions on the parameters do not hold |
| [NonReadableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonReadableChannelException.html) | If `shared` is true but this channel was not opened for reading |
| [NonWritableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonWritableChannelException.html) | If `shared` is false but this channel was not opened for writing |
|---|---|

#### public abstract [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<[FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html)\>
**lock**
(long position, long size, boolean shared)

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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(long,%20long,%20boolean,%20A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>)`
method except that instead of specifying a completion handler, this
method returns a `Future` representing the pending result. The
`Future`'s `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get()` method returns the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html` on successful completion.

##### Parameters

| position | The position at which the locked region is to start; must be non-negative |
| size | The size of the locked region; must be non-negative, and the sum `position` + `size` must be non-negative |
| shared | `true` 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) |
|---|---|

##### Returns

- a `Future` object representing the pending result

##### Throws

| [OverlappingFileLockException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/OverlappingFileLockException.html) | If a lock is already held by this Java virtual machine, or there is already a pending attempt to lock a region |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the preconditions on the parameters do not hold |
| [NonReadableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonReadableChannelException.html) | If `shared` is true but this channel was not opened for reading |
| [NonWritableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonWritableChannelException.html) | If `shared` is false but this channel was not opened for writing |
|---|---|

#### public final void
**lock**
(A attachment, [CompletionHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html)\<[FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html), ? 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 `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 `FileLock`.

An invocation of this method of the form `ch.lock(att,handler)`
behaves in exactly the same way as the invocation

         ch.https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(long,%20long,%20boolean,%20A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>)(0L, Long.MAX_VALUE, false, att, handler)
     
<br />

##### Parameters

| attachment | The object to attach to the I/O operation; can be `null` |
| handler | The handler for consuming the result |
|---|---|

##### Throws

| [OverlappingFileLockException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/OverlappingFileLockException.html) | If a lock is already held by this Java virtual machine, or there is already a pending attempt to lock a region |
| [NonWritableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonWritableChannelException.html) | If this channel was not opened for writing |
|---|---|

#### public final [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<[FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html)\>
**lock**
()

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 `Future` representing the
pending result of the operation. The `Future`'s `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get()` method returns the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html` on successful completion.

An invocation of this method behaves in exactly the same way as the
invocation

         ch.https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(long,%20long,%20boolean)(0L, Long.MAX_VALUE, false)
     
<br />

##### Returns

- a `Future` object representing the pending result

##### Throws

| [OverlappingFileLockException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/OverlappingFileLockException.html) | If a lock is already held by this Java virtual machine, or there is already a pending attempt to lock a region |
| [NonWritableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonWritableChannelException.html) | If this channel was not opened for writing |
|---|---|

#### public static [AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html)
**open**
([Path](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Path.html) file, [OpenOption...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/OpenOption.html) options)

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.https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#open(java.nio.file.Path,%20java.util.Set<?%20extends%20java.nio.file.OpenOption>,%20java.util.concurrent.ExecutorService,%20java.nio.file.attribute.FileAttribute<?>...)(file, opts, null, new FileAttribute<?>[0]);
     
where `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.

##### Parameters

| file | The path of the file to open or create |
| options | Options specifying how the file is opened |
|---|---|

##### Returns

- A new asynchronous file channel

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the set contains an invalid combination of options |
| [UnsupportedOperationException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/UnsupportedOperationException.html) | If the `file` is associated with a provider that does not support creating file channels, or an unsupported open option is specified |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If an I/O error occurs |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | If a security manager is installed and it denies an unspecified permission required by the implementation. In the case of the default provider, the `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkRead(java.lang.String)` method is invoked to check read access if the file is opened for reading. The `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkWrite(java.lang.String)` method is invoked to check write access if the file is opened for writing |
|---|---|

#### public static [AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html)
**open**
([Path](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/Path.html) file, [Set](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/Set.html)\<? extends [OpenOption](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/OpenOption.html)\> options, [ExecutorService](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html) executor, [FileAttribute...](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/attribute/FileAttribute.html)\<?\> attrs)

Opens or creates a file for reading and/or writing, returning an
asynchronous file channel to access the file.

The `options` parameter determines how the file is opened.
The `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#READ` and `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#WRITE` options determines if the file should be opened for reading and/or
writing. If neither option is contained in the array then an existing file
is opened for reading.

In addition to `READ` and `WRITE`, the following options
may be present:

| Option | Description |
|---|---|
| `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#TRUNCATE_EXISTING` | When opening an existing file, the file is first truncated to a size of 0 bytes. This option is ignored when the file is opened only for reading. |
| `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#CREATE_NEW` | If this option is present then a new file is created, failing if the file already exists. When creating a file the check for the existence of the file and the creation of the file if it does not exist is atomic with respect to other file system operations. This option is ignored when the file is opened only for reading. |
| `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#CREATE` | If this option is present then an existing file is opened if it exists, otherwise a new file is created. When creating a file the check for the existence of the file and the creation of the file if it does not exist is atomic with respect to other file system operations. This option is ignored if the `CREATE_NEW` option is also present or the file is opened only for reading. |
| `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#DELETE_ON_CLOSE` | When this option is present then the implementation makes a *best effort* attempt to delete the file when closed by the the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousChannel.html#close()` method. If the `close` method is not invoked then a *best effort* attempt is made to delete the file when the Java virtual machine terminates. |
| `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#SPARSE` | When creating a new file this option is a *hint* that the new file will be sparse. This option is ignored when not creating a new file. |
| `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#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](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/package-summary.html#integrity)). |
| `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/StandardOpenOption.html#DSYNC` | Requires that every update to the file's content be written synchronously to the underlying storage device. (see [Synchronized I/O file integrity](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/package-summary.html#integrity)). |

An implementation may also support additional options.

The `executor` parameter is the `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/ExecutorService.html` to
which tasks are submitted to handle I/O events and dispatch completion
results for operations initiated on resulting channel.
The nature of these tasks is highly implementation specific and so care
should be taken when configuring the `Executor`. Minimally it
should support an unbounded work queue and should not run tasks on the
caller thread of the `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Executor.html#execute(java.lang.Runnable)` method.
Shutting down the executor service while the channel is open results in
unspecified behavior.

The `attrs` parameter is an optional array of file `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/attribute/FileAttribute.html` to set atomically when creating the file.

The new channel is created by invoking the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/file/spi/FileSystemProvider.html#newFileChannel(java.nio.file.Path,%20java.util.Set<?%20extends%20java.nio.file.OpenOption>,%20java.nio.file.attribute.FileAttribute<?>...)` method on the
provider that created the `Path`.

##### Parameters

| file | The path of the file to open or create |
| options | Options specifying how the file is opened |
| executor | The thread pool or `null` to associate the channel with the default thread pool |
| attrs | An optional list of file attributes to set atomically when creating the file |
|---|---|

##### Returns

- A new asynchronous file channel

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the set contains an invalid combination of options |
| [UnsupportedOperationException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/UnsupportedOperationException.html) | If the `file` is associated with a provider that does not support creating asynchronous file channels, or an unsupported open option is specified, or the array contains an attribute that cannot be set atomically when creating the file |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If an I/O error occurs |
| [SecurityException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityException.html) | If a security manager is installed and it denies an unspecified permission required by the implementation. In the case of the default provider, the `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkRead(java.lang.String)` method is invoked to check read access if the file is opened for reading. The `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/SecurityManager.html#checkWrite(java.lang.String)` method is invoked to check write access if the file is opened for writing |
|---|---|

#### public abstract void
**read**
([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) dst, long position, A attachment, [CompletionHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html)\<[Integer](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Integer.html), ? 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 `-1` if the given
position is greater than or equal to the file's size at the time that the
read is attempted.

This method works in the same manner as the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html#read(java.nio.ByteBuffer,%20A,%20java.nio.channels.CompletionHandler<java.lang.Integer,%20?%20super%20A>)`
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.

##### Parameters

| dst | The buffer into which bytes are to be transferred |
| position | The file position at which the transfer is to begin; must be non-negative |
| attachment | The object to attach to the I/O operation; can be `null` |
| handler | The handler for consuming the result |
|---|---|

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the position is negative or the buffer is read-only |
| [NonReadableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonReadableChannelException.html) | If this channel was not opened for reading |
|---|---|

#### public abstract [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<[Integer](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Integer.html)\>
**read**
([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) dst, long position)

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 `Future` representing the pending result of the
operation. The `Future`'s `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get()` method returns
the number of bytes read or `-1` if the given position is greater
than or equal to the file's size at the time that the read is attempted.

This method works in the same manner as the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html#read(java.nio.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
no bytes are read.

##### Parameters

| dst | The buffer into which bytes are to be transferred |
| position | The file position at which the transfer is to begin; must be non-negative |
|---|---|

##### Returns

- A `Future` object representing the pending result

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the position is negative or the buffer is read-only |
| [NonReadableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonReadableChannelException.html) | If this channel was not opened for reading |
|---|---|

#### public abstract long
**size**
()

Returns the current size of this channel's file.

##### Returns

- The current size of this channel's file, measured in bytes

##### Throws

| [ClosedChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ClosedChannelException.html) | If this channel is closed |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If some other I/O error occurs |
|---|---|

#### public abstract [AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html)
**truncate**
(long size)

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.

<br />

##### Parameters

| size | The new size, a non-negative byte count |
|---|---|

##### Returns

- This file channel

##### Throws

| [NonWritableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonWritableChannelException.html) | If this channel was not opened for writing |
| [ClosedChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ClosedChannelException.html) | If this channel is closed |
| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the new size is negative |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If some other I/O error occurs |
|---|---|

#### public abstract [FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html)
**tryLock**
(long position, long size, boolean shared)

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 `null`. If it fails to acquire
a lock for any other reason then an appropriate exception is thrown.

##### Parameters

| position | The position at which the locked region is to start; must be non-negative |
| size | The size of the locked region; must be non-negative, and the sum `position` + `size` must be non-negative |
| shared | `true` to request a shared lock, `false` to request an exclusive lock |
|---|---|

##### Returns

- A lock object representing the newly-acquired lock, or `null` if the lock could not be acquired because another program holds an overlapping lock

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the preconditions on the parameters do not hold |
| [ClosedChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ClosedChannelException.html) | If this channel is closed |
| [OverlappingFileLockException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/OverlappingFileLockException.html) | 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 |
| [NonReadableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonReadableChannelException.html) | If `shared` is true but this channel was not opened for reading |
| [NonWritableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonWritableChannelException.html) | If `shared` is false but this channel was not opened for writing |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If some other I/O error occurs |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(long,%20long,%20boolean,%20A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#tryLock()`

#### public final [FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html)
**tryLock**
()

Attempts to acquire an exclusive lock on this channel's file.

An invocation of this method of the form `ch.tryLock()`
behaves in exactly the same way as the invocation

         ch.https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#tryLock(long,%20long,%20boolean)(0L, Long.MAX_VALUE, false) 

<br />

##### Returns

- A lock object representing the newly-acquired lock, or `null` if the lock could not be acquired because another program holds an overlapping lock

##### Throws

| [ClosedChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ClosedChannelException.html) | If this channel is closed |
| [OverlappingFileLockException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/OverlappingFileLockException.html) | 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 |
| [NonWritableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonWritableChannelException.html) | If `shared` is false but this channel was not opened for writing |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If some other I/O error occurs |
|---|---|

##### See Also

- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(long,%20long,%20boolean,%20A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>)`
- `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#tryLock(long,%20long,%20boolean)`

#### public abstract [Future](https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html)\<[Integer](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Integer.html)\>
**write**
([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) src, long position)

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 `Future` representing the pending result of the
write operation. The `Future`'s `https://developers.google.com/j2objc/javadoc/jre/reference/java/util/concurrent/Future.html#get()` method
returns the number of bytes written.

This method works in the same manner as the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html#write(java.nio.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
are unspecified.

##### Parameters

| src | The buffer from which bytes are to be transferred |
| position | The file position at which the transfer is to begin; must be non-negative |
|---|---|

##### Returns

- A `Future` object representing the pending result

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the position is negative |
| [NonWritableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonWritableChannelException.html) | If this channel was not opened for writing |
|---|---|

#### public abstract void
**write**
([ByteBuffer](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/ByteBuffer.html) src, long position, A attachment, [CompletionHandler](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/CompletionHandler.html)\<[Integer](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Integer.html), ? 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousByteChannel.html#write(java.nio.ByteBuffer,%20A,%20java.nio.channels.CompletionHandler<java.lang.Integer,%20?%20super%20A>)`
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.

##### Parameters

| src | The buffer from which bytes are to be transferred |
| position | The file position at which the transfer is to begin; must be non-negative |
| attachment | The object to attach to the I/O operation; can be `null` |
| handler | The handler for consuming the result |
|---|---|

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the position is negative |
| [NonWritableChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/NonWritableChannelException.html) | If this channel was not opened for writing |
|---|---|