Path: blob/master/src/java.base/share/classes/java/nio/channels/ReadableByteChannel.java
41159 views
/*1* Copyright (c) 2000, 2019, 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.io.IOException;28import java.nio.ByteBuffer;293031/**32* A channel that can read bytes.33*34* <p> Only one read operation upon a readable channel may be in progress at35* any given time. If one thread initiates a read operation upon a channel36* then any other thread that attempts to initiate another read operation will37* block until the first operation is complete. Whether or not other kinds of38* I/O operations may proceed concurrently with a read operation depends upon39* the type of the channel. </p>40*41*42* @author Mark Reinhold43* @author JSR-51 Expert Group44* @since 1.445*/4647public interface ReadableByteChannel extends Channel {4849/**50* Reads a sequence of bytes from this channel into the given buffer.51*52* <p> An attempt is made to read up to <i>r</i> bytes from the channel,53* where <i>r</i> is the number of bytes remaining in the buffer, that is,54* {@code dst.remaining()}, at the moment this method is invoked.55*56* <p> Suppose that a byte sequence of length <i>n</i> is read, where57* {@code 0} {@code <=} <i>n</i> {@code <=} <i>r</i>.58* This byte sequence will be transferred into the buffer so that the first59* byte in the sequence is at index <i>p</i> and the last byte is at index60* <i>p</i> {@code +} <i>n</i> {@code -} {@code 1},61* where <i>p</i> is the buffer's position at the moment this method is62* invoked. Upon return the buffer's position will be equal to63* <i>p</i> {@code +} <i>n</i>; its limit will not have changed.64*65* <p> A read operation might not fill the buffer, and in fact it might not66* read any bytes at all. Whether or not it does so depends upon the67* nature and state of the channel. A socket channel in non-blocking mode,68* for example, cannot read any more bytes than are immediately available69* from the socket's input buffer; similarly, a file channel cannot read70* any more bytes than remain in the file. It is guaranteed, however, that71* if a channel is in blocking mode and there is at least one byte72* remaining in the buffer then this method will block until at least one73* byte is read.74*75* <p> This method may be invoked at any time. If another thread has76* already initiated a read operation upon this channel, however, then an77* invocation of this method will block until the first operation is78* complete. </p>79*80* @param dst81* The buffer into which bytes are to be transferred82*83* @return The number of bytes read, possibly zero, or {@code -1} if the84* channel has reached end-of-stream85*86* @throws IllegalArgumentException87* If the buffer is read-only88*89* @throws NonReadableChannelException90* If this channel was not opened for reading91*92* @throws ClosedChannelException93* If this channel is closed94*95* @throws AsynchronousCloseException96* If another thread closes this channel97* while the read operation is in progress98*99* @throws ClosedByInterruptException100* If another thread interrupts the current thread101* while the read operation is in progress, thereby102* closing the channel and setting the current thread's103* interrupt status104*105* @throws IOException106* If some other I/O error occurs107*/108public int read(ByteBuffer dst) throws IOException;109110}111112113