src/java.base/share/classes/java/nio/channels/ScatteringByteChannel.java
author bpb
Fri, 22 Nov 2019 09:00:16 -0800
changeset 59227 46084917fde7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException Reviewed-by: alanb, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
59227
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio.channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A channel that can read bytes into a sequence of buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> A <i>scattering</i> read operation reads, in a single invocation, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * sequence of bytes into one or more of a given sequence of buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Scattering reads are often useful when implementing network protocols or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * file formats that, for example, group data into segments consisting of one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * or more fixed-length headers followed by a variable-length body.  Similar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <i>gathering</i> write operations are defined in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * GatheringByteChannel} interface.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public interface ScatteringByteChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    extends ReadableByteChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Reads a sequence of bytes from this channel into a subsequence of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * given buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * <p> An invocation of this method attempts to read up to <i>r</i> bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * from this channel, where <i>r</i> is the total number of bytes remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * the specified subsequence of the given buffer array, that is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * dsts[offset].remaining()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *     + dsts[offset+1].remaining()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *     + ... + dsts[offset+length-1].remaining()</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * at the moment that this method is invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * <p> Suppose that a byte sequence of length <i>n</i> is read, where
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    69
     * {@code 0}&nbsp;{@code <=}&nbsp;<i>n</i>&nbsp;{@code <=}&nbsp;<i>r</i>.
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    70
     * Up to the first {@code dsts[offset].remaining()} bytes of this sequence
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    71
     * are transferred into buffer {@code dsts[offset]}, up to the next
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    72
     * {@code dsts[offset+1].remaining()} bytes are transferred into buffer
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    73
     * {@code dsts[offset+1]}, and so forth, until the entire byte sequence
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * is transferred into the given buffers.  As many bytes as possible are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * transferred into each buffer, hence the final position of each updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * buffer, except the last updated buffer, is guaranteed to be equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * that buffer's limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <p> This method may be invoked at any time.  If another thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * already initiated a read operation upon this channel, however, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * invocation of this method will block until the first operation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * complete. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param  dsts
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *         The buffers into which bytes are to be transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *         The offset within the buffer array of the first buffer into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *         which bytes are to be transferred; must be non-negative and no
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    90
     *         larger than {@code dsts.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param  length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *         The maximum number of buffers to be accessed; must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *         non-negative and no larger than
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    95
     *         {@code dsts.length}&nbsp;-&nbsp;{@code offset}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @return The number of bytes read, possibly zero,
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    98
     *         or {@code -1} if the channel has reached end-of-stream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @throws  IndexOutOfBoundsException
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   101
     *          If the preconditions on the {@code offset} and {@code length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *          parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
59227
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
   104
     * @throws  IllegalArgumentException
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
   105
     *          If any of the buffers is read-only
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
   106
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @throws  NonReadableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *          If this channel was not opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *          while the read operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *          while the read operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public long read(ByteBuffer[] dsts, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Reads a sequence of bytes from this channel into the given buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   132
     * <p> An invocation of this method of the form {@code c.read(dsts)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * behaves in exactly the same manner as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * c.read(dsts, 0, dsts.length);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param  dsts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *         The buffers into which bytes are to be transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return The number of bytes read, possibly zero,
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   142
     *         or {@code -1} if the channel has reached end-of-stream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
59227
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
   144
     * @throws  IllegalArgumentException
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
   145
     *          If any of the buffers is read-only
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
   146
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @throws  NonReadableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *          If this channel was not opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *          while the read operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *          while the read operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public long read(ByteBuffer[] dsts) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
}