Path: blob/master/src/java.base/share/classes/java/nio/channels/GatheringByteChannel.java
41159 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 write bytes from a sequence of buffers.33*34* <p> A <i>gathering</i> write operation writes, in a single invocation, a35* sequence of bytes from one or more of a given sequence of buffers.36* Gathering writes 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>scattering</i> read operations are defined in the {@link40* ScatteringByteChannel} interface. </p>41*42*43* @author Mark Reinhold44* @author JSR-51 Expert Group45* @since 1.446*/4748public interface GatheringByteChannel49extends WritableByteChannel50{5152/**53* Writes a sequence of bytes to this channel from a subsequence of the54* given buffers.55*56* <p> An attempt is made to write up to <i>r</i> bytes to this channel,57* where <i>r</i> is the total number of bytes remaining in the specified58* subsequence of the given buffer array, that is,59*60* <blockquote><pre>61* srcs[offset].remaining()62* + srcs[offset+1].remaining()63* + ... + srcs[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 written, where68* {@code 0} {@code <=} <i>n</i> {@code <=} <i>r</i>.69* Up to the first {@code srcs[offset].remaining()} bytes of this sequence70* are written from buffer {@code srcs[offset]}, up to the next71* {@code srcs[offset+1].remaining()} bytes are written from buffer72* {@code srcs[offset+1]}, and so forth, until the entire byte sequence is73* written. As many bytes as possible are written from each buffer, hence74* the final position of each updated buffer, except the last updated75* buffer, is guaranteed to be equal to that buffer's limit.76*77* <p> Unless otherwise specified, a write operation will return only after78* writing all of the <i>r</i> requested bytes. Some types of channels,79* depending upon their state, may write only some of the bytes or possibly80* none at all. A socket channel in non-blocking mode, for example, cannot81* write any more bytes than are free in the socket's output buffer.82*83* <p> This method may be invoked at any time. If another thread has84* already initiated a write operation upon this channel, however, then an85* invocation of this method will block until the first operation is86* complete. </p>87*88* @param srcs89* The buffers from which bytes are to be retrieved90*91* @param offset92* The offset within the buffer array of the first buffer from93* which bytes are to be retrieved; must be non-negative and no94* larger than {@code srcs.length}95*96* @param length97* The maximum number of buffers to be accessed; must be98* non-negative and no larger than99* {@code srcs.length} - {@code offset}100*101* @return The number of bytes written, possibly zero102*103* @throws IndexOutOfBoundsException104* If the preconditions on the {@code offset} and {@code length}105* parameters do not hold106*107* @throws NonWritableChannelException108* If this channel was not opened for writing109*110* @throws ClosedChannelException111* If this channel is closed112*113* @throws AsynchronousCloseException114* If another thread closes this channel115* while the write operation is in progress116*117* @throws ClosedByInterruptException118* If another thread interrupts the current thread119* while the write operation is in progress, thereby120* closing the channel and setting the current thread's121* interrupt status122*123* @throws IOException124* If some other I/O error occurs125*/126public long write(ByteBuffer[] srcs, int offset, int length)127throws IOException;128129130/**131* Writes a sequence of bytes to this channel from the given buffers.132*133* <p> An invocation of this method of the form {@code c.write(srcs)}134* behaves in exactly the same manner as the invocation135*136* <blockquote><pre>137* c.write(srcs, 0, srcs.length);</pre></blockquote>138*139* @param srcs140* The buffers from which bytes are to be retrieved141*142* @return The number of bytes written, possibly zero143*144* @throws NonWritableChannelException145* If this channel was not opened for writing146*147* @throws ClosedChannelException148* If this channel is closed149*150* @throws AsynchronousCloseException151* If another thread closes this channel152* while the write operation is in progress153*154* @throws ClosedByInterruptException155* If another thread interrupts the current thread156* while the write operation is in progress, thereby157* closing the channel and setting the current thread's158* interrupt status159*160* @throws IOException161* If some other I/O error occurs162*/163public long write(ByteBuffer[] srcs) throws IOException;164165}166167168