Path: blob/master/src/java.base/share/classes/java/nio/channels/AsynchronousFileChannel.java
41159 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.nio.channels;2627import java.nio.file.*;28import java.nio.file.attribute.FileAttribute;29import java.nio.file.spi.*;30import java.nio.ByteBuffer;31import java.io.IOException;32import java.util.concurrent.Future;33import java.util.concurrent.ExecutorService;34import java.util.Set;35import java.util.HashSet;36import java.util.Collections;3738/**39* An asynchronous channel for reading, writing, and manipulating a file.40*41* <p> An asynchronous file channel is created when a file is opened by invoking42* one of the {@link #open open} methods defined by this class. The file contains43* a variable-length sequence of bytes that can be read and written and whose44* current size can be {@link #size() queried}. The size of the file increases45* when bytes are written beyond its current size; the size of the file decreases46* when it is {@link #truncate truncated}.47*48* <p> An asynchronous file channel does not have a <i>current position</i>49* within the file. Instead, the file position is specified to each read and50* write method that initiates asynchronous operations. A {@link CompletionHandler}51* is specified as a parameter and is invoked to consume the result of the I/O52* operation. This class also defines read and write methods that initiate53* asynchronous operations, returning a {@link Future} to represent the pending54* result of the operation. The {@code Future} may be used to check if the55* operation has completed, wait for its completion, and retrieve the result.56*57* <p> In addition to read and write operations, this class defines the58* following operations: </p>59*60* <ul>61*62* <li><p> Updates made to a file may be {@link #force <i>forced63* out</i>} to the underlying storage device, ensuring that data are not64* lost in the event of a system crash. </p></li>65*66* <li><p> A region of a file may be {@link #lock <i>locked</i>} against67* access by other programs. </p></li>68*69* </ul>70*71* <p> An {@code AsynchronousFileChannel} is associated with a thread pool to72* which tasks are submitted to handle I/O events and dispatch to completion73* handlers that consume the results of I/O operations on the channel. The74* completion handler for an I/O operation initiated on a channel is guaranteed75* to be invoked by one of the threads in the thread pool (This ensures that the76* completion handler is run by a thread with the expected <em>identity</em>).77* Where an I/O operation completes immediately, and the initiating thread is78* itself a thread in the thread pool, then the completion handler may be invoked79* directly by the initiating thread. When an {@code AsynchronousFileChannel} is80* created without specifying a thread pool then the channel is associated with81* a system-dependent default thread pool that may be shared with other82* channels. The default thread pool is configured by the system properties83* defined by the {@link AsynchronousChannelGroup} class.84*85* <p> Channels of this type are safe for use by multiple concurrent threads. The86* {@link Channel#close close} method may be invoked at any time, as specified87* by the {@link Channel} interface. This causes all outstanding asynchronous88* operations on the channel to complete with the exception {@link89* AsynchronousCloseException}. Multiple read and write operations may be90* outstanding at the same time. When multiple read and write operations are91* outstanding then the ordering of the I/O operations, and the order that the92* completion handlers are invoked, is not specified; they are not, in particular,93* guaranteed to execute in the order that the operations were initiated. The94* {@link java.nio.ByteBuffer ByteBuffers} used when reading or writing are not95* safe for use by multiple concurrent I/O operations. Furthermore, after an I/O96* operation is initiated then care should be taken to ensure that the buffer is97* not accessed until after the operation has completed.98*99* <p> As with {@link FileChannel}, the view of a file provided by an instance of100* this class is guaranteed to be consistent with other views of the same file101* provided by other instances in the same program. The view provided by an102* instance of this class may or may not, however, be consistent with the views103* seen by other concurrently-running programs due to caching performed by the104* underlying operating system and delays induced by network-filesystem protocols.105* This is true regardless of the language in which these other programs are106* written, and whether they are running on the same machine or on some other107* machine. The exact nature of any such inconsistencies are system-dependent108* and are therefore unspecified.109*110* @since 1.7111*/112113public abstract class AsynchronousFileChannel114implements AsynchronousChannel115{116/**117* Initializes a new instance of this class.118*/119protected AsynchronousFileChannel() {120}121122/**123* Opens or creates a file for reading and/or writing, returning an124* asynchronous file channel to access the file.125*126* <p> The {@code options} parameter determines how the file is opened.127* The {@link StandardOpenOption#READ READ} and {@link StandardOpenOption#WRITE128* WRITE} options determines if the file should be opened for reading and/or129* writing. If neither option is contained in the array then an existing file130* is opened for reading.131*132* <p> In addition to {@code READ} and {@code WRITE}, the following options133* may be present:134*135* <table class="striped">136* <caption style="display:none">additional options</caption>137* <thead>138* <tr> <th scope="col">Option</th> <th scope="col">Description</th> </tr>139* </thead>140* <tbody>141* <tr>142* <th scope="row"> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </th>143* <td> When opening an existing file, the file is first truncated to a144* size of 0 bytes. This option is ignored when the file is opened only145* for reading.</td>146* </tr>147* <tr>148* <th scope="row"> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </th>149* <td> If this option is present then a new file is created, failing if150* the file already exists. When creating a file the check for the151* existence of the file and the creation of the file if it does not exist152* is atomic with respect to other file system operations. This option is153* ignored when the file is opened only for reading. </td>154* </tr>155* <tr>156* <th scope="row" > {@link StandardOpenOption#CREATE CREATE} </th>157* <td> If this option is present then an existing file is opened if it158* exists, otherwise a new file is created. When creating a file the check159* for the existence of the file and the creation of the file if it does160* not exist is atomic with respect to other file system operations. This161* option is ignored if the {@code CREATE_NEW} option is also present or162* the file is opened only for reading. </td>163* </tr>164* <tr>165* <th scope="row" > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </th>166* <td> When this option is present then the implementation makes a167* <em>best effort</em> attempt to delete the file when closed by168* the {@link #close close} method. If the {@code close} method is not169* invoked then a <em>best effort</em> attempt is made to delete the file170* when the Java virtual machine terminates. </td>171* </tr>172* <tr>173* <th scope="row">{@link StandardOpenOption#SPARSE SPARSE} </th>174* <td> When creating a new file this option is a <em>hint</em> that the175* new file will be sparse. This option is ignored when not creating176* a new file. </td>177* </tr>178* <tr>179* <th scope="row"> {@link StandardOpenOption#SYNC SYNC} </th>180* <td> Requires that every update to the file's content or metadata be181* written synchronously to the underlying storage device. (see <a182* href="../file/package-summary.html#integrity"> Synchronized I/O file183* integrity</a>). </td>184* </tr>185* <tr>186* <th scope="row"> {@link StandardOpenOption#DSYNC DSYNC} </th>187* <td> Requires that every update to the file's content be written188* synchronously to the underlying storage device. (see <a189* href="../file/package-summary.html#integrity"> Synchronized I/O file190* integrity</a>). </td>191* </tr>192* </tbody>193* </table>194*195* <p> An implementation may also support additional options.196*197* <p> The {@code executor} parameter is the {@link ExecutorService} to198* which tasks are submitted to handle I/O events and dispatch completion199* results for operations initiated on resulting channel.200* The nature of these tasks is highly implementation specific and so care201* should be taken when configuring the {@code Executor}. Minimally it202* should support an unbounded work queue and should not run tasks on the203* caller thread of the {@link ExecutorService#execute execute} method.204* Shutting down the executor service while the channel is open results in205* unspecified behavior.206*207* <p> The {@code attrs} parameter is an optional array of file {@link208* FileAttribute file-attributes} to set atomically when creating the file.209*210* <p> The new channel is created by invoking the {@link211* FileSystemProvider#newAsynchronousFileChannel newAsynchronousFileChannel}212* method on the provider that created the {@code Path}.213*214* @param file215* The path of the file to open or create216* @param options217* Options specifying how the file is opened218* @param executor219* The thread pool or {@code null} to associate the channel with220* the default thread pool221* @param attrs222* An optional list of file attributes to set atomically when223* creating the file224*225* @return A new asynchronous file channel226*227* @throws IllegalArgumentException228* If the set contains an invalid combination of options229* @throws UnsupportedOperationException230* If the {@code file} is associated with a provider that does not231* support creating asynchronous file channels, or an unsupported232* open option is specified, or the array contains an attribute that233* cannot be set atomically when creating the file234* @throws FileAlreadyExistsException235* If a file of that name already exists and the {@link236* StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified237* and the file is being opened for writing238* <i>(<a href="../file/package-summary.html#optspecex">optional239* specific exception</a>)</i>240* @throws IOException241* If an I/O error occurs242* @throws SecurityException243* If a security manager is installed and it denies an244* unspecified permission required by the implementation.245* In the case of the default provider, the {@link246* SecurityManager#checkRead(String)} method is invoked to check247* read access if the file is opened for reading. The {@link248* SecurityManager#checkWrite(String)} method is invoked to check249* write access if the file is opened for writing250*/251public static AsynchronousFileChannel open(Path file,252Set<? extends OpenOption> options,253ExecutorService executor,254FileAttribute<?>... attrs)255throws IOException256{257FileSystemProvider provider = file.getFileSystem().provider();258return provider.newAsynchronousFileChannel(file, options, executor, attrs);259}260261@SuppressWarnings({"unchecked", "rawtypes"}) // generic array construction262private static final FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0];263264/**265* Opens or creates a file for reading and/or writing, returning an266* asynchronous file channel to access the file.267*268* <p> An invocation of this method behaves in exactly the same way as the269* invocation270* <pre>271* ch.{@link #open(Path,Set,ExecutorService,FileAttribute[])272* open}(file, opts, null, new FileAttribute<?>[0]);273* </pre>274* where {@code opts} is a {@code Set} containing the options specified to275* this method.276*277* <p> The resulting channel is associated with default thread pool to which278* tasks are submitted to handle I/O events and dispatch to completion279* handlers that consume the result of asynchronous operations performed on280* the resulting channel.281*282* @param file283* The path of the file to open or create284* @param options285* Options specifying how the file is opened286*287* @return A new asynchronous file channel288*289* @throws IllegalArgumentException290* If the set contains an invalid combination of options291* @throws UnsupportedOperationException292* If the {@code file} is associated with a provider that does not293* support creating file channels, or an unsupported open option is294* specified295* @throws FileAlreadyExistsException296* If a file of that name already exists and the {@link297* StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified298* and the file is being opened for writing299* <i>(<a href="../file/package-summary.html#optspecex">optional300* specific exception</a>)</i>301* @throws IOException302* If an I/O error occurs303* @throws SecurityException304* If a security manager is installed and it denies an305* unspecified permission required by the implementation.306* In the case of the default provider, the {@link307* SecurityManager#checkRead(String)} method is invoked to check308* read access if the file is opened for reading. The {@link309* SecurityManager#checkWrite(String)} method is invoked to check310* write access if the file is opened for writing311*/312public static AsynchronousFileChannel open(Path file, OpenOption... options)313throws IOException314{315Set<OpenOption> set;316if (options.length == 0) {317set = Collections.emptySet();318} else {319set = new HashSet<>();320Collections.addAll(set, options);321}322return open(file, set, null, NO_ATTRIBUTES);323}324325/**326* Returns the current size of this channel's file.327*328* @return The current size of this channel's file, measured in bytes329*330* @throws ClosedChannelException331* If this channel is closed332* @throws IOException333* If some other I/O error occurs334*/335public abstract long size() throws IOException;336337/**338* Truncates this channel's file to the given size.339*340* <p> If the given size is less than the file's current size then the file341* is truncated, discarding any bytes beyond the new end of the file. If342* the given size is greater than or equal to the file's current size then343* the file is not modified. </p>344*345* @param size346* The new size, a non-negative byte count347*348* @return This file channel349*350* @throws NonWritableChannelException351* If this channel was not opened for writing352*353* @throws ClosedChannelException354* If this channel is closed355*356* @throws IllegalArgumentException357* If the new size is negative358*359* @throws IOException360* If some other I/O error occurs361*/362public abstract AsynchronousFileChannel truncate(long size) throws IOException;363364/**365* Forces any updates to this channel's file to be written to the storage366* device that contains it.367*368* <p> If this channel's file resides on a local storage device then when369* this method returns it is guaranteed that all changes made to the file370* since this channel was created, or since this method was last invoked,371* will have been written to that device. This is useful for ensuring that372* critical information is not lost in the event of a system crash.373*374* <p> If the file does not reside on a local device then no such guarantee375* is made.376*377* <p> The {@code metaData} parameter can be used to limit the number of378* I/O operations that this method is required to perform. Passing379* {@code false} for this parameter indicates that only updates to the380* file's content need be written to storage; passing {@code true}381* indicates that updates to both the file's content and metadata must be382* written, which generally requires at least one more I/O operation.383* Whether this parameter actually has any effect is dependent upon the384* underlying operating system and is therefore unspecified.385*386* <p> Invoking this method may cause an I/O operation to occur even if the387* channel was only opened for reading. Some operating systems, for388* example, maintain a last-access time as part of a file's metadata, and389* this time is updated whenever the file is read. Whether or not this is390* actually done is system-dependent and is therefore unspecified.391*392* <p> This method is only guaranteed to force changes that were made to393* this channel's file via the methods defined in this class.394*395* @param metaData396* If {@code true} then this method is required to force changes397* to both the file's content and metadata to be written to398* storage; otherwise, it need only force content changes to be399* written400*401* @throws ClosedChannelException402* If this channel is closed403*404* @throws IOException405* If some other I/O error occurs406*/407public abstract void force(boolean metaData) throws IOException;408409/**410* Acquires a lock on the given region of this channel's file.411*412* <p> This method initiates an operation to acquire a lock on the given413* region of this channel's file. The {@code handler} parameter is a414* completion handler that is invoked when the lock is acquired (or the415* operation fails). The result passed to the completion handler is the416* resulting {@code FileLock}.417*418* <p> The region specified by the {@code position} and {@code size}419* parameters need not be contained within, or even overlap, the actual420* underlying file. Lock regions are fixed in size; if a locked region421* initially contains the end of the file and the file grows beyond the422* region then the new portion of the file will not be covered by the lock.423* If a file is expected to grow in size and a lock on the entire file is424* required then a region starting at zero, and no smaller than the425* expected maximum size of the file, should be locked. The two-argument426* {@link #lock(Object,CompletionHandler)} method simply locks a region427* of size {@link Long#MAX_VALUE}. If a lock that overlaps the requested428* region is already held by this Java virtual machine, or this method has429* been invoked to lock an overlapping region and that operation has not430* completed, then this method throws {@link OverlappingFileLockException}.431*432* <p> Some operating systems do not support a mechanism to acquire a file433* lock in an asynchronous manner. Consequently an implementation may434* acquire the file lock in a background thread or from a task executed by435* a thread in the associated thread pool. If there are many lock operations436* outstanding then it may consume threads in the Java virtual machine for437* indefinite periods.438*439* <p> Some operating systems do not support shared locks, in which case a440* request for a shared lock is automatically converted into a request for441* an exclusive lock. Whether the newly-acquired lock is shared or442* exclusive may be tested by invoking the resulting lock object's {@link443* FileLock#isShared() isShared} method.444*445* <p> File locks are held on behalf of the entire Java virtual machine.446* They are not suitable for controlling access to a file by multiple447* threads within the same virtual machine.448*449* @param <A>450* The type of the attachment451* @param position452* The position at which the locked region is to start; must be453* non-negative454* @param size455* The size of the locked region; must be non-negative, and the sum456* {@code position} + {@code size} must be non-negative457* @param shared458* {@code true} to request a shared lock, in which case this459* channel must be open for reading (and possibly writing);460* {@code false} to request an exclusive lock, in which case this461* channel must be open for writing (and possibly reading)462* @param attachment463* The object to attach to the I/O operation; can be {@code null}464* @param handler465* The handler for consuming the result466*467* @throws OverlappingFileLockException468* If a lock that overlaps the requested region is already held by469* this Java virtual machine, or there is already a pending attempt470* to lock an overlapping region471* @throws IllegalArgumentException472* If the preconditions on the parameters do not hold473* @throws NonReadableChannelException474* If {@code shared} is true but this channel was not opened for reading475* @throws NonWritableChannelException476* If {@code shared} is false but this channel was not opened for writing477*/478public abstract <A> void lock(long position,479long size,480boolean shared,481A attachment,482CompletionHandler<FileLock,? super A> handler);483484/**485* Acquires an exclusive lock on this channel's file.486*487* <p> This method initiates an operation to acquire a lock on the given488* region of this channel's file. The {@code handler} parameter is a489* completion handler that is invoked when the lock is acquired (or the490* operation fails). The result passed to the completion handler is the491* resulting {@code FileLock}.492*493* <p> An invocation of this method of the form {@code ch.lock(att,handler)}494* behaves in exactly the same way as the invocation495* <pre>496* ch.{@link #lock(long,long,boolean,Object,CompletionHandler) lock}(0L, Long.MAX_VALUE, false, att, handler)497* </pre>498*499* @param <A>500* The type of the attachment501* @param attachment502* The object to attach to the I/O operation; can be {@code null}503* @param handler504* The handler for consuming the result505*506* @throws OverlappingFileLockException507* If a lock is already held by this Java virtual machine, or there508* is already a pending attempt to lock a region509* @throws NonWritableChannelException510* If this channel was not opened for writing511*/512public final <A> void lock(A attachment,513CompletionHandler<FileLock,? super A> handler)514{515lock(0L, Long.MAX_VALUE, false, attachment, handler);516}517518/**519* Acquires a lock on the given region of this channel's file.520*521* <p> This method initiates an operation to acquire a lock on the given522* region of this channel's file. The method behaves in exactly the same523* manner as the {@link #lock(long, long, boolean, Object, CompletionHandler)}524* method except that instead of specifying a completion handler, this525* method returns a {@code Future} representing the pending result. The526* {@code Future}'s {@link Future#get() get} method returns the {@link527* FileLock} on successful completion.528*529* @param position530* The position at which the locked region is to start; must be531* non-negative532* @param size533* The size of the locked region; must be non-negative, and the sum534* {@code position} + {@code size} must be non-negative535* @param shared536* {@code true} to request a shared lock, in which case this537* channel must be open for reading (and possibly writing);538* {@code false} to request an exclusive lock, in which case this539* channel must be open for writing (and possibly reading)540*541* @return a {@code Future} object representing the pending result542*543* @throws OverlappingFileLockException544* If a lock is already held by this Java virtual machine, or there545* is already a pending attempt to lock a region546* @throws IllegalArgumentException547* If the preconditions on the parameters do not hold548* @throws NonReadableChannelException549* If {@code shared} is true but this channel was not opened for reading550* @throws NonWritableChannelException551* If {@code shared} is false but this channel was not opened for writing552*/553public abstract Future<FileLock> lock(long position, long size, boolean shared);554555/**556* Acquires an exclusive lock on this channel's file.557*558* <p> This method initiates an operation to acquire an exclusive lock on this559* channel's file. The method returns a {@code Future} representing the560* pending result of the operation. The {@code Future}'s {@link Future#get()561* get} method returns the {@link FileLock} on successful completion.562*563* <p> An invocation of this method behaves in exactly the same way as the564* invocation565* <pre>566* ch.{@link #lock(long,long,boolean) lock}(0L, Long.MAX_VALUE, false)567* </pre>568*569* @return a {@code Future} object representing the pending result570*571* @throws OverlappingFileLockException572* If a lock is already held by this Java virtual machine, or there573* is already a pending attempt to lock a region574* @throws NonWritableChannelException575* If this channel was not opened for writing576*/577public final Future<FileLock> lock() {578return lock(0L, Long.MAX_VALUE, false);579}580581/**582* Attempts to acquire a lock on the given region of this channel's file.583*584* <p> This method does not block. An invocation always returns immediately,585* either having acquired a lock on the requested region or having failed to586* do so. If it fails to acquire a lock because an overlapping lock is held587* by another program then it returns {@code null}. If it fails to acquire588* a lock for any other reason then an appropriate exception is thrown.589*590* @param position591* The position at which the locked region is to start; must be592* non-negative593*594* @param size595* The size of the locked region; must be non-negative, and the sum596* {@code position} + {@code size} must be non-negative597*598* @param shared599* {@code true} to request a shared lock,600* {@code false} to request an exclusive lock601*602* @return A lock object representing the newly-acquired lock,603* or {@code null} if the lock could not be acquired604* because another program holds an overlapping lock605*606* @throws IllegalArgumentException607* If the preconditions on the parameters do not hold608* @throws ClosedChannelException609* If this channel is closed610* @throws OverlappingFileLockException611* If a lock that overlaps the requested region is already held by612* this Java virtual machine, or if another thread is already613* blocked in this method and is attempting to lock an overlapping614* region of the same file615* @throws NonReadableChannelException616* If {@code shared} is true but this channel was not opened for reading617* @throws NonWritableChannelException618* If {@code shared} is false but this channel was not opened for writing619*620* @throws IOException621* If some other I/O error occurs622*623* @see #lock(Object,CompletionHandler)624* @see #lock(long,long,boolean,Object,CompletionHandler)625* @see #tryLock()626*/627public abstract FileLock tryLock(long position, long size, boolean shared)628throws IOException;629630/**631* Attempts to acquire an exclusive lock on this channel's file.632*633* <p> An invocation of this method of the form {@code ch.tryLock()}634* behaves in exactly the same way as the invocation635*636* <pre>637* ch.{@link #tryLock(long,long,boolean) tryLock}(0L, Long.MAX_VALUE, false) </pre>638*639* @return A lock object representing the newly-acquired lock,640* or {@code null} if the lock could not be acquired641* because another program holds an overlapping lock642*643* @throws ClosedChannelException644* If this channel is closed645* @throws OverlappingFileLockException646* If a lock that overlaps the requested region is already held by647* this Java virtual machine, or if another thread is already648* blocked in this method and is attempting to lock an overlapping649* region650* @throws NonWritableChannelException651* If {@code shared} is false but this channel was not opened for writing652*653* @throws IOException654* If some other I/O error occurs655*656* @see #lock(Object,CompletionHandler)657* @see #lock(long,long,boolean,Object,CompletionHandler)658* @see #tryLock(long,long,boolean)659*/660public final FileLock tryLock() throws IOException {661return tryLock(0L, Long.MAX_VALUE, false);662}663664/**665* Reads a sequence of bytes from this channel into the given buffer,666* starting at the given file position.667*668* <p> This method initiates the reading of a sequence of bytes from this669* channel into the given buffer, starting at the given file position. The670* result of the read is the number of bytes read or {@code -1} if the given671* position is greater than or equal to the file's size at the time that the672* read is attempted.673*674* <p> This method works in the same manner as the {@link675* AsynchronousByteChannel#read(ByteBuffer,Object,CompletionHandler)}676* method, except that bytes are read starting at the given file position.677* If the given file position is greater than the file's size at the time678* that the read is attempted then no bytes are read.679*680* @param <A>681* The type of the attachment682* @param dst683* The buffer into which bytes are to be transferred684* @param position685* The file position at which the transfer is to begin;686* must be non-negative687* @param attachment688* The object to attach to the I/O operation; can be {@code null}689* @param handler690* The handler for consuming the result691*692* @throws IllegalArgumentException693* If the position is negative or the buffer is read-only694* @throws NonReadableChannelException695* If this channel was not opened for reading696*/697public abstract <A> void read(ByteBuffer dst,698long position,699A attachment,700CompletionHandler<Integer,? super A> handler);701702/**703* Reads a sequence of bytes from this channel into the given buffer,704* starting at the given file position.705*706* <p> This method initiates the reading of a sequence of bytes from this707* channel into the given buffer, starting at the given file position. This708* method returns a {@code Future} representing the pending result of the709* operation. The {@code Future}'s {@link Future#get() get} method returns710* the number of bytes read or {@code -1} if the given position is greater711* than or equal to the file's size at the time that the read is attempted.712*713* <p> This method works in the same manner as the {@link714* AsynchronousByteChannel#read(ByteBuffer)} method, except that bytes are715* read starting at the given file position. If the given file position is716* greater than the file's size at the time that the read is attempted then717* no bytes are read.718*719* @param dst720* The buffer into which bytes are to be transferred721* @param position722* The file position at which the transfer is to begin;723* must be non-negative724*725* @return A {@code Future} object representing the pending result726*727* @throws IllegalArgumentException728* If the position is negative or the buffer is read-only729* @throws NonReadableChannelException730* If this channel was not opened for reading731*/732public abstract Future<Integer> read(ByteBuffer dst, long position);733734/**735* Writes a sequence of bytes to this channel from the given buffer, starting736* at the given file position.737*738* <p> This method works in the same manner as the {@link739* AsynchronousByteChannel#write(ByteBuffer,Object,CompletionHandler)}740* method, except that bytes are written starting at the given file position.741* If the given position is greater than the file's size, at the time that742* the write is attempted, then the file will be grown to accommodate the new743* bytes; the values of any bytes between the previous end-of-file and the744* newly-written bytes are unspecified.745*746* @param <A>747* The type of the attachment748* @param src749* The buffer from which bytes are to be transferred750* @param position751* The file position at which the transfer is to begin;752* must be non-negative753* @param attachment754* The object to attach to the I/O operation; can be {@code null}755* @param handler756* The handler for consuming the result757*758* @throws IllegalArgumentException759* If the position is negative760* @throws NonWritableChannelException761* If this channel was not opened for writing762*/763public abstract <A> void write(ByteBuffer src,764long position,765A attachment,766CompletionHandler<Integer,? super A> handler);767768/**769* Writes a sequence of bytes to this channel from the given buffer, starting770* at the given file position.771*772* <p> This method initiates the writing of a sequence of bytes to this773* channel from the given buffer, starting at the given file position. The774* method returns a {@code Future} representing the pending result of the775* write operation. The {@code Future}'s {@link Future#get() get} method776* returns the number of bytes written.777*778* <p> This method works in the same manner as the {@link779* AsynchronousByteChannel#write(ByteBuffer)} method, except that bytes are780* written starting at the given file position. If the given position is781* greater than the file's size, at the time that the write is attempted,782* then the file will be grown to accommodate the new bytes; the values of783* any bytes between the previous end-of-file and the newly-written bytes784* are unspecified.785*786* @param src787* The buffer from which bytes are to be transferred788* @param position789* The file position at which the transfer is to begin;790* must be non-negative791*792* @return A {@code Future} object representing the pending result793*794* @throws IllegalArgumentException795* If the position is negative796* @throws NonWritableChannelException797* If this channel was not opened for writing798*/799public abstract Future<Integer> write(ByteBuffer src, long position);800}801802803