src/java.base/share/classes/java/nio/channels/ReadableByteChannel.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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> Only one read operation upon a readable channel may be in progress at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * any given time.  If one thread initiates a read operation upon a channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * then any other thread that attempts to initiate another read operation will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * block until the first operation is complete.  Whether or not other kinds of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * I/O operations may proceed concurrently with a read operation depends upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * the type of the channel. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public interface ReadableByteChannel extends Channel {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Reads a sequence of bytes from this channel into the given buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * <p> An attempt is made to read up to <i>r</i> bytes from the channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * where <i>r</i> is the number of bytes remaining in the buffer, that is,
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    55
     * {@code dst.remaining()}, at the moment this method is invoked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * <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
    58
     * {@code 0}&nbsp;{@code <=}&nbsp;<i>n</i>&nbsp;{@code <=}&nbsp;<i>r</i>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * This byte sequence will be transferred into the buffer so that the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * byte in the sequence is at index <i>p</i> and the last byte is at index
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    61
     * <i>p</i>&nbsp;{@code +}&nbsp;<i>n</i>&nbsp;{@code -}&nbsp;{@code 1},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * where <i>p</i> is the buffer's position at the moment this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * invoked.  Upon return the buffer's position will be equal to
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    64
     * <i>p</i>&nbsp;{@code +}&nbsp;<i>n</i>; its limit will not have changed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * <p> A read operation might not fill the buffer, and in fact it might not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * read any bytes at all.  Whether or not it does so depends upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * nature and state of the channel.  A socket channel in non-blocking mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * for example, cannot read any more bytes than are immediately available
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * from the socket's input buffer; similarly, a file channel cannot read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * any more bytes than remain in the file.  It is guaranteed, however, that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * if a channel is in blocking mode and there is at least one byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * remaining in the buffer then this method will block until at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * byte is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * <p> This method may be invoked at any time.  If another thread has
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * already initiated a read operation upon this channel, however, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * invocation of this method will block until the first operation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * complete. </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param  dst
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *         The buffer into which bytes are to be transferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
32143
394ab6a6658d 8133459: replace <tt> tags (obsolete in html5) in java.nio docs
avstepan
parents: 25859
diff changeset
    84
     * @return  The number of bytes read, possibly zero, or {@code -1} if the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *          channel has reached end-of-stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
59227
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
    87
     * @throws  IllegalArgumentException
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
    88
     *          If the buffer is read-only
46084917fde7 8164993: (ch) ReadableByteChannel should note a possible IllegalArgumentException
bpb
parents: 47216
diff changeset
    89
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @throws  NonReadableChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *          If this channel was not opened for reading
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *          If this channel is closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @throws  AsynchronousCloseException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *          If another thread closes this channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *          while the read operation is in progress
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @throws  ClosedByInterruptException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *          If another thread interrupts the current thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *          while the read operation is in progress, thereby
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *          closing the channel and setting the current thread's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *          interrupt status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *          If some other I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public int read(ByteBuffer dst) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
}