Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/channels/AsynchronousFileChannel.java
38918 views
/*1* Copyright (c) 2007, 2013, 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 border=1 cellpadding=5 summary="">136* <tr> <th>Option</th> <th>Description</th> </tr>137* <tr>138* <td> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </td>139* <td> When opening an existing file, the file is first truncated to a140* size of 0 bytes. This option is ignored when the file is opened only141* for reading.</td>142* </tr>143* <tr>144* <td> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </td>145* <td> If this option is present then a new file is created, failing if146* the file already exists. When creating a file the check for the147* existence of the file and the creation of the file if it does not exist148* is atomic with respect to other file system operations. This option is149* ignored when the file is opened only for reading. </td>150* </tr>151* <tr>152* <td > {@link StandardOpenOption#CREATE CREATE} </td>153* <td> If this option is present then an existing file is opened if it154* exists, otherwise a new file is created. When creating a file the check155* for the existence of the file and the creation of the file if it does156* not exist is atomic with respect to other file system operations. This157* option is ignored if the {@code CREATE_NEW} option is also present or158* the file is opened only for reading. </td>159* </tr>160* <tr>161* <td > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </td>162* <td> When this option is present then the implementation makes a163* <em>best effort</em> attempt to delete the file when closed by the164* the {@link #close close} method. If the {@code close} method is not165* invoked then a <em>best effort</em> attempt is made to delete the file166* when the Java virtual machine terminates. </td>167* </tr>168* <tr>169* <td>{@link StandardOpenOption#SPARSE SPARSE} </td>170* <td> When creating a new file this option is a <em>hint</em> that the171* new file will be sparse. This option is ignored when not creating172* a new file. </td>173* </tr>174* <tr>175* <td> {@link StandardOpenOption#SYNC SYNC} </td>176* <td> Requires that every update to the file's content or metadata be177* written synchronously to the underlying storage device. (see <a178* href="../file/package-summary.html#integrity"> Synchronized I/O file179* integrity</a>). </td>180* </tr>181* <tr>182* <td> {@link StandardOpenOption#DSYNC DSYNC} </td>183* <td> Requires that every update to the file's content be written184* synchronously to the underlying storage device. (see <a185* href="../file/package-summary.html#integrity"> Synchronized I/O file186* integrity</a>). </td>187* </tr>188* </table>189*190* <p> An implementation may also support additional options.191*192* <p> The {@code executor} parameter is the {@link ExecutorService} to193* which tasks are submitted to handle I/O events and dispatch completion194* results for operations initiated on resulting channel.195* The nature of these tasks is highly implementation specific and so care196* should be taken when configuring the {@code Executor}. Minimally it197* should support an unbounded work queue and should not run tasks on the198* caller thread of the {@link ExecutorService#execute execute} method.199* Shutting down the executor service while the channel is open results in200* unspecified behavior.201*202* <p> The {@code attrs} parameter is an optional array of file {@link203* FileAttribute file-attributes} to set atomically when creating the file.204*205* <p> The new channel is created by invoking the {@link206* FileSystemProvider#newFileChannel newFileChannel} method on the207* provider that created the {@code Path}.208*209* @param file210* The path of the file to open or create211* @param options212* Options specifying how the file is opened213* @param executor214* The thread pool or {@code null} to associate the channel with215* the default thread pool216* @param attrs217* An optional list of file attributes to set atomically when218* creating the file219*220* @return A new asynchronous file channel221*222* @throws IllegalArgumentException223* If the set contains an invalid combination of options224* @throws UnsupportedOperationException225* If the {@code file} is associated with a provider that does not226* support creating asynchronous file channels, or an unsupported227* open option is specified, or the array contains an attribute that228* cannot be set atomically when creating the file229* @throws IOException230* If an I/O error occurs231* @throws SecurityException232* If a security manager is installed and it denies an233* unspecified permission required by the implementation.234* In the case of the default provider, the {@link235* SecurityManager#checkRead(String)} method is invoked to check236* read access if the file is opened for reading. The {@link237* SecurityManager#checkWrite(String)} method is invoked to check238* write access if the file is opened for writing239*/240public static AsynchronousFileChannel open(Path file,241Set<? extends OpenOption> options,242ExecutorService executor,243FileAttribute<?>... attrs)244throws IOException245{246FileSystemProvider provider = file.getFileSystem().provider();247return provider.newAsynchronousFileChannel(file, options, executor, attrs);248}249250@SuppressWarnings({"unchecked", "rawtypes"}) // generic array construction251private static final FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0];252253/**254* Opens or creates a file for reading and/or writing, returning an255* asynchronous file channel to access the file.256*257* <p> An invocation of this method behaves in exactly the same way as the258* invocation259* <pre>260* ch.{@link #open(Path,Set,ExecutorService,FileAttribute[])261* open}(file, opts, null, new FileAttribute<?>[0]);262* </pre>263* where {@code opts} is a {@code Set} containing the options specified to264* this method.265*266* <p> The resulting channel is associated with default thread pool to which267* tasks are submitted to handle I/O events and dispatch to completion268* handlers that consume the result of asynchronous operations performed on269* the resulting channel.270*271* @param file272* The path of the file to open or create273* @param options274* Options specifying how the file is opened275*276* @return A new asynchronous file channel277*278* @throws IllegalArgumentException279* If the set contains an invalid combination of options280* @throws UnsupportedOperationException281* If the {@code file} is associated with a provider that does not282* support creating file channels, or an unsupported open option is283* specified284* @throws IOException285* If an I/O error occurs286* @throws SecurityException287* If a security manager is installed and it denies an288* unspecified permission required by the implementation.289* In the case of the default provider, the {@link290* SecurityManager#checkRead(String)} method is invoked to check291* read access if the file is opened for reading. The {@link292* SecurityManager#checkWrite(String)} method is invoked to check293* write access if the file is opened for writing294*/295public static AsynchronousFileChannel open(Path file, OpenOption... options)296throws IOException297{298Set<OpenOption> set = new HashSet<OpenOption>(options.length);299Collections.addAll(set, options);300return open(file, set, null, NO_ATTRIBUTES);301}302303/**304* Returns the current size of this channel's file.305*306* @return The current size of this channel's file, measured in bytes307*308* @throws ClosedChannelException309* If this channel is closed310* @throws IOException311* If some other I/O error occurs312*/313public abstract long size() throws IOException;314315/**316* Truncates this channel's file to the given size.317*318* <p> If the given size is less than the file's current size then the file319* is truncated, discarding any bytes beyond the new end of the file. If320* the given size is greater than or equal to the file's current size then321* the file is not modified. </p>322*323* @param size324* The new size, a non-negative byte count325*326* @return This file channel327*328* @throws NonWritableChannelException329* If this channel was not opened for writing330*331* @throws ClosedChannelException332* If this channel is closed333*334* @throws IllegalArgumentException335* If the new size is negative336*337* @throws IOException338* If some other I/O error occurs339*/340public abstract AsynchronousFileChannel truncate(long size) throws IOException;341342/**343* Forces any updates to this channel's file to be written to the storage344* device that contains it.345*346* <p> If this channel's file resides on a local storage device then when347* this method returns it is guaranteed that all changes made to the file348* since this channel was created, or since this method was last invoked,349* will have been written to that device. This is useful for ensuring that350* critical information is not lost in the event of a system crash.351*352* <p> If the file does not reside on a local device then no such guarantee353* is made.354*355* <p> The {@code metaData} parameter can be used to limit the number of356* I/O operations that this method is required to perform. Passing357* {@code false} for this parameter indicates that only updates to the358* file's content need be written to storage; passing {@code true}359* indicates that updates to both the file's content and metadata must be360* written, which generally requires at least one more I/O operation.361* Whether this parameter actually has any effect is dependent upon the362* underlying operating system and is therefore unspecified.363*364* <p> Invoking this method may cause an I/O operation to occur even if the365* channel was only opened for reading. Some operating systems, for366* example, maintain a last-access time as part of a file's metadata, and367* this time is updated whenever the file is read. Whether or not this is368* actually done is system-dependent and is therefore unspecified.369*370* <p> This method is only guaranteed to force changes that were made to371* this channel's file via the methods defined in this class.372*373* @param metaData374* If {@code true} then this method is required to force changes375* to both the file's content and metadata to be written to376* storage; otherwise, it need only force content changes to be377* written378*379* @throws ClosedChannelException380* If this channel is closed381*382* @throws IOException383* If some other I/O error occurs384*/385public abstract void force(boolean metaData) throws IOException;386387/**388* Acquires a lock on the given region of this channel's file.389*390* <p> This method initiates an operation to acquire a lock on the given391* region of this channel's file. The {@code handler} parameter is a392* completion handler that is invoked when the lock is acquired (or the393* operation fails). The result passed to the completion handler is the394* resulting {@code FileLock}.395*396* <p> The region specified by the {@code position} and {@code size}397* parameters need not be contained within, or even overlap, the actual398* underlying file. Lock regions are fixed in size; if a locked region399* initially contains the end of the file and the file grows beyond the400* region then the new portion of the file will not be covered by the lock.401* If a file is expected to grow in size and a lock on the entire file is402* required then a region starting at zero, and no smaller than the403* expected maximum size of the file, should be locked. The two-argument404* {@link #lock(Object,CompletionHandler)} method simply locks a region405* of size {@link Long#MAX_VALUE}. If a lock that overlaps the requested406* region is already held by this Java virtual machine, or this method has407* been invoked to lock an overlapping region and that operation has not408* completed, then this method throws {@link OverlappingFileLockException}.409*410* <p> Some operating systems do not support a mechanism to acquire a file411* lock in an asynchronous manner. Consequently an implementation may412* acquire the file lock in a background thread or from a task executed by413* a thread in the associated thread pool. If there are many lock operations414* outstanding then it may consume threads in the Java virtual machine for415* indefinite periods.416*417* <p> Some operating systems do not support shared locks, in which case a418* request for a shared lock is automatically converted into a request for419* an exclusive lock. Whether the newly-acquired lock is shared or420* exclusive may be tested by invoking the resulting lock object's {@link421* FileLock#isShared() isShared} method.422*423* <p> File locks are held on behalf of the entire Java virtual machine.424* They are not suitable for controlling access to a file by multiple425* threads within the same virtual machine.426*427* @param <A>428* The type of the attachment429* @param position430* The position at which the locked region is to start; must be431* non-negative432* @param size433* The size of the locked region; must be non-negative, and the sum434* {@code position} + {@code size} must be non-negative435* @param shared436* {@code true} to request a shared lock, in which case this437* channel must be open for reading (and possibly writing);438* {@code false} to request an exclusive lock, in which case this439* channel must be open for writing (and possibly reading)440* @param attachment441* The object to attach to the I/O operation; can be {@code null}442* @param handler443* The handler for consuming the result444*445* @throws OverlappingFileLockException446* If a lock that overlaps the requested region is already held by447* this Java virtual machine, or there is already a pending attempt448* to lock an overlapping region449* @throws IllegalArgumentException450* If the preconditions on the parameters do not hold451* @throws NonReadableChannelException452* If {@code shared} is true but this channel was not opened for reading453* @throws NonWritableChannelException454* If {@code shared} is false but this channel was not opened for writing455*/456public abstract <A> void lock(long position,457long size,458boolean shared,459A attachment,460CompletionHandler<FileLock,? super A> handler);461462/**463* Acquires an exclusive lock on this channel's file.464*465* <p> This method initiates an operation to acquire a lock on the given466* region of this channel's file. The {@code handler} parameter is a467* completion handler that is invoked when the lock is acquired (or the468* operation fails). The result passed to the completion handler is the469* resulting {@code FileLock}.470*471* <p> An invocation of this method of the form {@code ch.lock(att,handler)}472* behaves in exactly the same way as the invocation473* <pre>474* ch.{@link #lock(long,long,boolean,Object,CompletionHandler) lock}(0L, Long.MAX_VALUE, false, att, handler)475* </pre>476*477* @param <A>478* The type of the attachment479* @param attachment480* The object to attach to the I/O operation; can be {@code null}481* @param handler482* The handler for consuming the result483*484* @throws OverlappingFileLockException485* If a lock is already held by this Java virtual machine, or there486* is already a pending attempt to lock a region487* @throws NonWritableChannelException488* If this channel was not opened for writing489*/490public final <A> void lock(A attachment,491CompletionHandler<FileLock,? super A> handler)492{493lock(0L, Long.MAX_VALUE, false, attachment, handler);494}495496/**497* Acquires a lock on the given region of this channel's file.498*499* <p> This method initiates an operation to acquire a lock on the given500* region of this channel's file. The method behaves in exactly the same501* manner as the {@link #lock(long, long, boolean, Object, CompletionHandler)}502* method except that instead of specifying a completion handler, this503* method returns a {@code Future} representing the pending result. The504* {@code Future}'s {@link Future#get() get} method returns the {@link505* FileLock} on successful completion.506*507* @param position508* The position at which the locked region is to start; must be509* non-negative510* @param size511* The size of the locked region; must be non-negative, and the sum512* {@code position} + {@code size} must be non-negative513* @param shared514* {@code true} to request a shared lock, in which case this515* channel must be open for reading (and possibly writing);516* {@code false} to request an exclusive lock, in which case this517* channel must be open for writing (and possibly reading)518*519* @return a {@code Future} object representing the pending result520*521* @throws OverlappingFileLockException522* If a lock is already held by this Java virtual machine, or there523* is already a pending attempt to lock a region524* @throws IllegalArgumentException525* If the preconditions on the parameters do not hold526* @throws NonReadableChannelException527* If {@code shared} is true but this channel was not opened for reading528* @throws NonWritableChannelException529* If {@code shared} is false but this channel was not opened for writing530*/531public abstract Future<FileLock> lock(long position, long size, boolean shared);532533/**534* Acquires an exclusive lock on this channel's file.535*536* <p> This method initiates an operation to acquire an exclusive lock on this537* channel's file. The method returns a {@code Future} representing the538* pending result of the operation. The {@code Future}'s {@link Future#get()539* get} method returns the {@link FileLock} on successful completion.540*541* <p> An invocation of this method behaves in exactly the same way as the542* invocation543* <pre>544* ch.{@link #lock(long,long,boolean) lock}(0L, Long.MAX_VALUE, false)545* </pre>546*547* @return a {@code Future} object representing the pending result548*549* @throws OverlappingFileLockException550* If a lock is already held by this Java virtual machine, or there551* is already a pending attempt to lock a region552* @throws NonWritableChannelException553* If this channel was not opened for writing554*/555public final Future<FileLock> lock() {556return lock(0L, Long.MAX_VALUE, false);557}558559/**560* Attempts to acquire a lock on the given region of this channel's file.561*562* <p> This method does not block. An invocation always returns immediately,563* either having acquired a lock on the requested region or having failed to564* do so. If it fails to acquire a lock because an overlapping lock is held565* by another program then it returns {@code null}. If it fails to acquire566* a lock for any other reason then an appropriate exception is thrown.567*568* @param position569* The position at which the locked region is to start; must be570* non-negative571*572* @param size573* The size of the locked region; must be non-negative, and the sum574* {@code position} + {@code size} must be non-negative575*576* @param shared577* {@code true} to request a shared lock,578* {@code false} to request an exclusive lock579*580* @return A lock object representing the newly-acquired lock,581* or {@code null} if the lock could not be acquired582* because another program holds an overlapping lock583*584* @throws IllegalArgumentException585* If the preconditions on the parameters do not hold586* @throws ClosedChannelException587* If this channel is closed588* @throws OverlappingFileLockException589* If a lock that overlaps the requested region is already held by590* this Java virtual machine, or if another thread is already591* blocked in this method and is attempting to lock an overlapping592* region of the same file593* @throws NonReadableChannelException594* If {@code shared} is true but this channel was not opened for reading595* @throws NonWritableChannelException596* If {@code shared} is false but this channel was not opened for writing597*598* @throws IOException599* If some other I/O error occurs600*601* @see #lock(Object,CompletionHandler)602* @see #lock(long,long,boolean,Object,CompletionHandler)603* @see #tryLock()604*/605public abstract FileLock tryLock(long position, long size, boolean shared)606throws IOException;607608/**609* Attempts to acquire an exclusive lock on this channel's file.610*611* <p> An invocation of this method of the form {@code ch.tryLock()}612* behaves in exactly the same way as the invocation613*614* <pre>615* ch.{@link #tryLock(long,long,boolean) tryLock}(0L, Long.MAX_VALUE, false) </pre>616*617* @return A lock object representing the newly-acquired lock,618* or {@code null} if the lock could not be acquired619* because another program holds an overlapping lock620*621* @throws ClosedChannelException622* If this channel is closed623* @throws OverlappingFileLockException624* If a lock that overlaps the requested region is already held by625* this Java virtual machine, or if another thread is already626* blocked in this method and is attempting to lock an overlapping627* region628* @throws NonWritableChannelException629* If {@code shared} is false but this channel was not opened for writing630*631* @throws IOException632* If some other I/O error occurs633*634* @see #lock(Object,CompletionHandler)635* @see #lock(long,long,boolean,Object,CompletionHandler)636* @see #tryLock(long,long,boolean)637*/638public final FileLock tryLock() throws IOException {639return tryLock(0L, Long.MAX_VALUE, false);640}641642/**643* Reads a sequence of bytes from this channel into the given buffer,644* starting at the given file position.645*646* <p> This method initiates the reading of a sequence of bytes from this647* channel into the given buffer, starting at the given file position. The648* result of the read is the number of bytes read or {@code -1} if the given649* position is greater than or equal to the file's size at the time that the650* read is attempted.651*652* <p> This method works in the same manner as the {@link653* AsynchronousByteChannel#read(ByteBuffer,Object,CompletionHandler)}654* method, except that bytes are read starting at the given file position.655* If the given file position is greater than the file's size at the time656* that the read is attempted then no bytes are read.657*658* @param <A>659* The type of the attachment660* @param dst661* The buffer into which bytes are to be transferred662* @param position663* The file position at which the transfer is to begin;664* must be non-negative665* @param attachment666* The object to attach to the I/O operation; can be {@code null}667* @param handler668* The handler for consuming the result669*670* @throws IllegalArgumentException671* If the position is negative or the buffer is read-only672* @throws NonReadableChannelException673* If this channel was not opened for reading674*/675public abstract <A> void read(ByteBuffer dst,676long position,677A attachment,678CompletionHandler<Integer,? super A> handler);679680/**681* Reads a sequence of bytes from this channel into the given buffer,682* starting at the given file position.683*684* <p> This method initiates the reading of a sequence of bytes from this685* channel into the given buffer, starting at the given file position. This686* method returns a {@code Future} representing the pending result of the687* operation. The {@code Future}'s {@link Future#get() get} method returns688* the number of bytes read or {@code -1} if the given position is greater689* than or equal to the file's size at the time that the read is attempted.690*691* <p> This method works in the same manner as the {@link692* AsynchronousByteChannel#read(ByteBuffer)} method, except that bytes are693* read starting at the given file position. If the given file position is694* greater than the file's size at the time that the read is attempted then695* no bytes are read.696*697* @param dst698* The buffer into which bytes are to be transferred699* @param position700* The file position at which the transfer is to begin;701* must be non-negative702*703* @return A {@code Future} object representing the pending result704*705* @throws IllegalArgumentException706* If the position is negative or the buffer is read-only707* @throws NonReadableChannelException708* If this channel was not opened for reading709*/710public abstract Future<Integer> read(ByteBuffer dst, long position);711712/**713* Writes a sequence of bytes to this channel from the given buffer, starting714* at the given file position.715*716* <p> This method works in the same manner as the {@link717* AsynchronousByteChannel#write(ByteBuffer,Object,CompletionHandler)}718* method, except that bytes are written starting at the given file position.719* If the given position is greater than the file's size, at the time that720* the write is attempted, then the file will be grown to accommodate the new721* bytes; the values of any bytes between the previous end-of-file and the722* newly-written bytes are unspecified.723*724* @param <A>725* The type of the attachment726* @param src727* The buffer from which bytes are to be transferred728* @param position729* The file position at which the transfer is to begin;730* must be non-negative731* @param attachment732* The object to attach to the I/O operation; can be {@code null}733* @param handler734* The handler for consuming the result735*736* @throws IllegalArgumentException737* If the position is negative738* @throws NonWritableChannelException739* If this channel was not opened for writing740*/741public abstract <A> void write(ByteBuffer src,742long position,743A attachment,744CompletionHandler<Integer,? super A> handler);745746/**747* Writes a sequence of bytes to this channel from the given buffer, starting748* at the given file position.749*750* <p> This method initiates the writing of a sequence of bytes to this751* channel from the given buffer, starting at the given file position. The752* method returns a {@code Future} representing the pending result of the753* write operation. The {@code Future}'s {@link Future#get() get} method754* returns the number of bytes written.755*756* <p> This method works in the same manner as the {@link757* AsynchronousByteChannel#write(ByteBuffer)} method, except that bytes are758* written starting at the given file position. If the given position is759* greater than the file's size, at the time that the write is attempted,760* then the file will be grown to accommodate the new bytes; the values of761* any bytes between the previous end-of-file and the newly-written bytes762* are unspecified.763*764* @param src765* The buffer from which bytes are to be transferred766* @param position767* The file position at which the transfer is to begin;768* must be non-negative769*770* @return A {@code Future} object representing the pending result771*772* @throws IllegalArgumentException773* If the position is negative774* @throws NonWritableChannelException775* If this channel was not opened for writing776*/777public abstract Future<Integer> write(ByteBuffer src, long position);778}779780781