src/java.base/share/classes/java/io/PipedInputStream.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1995, 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.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A piped input stream should be connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * to a piped output stream; the piped  input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * stream then provides whatever data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * are written to the piped output  stream.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    33
 * Typically, data is read from a {@code PipedInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * object by one thread  and data is written
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    35
 * to the corresponding {@code PipedOutputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * by some  other thread. Attempting to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * both objects from a single thread is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * recommended, as it may deadlock the thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The piped input stream contains a buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * decoupling read operations from write operations,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * within limits.
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 34774
diff changeset
    42
 * A pipe is said to be <a id="BROKEN"> <i>broken</i> </a> if a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * thread that was providing data bytes to the connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * piped output stream is no longer alive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author  James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see     java.io.PipedOutputStream
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21334
diff changeset
    48
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class PipedInputStream extends InputStream {
34774
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 25859
diff changeset
    51
    boolean closedByWriter;
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 25859
diff changeset
    52
    volatile boolean closedByReader;
03b4e6dc367b 8145680: Remove unnecessary explicit initialization of volatile variables in java.base
redestad
parents: 25859
diff changeset
    53
    boolean connected;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        /* REMIND: identification of the read and write sides needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
           more sophisticated.  Either using thread groups (but what about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
           pipes within a thread?) or using finalization (but it may be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
           long time until the next GC). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    Thread readSide;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    Thread writeSide;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int DEFAULT_PIPE_SIZE = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * The default size of the pipe's circular input buffer.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21334
diff changeset
    66
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // This used to be a constant before the pipe size was allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // to change. This field will continue to be maintained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // for backward compatibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected static final int PIPE_SIZE = DEFAULT_PIPE_SIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * The circular buffer into which incoming data is placed.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21334
diff changeset
    75
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected byte buffer[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * The index of the position in the circular buffer at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * next byte of data will be stored when received from the connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * piped output stream. <code>in&lt;0</code> implies the buffer is empty,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    83
     * {@code in==out} implies the buffer is full
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21334
diff changeset
    84
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected int in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * The index of the position in the circular buffer at which the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * byte of data will be read by this piped input stream.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21334
diff changeset
    91
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    protected int out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    96
     * Creates a {@code PipedInputStream} so
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * that it is connected to the piped output
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    98
     * stream {@code src}. Data bytes written
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    99
     * to {@code src} will then be  available
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * as input from this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param      src   the stream to connect to.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   103
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public PipedInputStream(PipedOutputStream src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this(src, DEFAULT_PIPE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   110
     * Creates a {@code PipedInputStream} so that it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * connected to the piped output stream
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   112
     * {@code src} and uses the specified pipe size for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * the pipe's buffer.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   114
     * Data bytes written to {@code src} will then
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * be available as input from this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @param      src   the stream to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param      pipeSize the size of the pipe's buffer.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   119
     * @throws     IOException  if an I/O error occurs.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   120
     * @throws     IllegalArgumentException if {@code pipeSize <= 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @since      1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public PipedInputStream(PipedOutputStream src, int pipeSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         initPipe(pipeSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
         connect(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   130
     * Creates a {@code PipedInputStream} so
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * that it is not yet {@linkplain #connect(java.io.PipedOutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * connected}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * It must be {@linkplain java.io.PipedOutputStream#connect(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * java.io.PipedInputStream) connected} to a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   135
     * {@code PipedOutputStream} before being used.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    public PipedInputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        initPipe(DEFAULT_PIPE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   142
     * Creates a {@code PipedInputStream} so that it is not yet
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * {@linkplain #connect(java.io.PipedOutputStream) connected} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * uses the specified pipe size for the pipe's buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * It must be {@linkplain java.io.PipedOutputStream#connect(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * java.io.PipedInputStream)
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   147
     * connected} to a {@code PipedOutputStream} before being used.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param      pipeSize the size of the pipe's buffer.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   150
     * @throws     IllegalArgumentException if {@code pipeSize <= 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @since      1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public PipedInputStream(int pipeSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        initPipe(pipeSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private void initPipe(int pipeSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
         if (pipeSize <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new IllegalArgumentException("Pipe Size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         buffer = new byte[pipeSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Causes this piped input stream to be connected
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   166
     * to the piped  output stream {@code src}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * If this object is already connected to some
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   168
     * other piped output  stream, an {@code IOException}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   171
     * If {@code src} is an
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   172
     * unconnected piped output stream and {@code snk}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * is an unconnected piped input stream, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * may be connected by either the call:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18786
diff changeset
   175
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   176
     * <pre>{@code snk.connect(src)} </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * or the call:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18786
diff changeset
   179
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   180
     * <pre>{@code src.connect(snk)} </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <p>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18786
diff changeset
   182
     * The two calls have the same effect.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param      src   The piped output stream to connect to.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   185
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public void connect(PipedOutputStream src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        src.connect(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Receives a byte of data.  This method will block if no input is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * available.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   194
     * @param   b the byte being received
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   195
     * @throws  IOException If the pipe is <a href="#BROKEN"> {@code broken}</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *          {@link #connect(java.io.PipedOutputStream) unconnected},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *          closed, or if an I/O error occurs.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   198
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    protected synchronized void receive(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        checkStateForReceive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        writeSide = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (in == out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            awaitSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (in < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            in = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        buffer[in++] = (byte)(b & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        if (in >= buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            in = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * Receives data into an array of bytes.  This method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * block until some input is available.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   218
     * @param    b the buffer into which the data is received
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   219
     * @param    off the start offset of the data
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   220
     * @param    len the maximum number of bytes received
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   221
     * @throws   IOException If the pipe is <a href="#BROKEN"> broken</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *           {@link #connect(java.io.PipedOutputStream) unconnected},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *           closed,or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    synchronized void receive(byte b[], int off, int len)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        checkStateForReceive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        writeSide = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int bytesToTransfer = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        while (bytesToTransfer > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (in == out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                awaitSpace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            int nextTransferAmount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            if (out < in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                nextTransferAmount = buffer.length - in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            } else if (in < out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if (in == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    in = out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    nextTransferAmount = buffer.length - in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    nextTransferAmount = out - in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (nextTransferAmount > bytesToTransfer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                nextTransferAmount = bytesToTransfer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            assert(nextTransferAmount > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            System.arraycopy(b, off, buffer, in, nextTransferAmount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            bytesToTransfer -= nextTransferAmount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            off += nextTransferAmount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            in += nextTransferAmount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (in >= buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                in = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    private void checkStateForReceive() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        } else if (closedByWriter || closedByReader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            throw new IOException("Pipe closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } else if (readSide != null && !readSide.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new IOException("Read end dead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    private void awaitSpace() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        while (in == out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            checkStateForReceive();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            /* full: kick any waiting readers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                wait(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                throw new java.io.InterruptedIOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * Notifies all waiting threads that the last byte of data has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    synchronized void receivedLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        closedByWriter = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Reads the next byte of data from this piped input stream. The
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   291
     * value byte is returned as an {@code int} in the range
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   292
     * {@code 0} to {@code 255}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * This method blocks until input data is available, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   296
     * @return   the next byte of data, or {@code -1} if the end of the
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   297
     *           stream is reached.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   298
     * @throws   IOException  if the pipe is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *           {@link #connect(java.io.PipedOutputStream) unconnected},
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   300
     *           <a href="#BROKEN"> {@code broken}</a>, closed,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *           or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public synchronized int read()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else if (closedByReader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            throw new IOException("Pipe closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } else if (writeSide != null && !writeSide.isAlive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                   && !closedByWriter && (in < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            throw new IOException("Write end dead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        readSide = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        int trials = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        while (in < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            if (closedByWriter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                /* closed by writer, return EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if ((writeSide != null) && (!writeSide.isAlive()) && (--trials < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                throw new IOException("Pipe broken");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            /* might be a writer waiting */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                wait(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                throw new java.io.InterruptedIOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        int ret = buffer[out++] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        if (out >= buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (in == out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            /* now empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   344
     * Reads up to {@code len} bytes of data from this piped input
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   345
     * stream into an array of bytes. Less than {@code len} bytes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * will be read if the end of the data stream is reached or if
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   347
     * {@code len} exceeds the pipe's buffer size.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   348
     * If {@code len } is zero, then no bytes are read and 0 is returned;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * otherwise, the method blocks until at least 1 byte of input is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * available, end of the stream has been detected, or an exception is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * @param      b     the buffer into which the data is read.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   354
     * @param      off   the start offset in the destination array {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * @return     the total number of bytes read into the buffer, or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   357
     *             {@code -1} if there is no more data because the end of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     *             the stream has been reached.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   359
     * @throws     NullPointerException If {@code b} is {@code null}.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   360
     * @throws     IndexOutOfBoundsException If {@code off} is negative,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   361
     *             {@code len} is negative, or {@code len} is greater than
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   362
     *             {@code b.length - off}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   363
     * @throws     IOException if the pipe is <a href="#BROKEN"> {@code broken}</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *           {@link #connect(java.io.PipedOutputStream) unconnected},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *           closed, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    public synchronized int read(byte b[], int off, int len)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        } else if (off < 0 || len < 0 || len > b.length - off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        /* possibly wait on the first character */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        int c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        if (c < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        b[off] = (byte) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        int rlen = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        while ((in >= 0) && (len > 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            int available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            if (in > out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                available = Math.min((buffer.length - out), (in - out));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                available = buffer.length - out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            // A byte is read beforehand outside the loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            if (available > (len - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                available = len - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            System.arraycopy(buffer, out, b, off + rlen, available);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            out += available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            rlen += available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            len -= available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (out >= buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            if (in == out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                /* now empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        return rlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * Returns the number of bytes that can be read from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @return the number of bytes that can be read from this input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *         without blocking, or {@code 0} if this input stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *         closed by invoking its {@link #close()} method, or if the pipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *         is {@link #connect(java.io.PipedOutputStream) unconnected}, or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   421
     *         <a href="#BROKEN"> {@code broken}</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   423
     * @throws IOException  if an I/O error occurs.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   424
     * @since  1.0.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public synchronized int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if(in < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        else if(in == out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            return buffer.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        else if (in > out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            return in - out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return in + buffer.length - out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Closes this piped input stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   441
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public void close()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        closedByReader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
}