Path: blob/master/src/java.base/share/classes/java/nio/channels/ScatteringByteChannel.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 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* {@code 0} {@code <=} <i>n</i> {@code <=} <i>r</i>.69* Up to the first {@code dsts[offset].remaining()} bytes of this sequence70* are transferred into buffer {@code dsts[offset]}, up to the next71* {@code dsts[offset+1].remaining()} bytes are transferred into buffer72* {@code dsts[offset+1]}, 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 {@code dsts.length}90*91* @param length92* The maximum number of buffers to be accessed; must be93* non-negative and no larger than94* {@code dsts.length} - {@code offset}95*96* @return The number of bytes read, possibly zero,97* or {@code -1} if the channel has reached end-of-stream98*99* @throws IndexOutOfBoundsException100* If the preconditions on the {@code offset} and {@code length}101* parameters do not hold102*103* @throws IllegalArgumentException104* If any of the buffers is read-only105*106* @throws NonReadableChannelException107* If this channel was not opened for reading108*109* @throws ClosedChannelException110* If this channel is closed111*112* @throws AsynchronousCloseException113* If another thread closes this channel114* while the read operation is in progress115*116* @throws ClosedByInterruptException117* If another thread interrupts the current thread118* while the read operation is in progress, thereby119* closing the channel and setting the current thread's120* interrupt status121*122* @throws IOException123* If some other I/O error occurs124*/125public long read(ByteBuffer[] dsts, int offset, int length)126throws IOException;127128/**129* Reads a sequence of bytes from this channel into the given buffers.130*131* <p> An invocation of this method of the form {@code c.read(dsts)}132* behaves in exactly the same manner as the invocation133*134* <blockquote><pre>135* c.read(dsts, 0, dsts.length);</pre></blockquote>136*137* @param dsts138* The buffers into which bytes are to be transferred139*140* @return The number of bytes read, possibly zero,141* or {@code -1} if the channel has reached end-of-stream142*143* @throws IllegalArgumentException144* If any of the buffers is read-only145*146* @throws NonReadableChannelException147* If this channel was not opened for reading148*149* @throws ClosedChannelException150* If this channel is closed151*152* @throws AsynchronousCloseException153* If another thread closes this channel154* while the read operation is in progress155*156* @throws ClosedByInterruptException157* If another thread interrupts the current thread158* while the read operation is in progress, thereby159* closing the channel and setting the current thread's160* interrupt status161*162* @throws IOException163* If some other I/O error occurs164*/165public long read(ByteBuffer[] dsts) throws IOException;166167}168169170