001 /*
002 * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.nio.channels;
027
028 import java.io.IOException;
029 import java.nio.ByteBuffer;
030
031 /**
032 * A channel that can write bytes from a sequence of buffers.
033 *
034 * <p> A <i>gathering</i> write operation writes, in a single invocation, a
035 * sequence of bytes from one or more of a given sequence of buffers.
036 * Gathering writes are often useful when implementing network protocols or
037 * file formats that, for example, group data into segments consisting of one
038 * or more fixed-length headers followed by a variable-length body. Similar
039 * <i>scattering</i> read operations are defined in the {@link
040 * ScatteringByteChannel} interface. </p>
041 *
042 *
043 * @author Mark Reinhold
044 * @author JSR-51 Expert Group
045 * @version 1.19, 07/05/05
046 * @since 1.4
047 */
048
049 public interface GatheringByteChannel extends WritableByteChannel {
050
051 /**
052 * Writes a sequence of bytes to this channel from a subsequence of the
053 * given buffers.
054 *
055 * <p> An attempt is made to write up to <i>r</i> bytes to this channel,
056 * where <i>r</i> is the total number of bytes remaining in the specified
057 * subsequence of the given buffer array, that is,
058 *
059 * <blockquote><pre>
060 * srcs[offset].remaining()
061 * + srcs[offset+1].remaining()
062 * + ... + srcs[offset+length-1].remaining()</pre></blockquote>
063 *
064 * at the moment that this method is invoked.
065 *
066 * <p> Suppose that a byte sequence of length <i>n</i> is written, where
067 * <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>.
068 * Up to the first <tt>srcs[offset].remaining()</tt> bytes of this sequence
069 * are written from buffer <tt>srcs[offset]</tt>, up to the next
070 * <tt>srcs[offset+1].remaining()</tt> bytes are written from buffer
071 * <tt>srcs[offset+1]</tt>, and so forth, until the entire byte sequence is
072 * written. As many bytes as possible are written from each buffer, hence
073 * the final position of each updated buffer, except the last updated
074 * buffer, is guaranteed to be equal to that buffer's limit.
075 *
076 * <p> Unless otherwise specified, a write operation will return only after
077 * writing all of the <i>r</i> requested bytes. Some types of channels,
078 * depending upon their state, may write only some of the bytes or possibly
079 * none at all. A socket channel in non-blocking mode, for example, cannot
080 * write any more bytes than are free in the socket's output buffer.
081 *
082 * <p> This method may be invoked at any time. If another thread has
083 * already initiated a write operation upon this channel, however, then an
084 * invocation of this method will block until the first operation is
085 * complete. </p>
086 *
087 * @param srcs
088 * The buffers from which bytes are to be retrieved
089 *
090 * @param offset
091 * The offset within the buffer array of the first buffer from
092 * which bytes are to be retrieved; must be non-negative and no
093 * larger than <tt>srcs.length</tt>
094 *
095 * @param length
096 * The maximum number of buffers to be accessed; must be
097 * non-negative and no larger than
098 * <tt>srcs.length</tt> - <tt>offset</tt>
099 *
100 * @return The number of bytes written, possibly zero
101 *
102 * @throws IndexOutOfBoundsException
103 * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
104 * parameters do not hold
105 *
106 * @throws NonWritableChannelException
107 * If this channel was not opened for writing
108 *
109 * @throws ClosedChannelException
110 * If this channel is closed
111 *
112 * @throws AsynchronousCloseException
113 * If another thread closes this channel
114 * while the write operation is in progress
115 *
116 * @throws ClosedByInterruptException
117 * If another thread interrupts the current thread
118 * while the write operation is in progress, thereby
119 * closing the channel and setting the current thread's
120 * interrupt status
121 *
122 * @throws IOException
123 * If some other I/O error occurs
124 */
125 public long write(ByteBuffer[] srcs, int offset, int length)
126 throws IOException;
127
128 /**
129 * Writes a sequence of bytes to this channel from the given buffers.
130 *
131 * <p> An invocation of this method of the form <tt>c.write(srcs)</tt>
132 * behaves in exactly the same manner as the invocation
133 *
134 * <blockquote><pre>
135 * c.write(srcs, 0, srcs.length);</pre></blockquote>
136 *
137 * @param srcs
138 * The buffers from which bytes are to be retrieved
139 *
140 * @return The number of bytes written, possibly zero
141 *
142 * @throws NonWritableChannelException
143 * If this channel was not opened for writing
144 *
145 * @throws ClosedChannelException
146 * If this channel is closed
147 *
148 * @throws AsynchronousCloseException
149 * If another thread closes this channel
150 * while the write operation is in progress
151 *
152 * @throws ClosedByInterruptException
153 * If another thread interrupts the current thread
154 * while the write operation is in progress, thereby
155 * closing the channel and setting the current thread's
156 * interrupt status
157 *
158 * @throws IOException
159 * If some other I/O error occurs
160 */
161 public long write(ByteBuffer[] srcs) throws IOException;
162
163 }
|