Path: blob/master/src/java.base/share/classes/java/nio/channels/AsynchronousByteChannel.java
41159 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.ByteBuffer;28import java.util.concurrent.Future;2930/**31* An asynchronous channel that can read and write bytes.32*33* <p> Some channels may not allow more than one read or write to be outstanding34* at any given time. If a thread invokes a read method before a previous read35* operation has completed then a {@link ReadPendingException} will be thrown.36* Similarly, if a write method is invoked before a previous write has completed37* then {@link WritePendingException} is thrown. Whether or not other kinds of38* I/O operations may proceed concurrently with a read operation depends upon39* the type of the channel.40*41* <p> Note that {@link java.nio.ByteBuffer ByteBuffers} are not safe for use by42* multiple concurrent threads. When a read or write operation is initiated then43* care must be taken to ensure that the buffer is not accessed until the44* operation completes.45*46* @see Channels#newInputStream(AsynchronousByteChannel)47* @see Channels#newOutputStream(AsynchronousByteChannel)48*49* @since 1.750*/5152public interface AsynchronousByteChannel53extends AsynchronousChannel54{55/**56* Reads a sequence of bytes from this channel into the given buffer.57*58* <p> This method initiates an asynchronous read operation to read a59* sequence of bytes from this channel into the given buffer. The {@code60* handler} parameter is a completion handler that is invoked when the read61* operation completes (or fails). The result passed to the completion62* handler is the number of bytes read or {@code -1} if no bytes could be63* read because the channel has reached end-of-stream.64*65* <p> The read operation may read up to <i>r</i> bytes from the channel,66* where <i>r</i> is the number of bytes remaining in the buffer, that is,67* {@code dst.remaining()} at the time that the read is attempted. Where68* <i>r</i> is 0, the read operation completes immediately with a result of69* {@code 0} without initiating an I/O operation.70*71* <p> Suppose that a byte sequence of length <i>n</i> is read, where72* {@code 0} {@code <} <i>n</i> {@code <=} <i>r</i>.73* This byte sequence will be transferred into the buffer so that the first74* byte in the sequence is at index <i>p</i> and the last byte is at index75* <i>p</i> {@code +} <i>n</i> {@code -} {@code 1},76* where <i>p</i> is the buffer's position at the moment the read is77* performed. Upon completion the buffer's position will be equal to78* <i>p</i> {@code +} <i>n</i>; its limit will not have changed.79*80* <p> Buffers are not safe for use by multiple concurrent threads so care81* should be taken to not access the buffer until the operation has82* completed.83*84* <p> This method may be invoked at any time. Some channel types may not85* allow more than one read to be outstanding at any given time. If a thread86* initiates a read operation before a previous read operation has87* completed then a {@link ReadPendingException} will be thrown.88*89* @param <A>90* The type of the attachment91* @param dst92* The buffer into which bytes are to be transferred93* @param attachment94* The object to attach to the I/O operation; can be {@code null}95* @param handler96* The completion handler97*98* @throws IllegalArgumentException99* If the buffer is read-only100* @throws ReadPendingException101* If the channel does not allow more than one read to be outstanding102* and a previous read has not completed103* @throws ShutdownChannelGroupException104* If the channel is associated with a {@link AsynchronousChannelGroup105* group} that has terminated106*/107<A> void read(ByteBuffer dst,108A attachment,109CompletionHandler<Integer,? super A> handler);110111/**112* Reads a sequence of bytes from this channel into the given buffer.113*114* <p> This method initiates an asynchronous read operation to read a115* sequence of bytes from this channel into the given buffer. The method116* behaves in exactly the same manner as the {@link117* #read(ByteBuffer,Object,CompletionHandler)118* read(ByteBuffer,Object,CompletionHandler)} method except that instead119* of specifying a completion handler, this method returns a {@code Future}120* representing the pending result. The {@code Future}'s {@link Future#get()121* get} method returns the number of bytes read or {@code -1} if no bytes122* could be read because the channel has reached end-of-stream.123*124* @param dst125* The buffer into which bytes are to be transferred126*127* @return A Future representing the result of the operation128*129* @throws IllegalArgumentException130* If the buffer is read-only131* @throws ReadPendingException132* If the channel does not allow more than one read to be outstanding133* and a previous read has not completed134*/135Future<Integer> read(ByteBuffer dst);136137/**138* Writes a sequence of bytes to this channel from the given buffer.139*140* <p> This method initiates an asynchronous write operation to write a141* sequence of bytes to this channel from the given buffer. The {@code142* handler} parameter is a completion handler that is invoked when the write143* operation completes (or fails). The result passed to the completion144* handler is the number of bytes written.145*146* <p> The write operation may write up to <i>r</i> bytes to the channel,147* where <i>r</i> is the number of bytes remaining in the buffer, that is,148* {@code src.remaining()} at the time that the write is attempted. Where149* <i>r</i> is 0, the write operation completes immediately with a result of150* {@code 0} without initiating an I/O operation.151*152* <p> Suppose that a byte sequence of length <i>n</i> is written, where153* {@code 0} {@code <} <i>n</i> {@code <=} <i>r</i>.154* This byte sequence will be transferred from the buffer starting at index155* <i>p</i>, where <i>p</i> is the buffer's position at the moment the156* write is performed; the index of the last byte written will be157* <i>p</i> {@code +} <i>n</i> {@code -} {@code 1}.158* Upon completion the buffer's position will be equal to159* <i>p</i> {@code +} <i>n</i>; its limit will not have changed.160*161* <p> Buffers are not safe for use by multiple concurrent threads so care162* should be taken to not access the buffer until the operation has163* completed.164*165* <p> This method may be invoked at any time. Some channel types may not166* allow more than one write to be outstanding at any given time. If a thread167* initiates a write operation before a previous write operation has168* completed then a {@link WritePendingException} will be thrown.169*170* @param <A>171* The type of the attachment172* @param src173* The buffer from which bytes are to be retrieved174* @param attachment175* The object to attach to the I/O operation; can be {@code null}176* @param handler177* The completion handler object178*179* @throws WritePendingException180* If the channel does not allow more than one write to be outstanding181* and a previous write has not completed182* @throws ShutdownChannelGroupException183* If the channel is associated with a {@link AsynchronousChannelGroup184* group} that has terminated185*/186<A> void write(ByteBuffer src,187A attachment,188CompletionHandler<Integer,? super A> handler);189190/**191* Writes a sequence of bytes to this channel from the given buffer.192*193* <p> This method initiates an asynchronous write operation to write a194* sequence of bytes to this channel from the given buffer. The method195* behaves in exactly the same manner as the {@link196* #write(ByteBuffer,Object,CompletionHandler)197* write(ByteBuffer,Object,CompletionHandler)} method except that instead198* of specifying a completion handler, this method returns a {@code Future}199* representing the pending result. The {@code Future}'s {@link Future#get()200* get} method returns the number of bytes written.201*202* @param src203* The buffer from which bytes are to be retrieved204*205* @return A Future representing the result of the operation206*207* @throws WritePendingException208* If the channel does not allow more than one write to be outstanding209* and a previous write has not completed210*/211Future<Integer> write(ByteBuffer src);212}213214215