001 /*
002 * Copyright 2000-2006 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 read bytes into a sequence of buffers.
033 *
034 * <p> A <i>scattering</i> read operation reads, in a single invocation, a
035 * sequence of bytes into one or more of a given sequence of buffers.
036 * Scattering reads 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>gathering</i> write operations are defined in the {@link
040 * GatheringByteChannel} interface. </p>
041 *
042 *
043 * @author Mark Reinhold
044 * @author JSR-51 Expert Group
045 * @version 1.21, 07/05/05
046 * @since 1.4
047 */
048
049 public interface ScatteringByteChannel extends ReadableByteChannel {
050
051 /**
052 * Reads a sequence of bytes from this channel into a subsequence of the
053 * given buffers.
054 *
055 * <p> An invocation of this method attempts to read up to <i>r</i> bytes
056 * from this channel, where <i>r</i> is the total number of bytes remaining
057 * the specified subsequence of the given buffer array, that is,
058 *
059 * <blockquote><pre>
060 * dsts[offset].remaining()
061 * + dsts[offset+1].remaining()
062 * + ... + dsts[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 read, where
067 * <tt>0</tt> <tt><=</tt> <i>n</i> <tt><=</tt> <i>r</i>.
068 * Up to the first <tt>dsts[offset].remaining()</tt> bytes of this sequence
069 * are transferred into buffer <tt>dsts[offset]</tt>, up to the next
070 * <tt>dsts[offset+1].remaining()</tt> bytes are transferred into buffer
071 * <tt>dsts[offset+1]</tt>, and so forth, until the entire byte sequence
072 * is transferred into the given buffers. As many bytes as possible are
073 * transferred into each buffer, hence the final position of each updated
074 * buffer, except the last updated buffer, is guaranteed to be equal to
075 * that buffer's limit.
076 *
077 * <p> This method may be invoked at any time. If another thread has
078 * already initiated a read operation upon this channel, however, then an
079 * invocation of this method will block until the first operation is
080 * complete. </p>
081 *
082 * @param dsts
083 * The buffers into which bytes are to be transferred
084 *
085 * @param offset
086 * The offset within the buffer array of the first buffer into
087 * which bytes are to be transferred; must be non-negative and no
088 * larger than <tt>dsts.length</tt>
089 *
090 * @param length
091 * The maximum number of buffers to be accessed; must be
092 * non-negative and no larger than
093 * <tt>dsts.length</tt> - <tt>offset</tt>
094 *
095 * @return The number of bytes read, possibly zero,
096 * or <tt>-1</tt> if the channel has reached end-of-stream
097 *
098 * @throws IndexOutOfBoundsException
099 * If the preconditions on the <tt>offset</tt> and <tt>length</tt>
100 * parameters do not hold
101 *
102 * @throws NonReadableChannelException
103 * If this channel was not opened for reading
104 *
105 * @throws ClosedChannelException
106 * If this channel is closed
107 *
108 * @throws AsynchronousCloseException
109 * If another thread closes this channel
110 * while the read operation is in progress
111 *
112 * @throws ClosedByInterruptException
113 * If another thread interrupts the current thread
114 * while the read operation is in progress, thereby
115 * closing the channel and setting the current thread's
116 * interrupt status
117 *
118 * @throws IOException
119 * If some other I/O error occurs
120 */
121 public long read(ByteBuffer[] dsts, int offset, int length)
122 throws IOException;
123
124 /**
125 * Reads a sequence of bytes from this channel into the given buffers.
126 *
127 * <p> An invocation of this method of the form <tt>c.read(dsts)</tt>
128 * behaves in exactly the same manner as the invocation
129 *
130 * <blockquote><pre>
131 * c.read(dsts, 0, dsts.length);</pre></blockquote>
132 *
133 * @param dsts
134 * The buffers into which bytes are to be transferred
135 *
136 * @return The number of bytes read, possibly zero,
137 * or <tt>-1</tt> if the channel has reached end-of-stream
138 *
139 * @throws NonReadableChannelException
140 * If this channel was not opened for reading
141 *
142 * @throws ClosedChannelException
143 * If this channel is closed
144 *
145 * @throws AsynchronousCloseException
146 * If another thread closes this channel
147 * while the read operation is in progress
148 *
149 * @throws ClosedByInterruptException
150 * If another thread interrupts the current thread
151 * while the read operation is in progress, thereby
152 * closing the channel and setting the current thread's
153 * interrupt status
154 *
155 * @throws IOException
156 * If some other I/O error occurs
157 */
158 public long read(ByteBuffer[] dsts) throws IOException;
159
160 }
|