J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package java.nio.channels; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | import java.nio.ByteBuffer; |
| 30 | |
| 31 | |
| 32 | /** |
| 33 | * A channel that can write bytes from a sequence of buffers. |
| 34 | * |
| 35 | * <p> A <i>gathering</i> write operation writes, in a single invocation, a |
| 36 | * sequence of bytes from one or more of a given sequence of buffers. |
| 37 | * Gathering writes are often useful when implementing network protocols or |
| 38 | * file formats that, for example, group data into segments consisting of one |
| 39 | * or more fixed-length headers followed by a variable-length body. Similar |
| 40 | * <i>scattering</i> read operations are defined in the {@link |
| 41 | * ScatteringByteChannel} interface. </p> |
| 42 | * |
| 43 | * |
| 44 | * @author Mark Reinhold |
| 45 | * @author JSR-51 Expert Group |
| 46 | * @since 1.4 |
| 47 | */ |
| 48 | |
| 49 | public interface GatheringByteChannel |
| 50 | extends WritableByteChannel |
| 51 | { |
| 52 | |
| 53 | /** |
| 54 | * Writes a sequence of bytes to this channel from a subsequence of the |
| 55 | * given buffers. |
| 56 | * |
| 57 | * <p> An attempt is made to write up to <i>r</i> bytes to this channel, |
| 58 | * where <i>r</i> is the total number of bytes remaining in the specified |
| 59 | * subsequence of the given buffer array, that is, |
| 60 | * |
| 61 | * <blockquote><pre> |
| 62 | * srcs[offset].remaining() |
| 63 | * + srcs[offset+1].remaining() |
| 64 | * + ... + srcs[offset+length-1].remaining()</pre></blockquote> |
| 65 | * |
| 66 | * at the moment that this method is invoked. |
| 67 | * |
| 68 | * <p> Suppose that a byte sequence of length <i>n</i> is written, where |
| 69 | * <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>. |
| 70 | * Up to the first <tt>srcs[offset].remaining()</tt> bytes of this sequence |
| 71 | * are written from buffer <tt>srcs[offset]</tt>, up to the next |
| 72 | * <tt>srcs[offset+1].remaining()</tt> bytes are written from buffer |
| 73 | * <tt>srcs[offset+1]</tt>, and so forth, until the entire byte sequence is |
| 74 | * written. As many bytes as possible are written from each buffer, hence |
| 75 | * the final position of each updated buffer, except the last updated |
| 76 | * buffer, is guaranteed to be equal to that buffer's limit. |
| 77 | * |
| 78 | * <p> Unless otherwise specified, a write operation will return only after |
| 79 | * writing all of the <i>r</i> requested bytes. Some types of channels, |
| 80 | * depending upon their state, may write only some of the bytes or possibly |
| 81 | * none at all. A socket channel in non-blocking mode, for example, cannot |
| 82 | * write any more bytes than are free in the socket's output buffer. |
| 83 | * |
| 84 | * <p> This method may be invoked at any time. If another thread has |
| 85 | * already initiated a write operation upon this channel, however, then an |
| 86 | * invocation of this method will block until the first operation is |
| 87 | * complete. </p> |
| 88 | * |
| 89 | * @param srcs |
| 90 | * The buffers from which bytes are to be retrieved |
| 91 | * |
| 92 | * @param offset |
| 93 | * The offset within the buffer array of the first buffer from |
| 94 | * which bytes are to be retrieved; must be non-negative and no |
| 95 | * larger than <tt>srcs.length</tt> |
| 96 | * |
| 97 | * @param length |
| 98 | * The maximum number of buffers to be accessed; must be |
| 99 | * non-negative and no larger than |
| 100 | * <tt>srcs.length</tt> - <tt>offset</tt> |
| 101 | * |
| 102 | * @return The number of bytes written, possibly zero |
| 103 | * |
| 104 | * @throws IndexOutOfBoundsException |
| 105 | * If the preconditions on the <tt>offset</tt> and <tt>length</tt> |
| 106 | * parameters do not hold |
| 107 | * |
| 108 | * @throws NonWritableChannelException |
| 109 | * If this channel was not opened for writing |
| 110 | * |
| 111 | * @throws ClosedChannelException |
| 112 | * If this channel is closed |
| 113 | * |
| 114 | * @throws AsynchronousCloseException |
| 115 | * If another thread closes this channel |
| 116 | * while the write operation is in progress |
| 117 | * |
| 118 | * @throws ClosedByInterruptException |
| 119 | * If another thread interrupts the current thread |
| 120 | * while the write operation is in progress, thereby |
| 121 | * closing the channel and setting the current thread's |
| 122 | * interrupt status |
| 123 | * |
| 124 | * @throws IOException |
| 125 | * If some other I/O error occurs |
| 126 | */ |
| 127 | public long write(ByteBuffer[] srcs, int offset, int length) |
| 128 | throws IOException; |
| 129 | |
| 130 | |
| 131 | /** |
| 132 | * Writes a sequence of bytes to this channel from the given buffers. |
| 133 | * |
| 134 | * <p> An invocation of this method of the form <tt>c.write(srcs)</tt> |
| 135 | * behaves in exactly the same manner as the invocation |
| 136 | * |
| 137 | * <blockquote><pre> |
| 138 | * c.write(srcs, 0, srcs.length);</pre></blockquote> |
| 139 | * |
| 140 | * @param srcs |
| 141 | * The buffers from which bytes are to be retrieved |
| 142 | * |
| 143 | * @return The number of bytes written, possibly zero |
| 144 | * |
| 145 | * @throws NonWritableChannelException |
| 146 | * If this channel was not opened for writing |
| 147 | * |
| 148 | * @throws ClosedChannelException |
| 149 | * If this channel is closed |
| 150 | * |
| 151 | * @throws AsynchronousCloseException |
| 152 | * If another thread closes this channel |
| 153 | * while the write operation is in progress |
| 154 | * |
| 155 | * @throws ClosedByInterruptException |
| 156 | * If another thread interrupts the current thread |
| 157 | * while the write operation is in progress, thereby |
| 158 | * closing the channel and setting the current thread's |
| 159 | * interrupt status |
| 160 | * |
| 161 | * @throws IOException |
| 162 | * If some other I/O error occurs |
| 163 | */ |
| 164 | public long write(ByteBuffer[] srcs) throws IOException; |
| 165 | |
| 166 | } |