Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/nio/channels/ScatteringByteChannel.java
38918 views
/*1* Copyright (c) 2000, 2006, 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 into a sequence of buffers.33*34* <p> A <i>scattering</i> read operation reads, in a single invocation, a35* sequence of bytes into one or more of a given sequence of buffers.36* Scattering reads are often useful when implementing network protocols or37* file formats that, for example, group data into segments consisting of one38* or more fixed-length headers followed by a variable-length body. Similar39* <i>gathering</i> write operations are defined in the {@link40* GatheringByteChannel} interface. </p>41*42*43* @author Mark Reinhold44* @author JSR-51 Expert Group45* @since 1.446*/4748public interface ScatteringByteChannel49extends ReadableByteChannel50{5152/**53* Reads a sequence of bytes from this channel into a subsequence of the54* given buffers.55*56* <p> An invocation of this method attempts to read up to <i>r</i> bytes57* from this channel, where <i>r</i> is the total number of bytes remaining58* the specified subsequence of the given buffer array, that is,59*60* <blockquote><pre>61* dsts[offset].remaining()62* + dsts[offset+1].remaining()63* + ... + dsts[offset+length-1].remaining()</pre></blockquote>64*65* at the moment that this method is invoked.66*67* <p> Suppose that a byte sequence of length <i>n</i> is read, where68* <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>.69* Up to the first <tt>dsts[offset].remaining()</tt> bytes of this sequence70* are transferred into buffer <tt>dsts[offset]</tt>, up to the next71* <tt>dsts[offset+1].remaining()</tt> bytes are transferred into buffer72* <tt>dsts[offset+1]</tt>, and so forth, until the entire byte sequence73* is transferred into the given buffers. As many bytes as possible are74* transferred into each buffer, hence the final position of each updated75* buffer, except the last updated buffer, is guaranteed to be equal to76* that buffer's limit.77*78* <p> This method may be invoked at any time. If another thread has79* already initiated a read operation upon this channel, however, then an80* invocation of this method will block until the first operation is81* complete. </p>82*83* @param dsts84* The buffers into which bytes are to be transferred85*86* @param offset87* The offset within the buffer array of the first buffer into88* which bytes are to be transferred; must be non-negative and no89* larger than <tt>dsts.length</tt>90*91* @param length92* The maximum number of buffers to be accessed; must be93* non-negative and no larger than94* <tt>dsts.length</tt> - <tt>offset</tt>95*96* @return The number of bytes read, possibly zero,97* or <tt>-1</tt> if the channel has reached end-of-stream98*99* @throws IndexOutOfBoundsException100* If the preconditions on the <tt>offset</tt> and <tt>length</tt>101* parameters do not hold102*103* @throws NonReadableChannelException104* If this channel was not opened for reading105*106* @throws ClosedChannelException107* If this channel is closed108*109* @throws AsynchronousCloseException110* If another thread closes this channel111* while the read operation is in progress112*113* @throws ClosedByInterruptException114* If another thread interrupts the current thread115* while the read operation is in progress, thereby116* closing the channel and setting the current thread's117* interrupt status118*119* @throws IOException120* If some other I/O error occurs121*/122public long read(ByteBuffer[] dsts, int offset, int length)123throws IOException;124125/**126* Reads a sequence of bytes from this channel into the given buffers.127*128* <p> An invocation of this method of the form <tt>c.read(dsts)</tt>129* behaves in exactly the same manner as the invocation130*131* <blockquote><pre>132* c.read(dsts, 0, dsts.length);</pre></blockquote>133*134* @param dsts135* The buffers into which bytes are to be transferred136*137* @return The number of bytes read, possibly zero,138* or <tt>-1</tt> if the channel has reached end-of-stream139*140* @throws NonReadableChannelException141* If this channel was not opened for reading142*143* @throws ClosedChannelException144* If this channel is closed145*146* @throws AsynchronousCloseException147* If another thread closes this channel148* while the read operation is in progress149*150* @throws ClosedByInterruptException151* If another thread interrupts the current thread152* while the read operation is in progress, thereby153* closing the channel and setting the current thread's154* interrupt status155*156* @throws IOException157* If some other I/O error occurs158*/159public long read(ByteBuffer[] dsts) throws IOException;160161}162163164