src/java.base/share/classes/java/nio/channels/GatheringByteChannel.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 32143 jdk/src/java.base/share/classes/java/nio/channels/GatheringByteChannel.java@394ab6a6658d
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2000, 2001, 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 write bytes from a sequence of buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> A <i>gathering</i> write operation writes, in a single invocation, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * sequence of bytes from one or more of a given sequence of buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Gathering writes 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>scattering</i> read operations are defined in the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * ScatteringByteChannel} 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 GatheringByteChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    extends WritableByteChannel
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
     * Writes a sequence of bytes to this channel from 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 attempt is made to write up to <i>r</i> bytes to this channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * where <i>r</i> is the total number of bytes remaining in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * 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
     * srcs[offset].remaining()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *     + srcs[offset+1].remaining()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     *     + ... + srcs[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 written, 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 srcs[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 written from buffer {@code srcs[offset]}, up to the next
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    72
     * {@code srcs[offset+1].remaining()} bytes are written from buffer
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    73
     * {@code srcs[offset+1]}, and so forth, until the entire byte sequence is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * written.  As many bytes as possible are written from each buffer, hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * the final position of each updated buffer, except the last updated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * buffer, is guaranteed to be equal to that buffer's limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <p> Unless otherwise specified, a write operation will return only after
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * writing all of the <i>r</i> requested bytes.  Some types of channels,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * depending upon their state, may write only some of the bytes or possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * none at all.  A socket channel in non-blocking mode, for example, cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * write any more bytes than are free in the socket's output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <p> This method may be invoked at any time.  If another thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * already initiated a write operation upon this channel, however, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * invocation of this method will block until the first operation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * complete. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param  srcs
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *         The buffers from which bytes are to be retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param  offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *         The offset within the buffer array of the first buffer from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *         which bytes are to be retrieved; must be non-negative and no
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    95
     *         larger than {@code srcs.length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param  length
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *         The maximum number of buffers to be accessed; must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *         non-negative and no larger than
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   100
     *         {@code srcs.length}&nbsp;-&nbsp;{@code offset}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @return  The number of bytes written, possibly zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @throws  IndexOutOfBoundsException
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   105
     *          If the preconditions on the {@code offset} and {@code length}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *          parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @throws  NonWritableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *          If this channel was not opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *          while the write operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *          while the write operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public long write(ByteBuffer[] srcs, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Writes a sequence of bytes to this channel from the given buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
   134
     * <p> An invocation of this method of the form {@code c.write(srcs)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * behaves in exactly the same manner as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * c.write(srcs, 0, srcs.length);</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param  srcs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         The buffers from which bytes are to be retrieved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return  The number of bytes written, possibly zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws  NonWritableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *          If this channel was not opened for writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *          while the write operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *          while the write operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    public long write(ByteBuffer[] srcs) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}