Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/channels/ReadableByteChannel.java
38918 views
/*1* Copyright (c) 2000, 2001, 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* <tt>dst.remaining()</tt>, at the moment this method is invoked.55*56* <p> Suppose that a byte sequence of length <i>n</i> is read, where57* <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <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> <tt>+</tt> <i>n</i> <tt>-</tt> <tt>1</tt>,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> <tt>+</tt> <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 <tt>-1</tt> if the84* channel has reached end-of-stream85*86* @throws NonReadableChannelException87* If this channel was not opened for reading88*89* @throws ClosedChannelException90* If this channel is closed91*92* @throws AsynchronousCloseException93* If another thread closes this channel94* while the read operation is in progress95*96* @throws ClosedByInterruptException97* If another thread interrupts the current thread98* while the read operation is in progress, thereby99* closing the channel and setting the current thread's100* interrupt status101*102* @throws IOException103* If some other I/O error occurs104*/105public int read(ByteBuffer dst) throws IOException;106107}108109110