src/java.base/share/classes/java/io/PipedReader.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 47216 71c04702a3d5
child 58288 48e480e56aad
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
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) 1996, 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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Piped character-input streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author      Mark Reinhold
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    33
 * @since       1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class PipedReader extends Reader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    boolean closedByWriter = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    boolean closedByReader = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /* REMIND: identification of the read and write sides needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
       more sophisticated.  Either using thread groups (but what about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
       pipes within a thread?) or using finalization (but it may be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
       long time until the next GC). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    Thread readSide;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    Thread writeSide;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    * The size of the pipe's circular input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int DEFAULT_PIPE_SIZE = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The circular buffer into which incoming data is placed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    char buffer[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The index of the position in the circular buffer at which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * next character of data will be stored when received from the connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * piped writer. <code>in&lt;0</code> implies the buffer is empty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * <code>in==out</code> implies the buffer is full
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * The index of the position in the circular buffer at which the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * character of data will be read by this piped reader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    int out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Creates a <code>PipedReader</code> so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * that it is connected to the piped writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <code>src</code>. Data written to <code>src</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * will then be available as input from this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param      src   the stream to connect to.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    79
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public PipedReader(PipedWriter src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this(src, DEFAULT_PIPE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Creates a <code>PipedReader</code> so that it is connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * to the piped writer <code>src</code> and uses the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * pipe size for the pipe's buffer. Data written to <code>src</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * will then be  available as input from this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param      src       the stream to connect to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @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
    93
     * @throws     IOException  if an I/O error occurs.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    94
     * @throws     IllegalArgumentException if {@code pipeSize <= 0}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @since      1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public PipedReader(PipedWriter src, int pipeSize) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        initPipe(pipeSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        connect(src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Creates a <code>PipedReader</code> so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * that it is not yet {@linkplain #connect(java.io.PipedWriter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * connected}. It must be {@linkplain java.io.PipedWriter#connect(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * java.io.PipedReader) connected} to a <code>PipedWriter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * before being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public PipedReader() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        initPipe(DEFAULT_PIPE_SIZE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Creates a <code>PipedReader</code> so that it is not yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * {@link #connect(java.io.PipedWriter) connected} and uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * the specified pipe size for the pipe's buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * It must be  {@linkplain java.io.PipedWriter#connect(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * java.io.PipedReader) connected} to a <code>PipedWriter</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * before being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @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
   123
     * @throws  IllegalArgumentException if {@code pipeSize <= 0}.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   124
     * @since   1.6
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public PipedReader(int pipeSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        initPipe(pipeSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private void initPipe(int pipeSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (pipeSize <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new IllegalArgumentException("Pipe size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        buffer = new char[pipeSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Causes this piped reader to be connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * to the piped  writer <code>src</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * If this object is already connected to some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * other piped writer, an <code>IOException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * If <code>src</code> is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * unconnected piped writer and <code>snk</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * is an unconnected piped reader, they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * may be connected by either the call:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18156
diff changeset
   148
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * <pre><code>snk.connect(src)</code> </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * or the call:
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18156
diff changeset
   152
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * <pre><code>src.connect(snk)</code> </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * <p>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18156
diff changeset
   155
     * The two calls have the same effect.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @param      src   The piped writer to connect to.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   158
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void connect(PipedWriter src) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        src.connect(this);
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
     * Receives a char of data. This method will block if no input is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    synchronized void receive(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else if (closedByWriter || closedByReader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new IOException("Pipe closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        } else if (readSide != null && !readSide.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw new IOException("Read end dead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        writeSide = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        while (in == out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if ((readSide != null) && !readSide.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                throw new IOException("Pipe broken");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            /* full: kick any waiting readers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                wait(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                throw new java.io.InterruptedIOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (in < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            in = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        buffer[in++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (in >= buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            in = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Receives data into an array of characters.  This method will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * block until some input is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    synchronized void receive(char c[], int off, int len)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        while (--len >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            receive(c[off++]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * Notifies all waiting threads that the last character of data has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    synchronized void receivedLast() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        closedByWriter = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Reads the next character of data from this piped stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * If no character is available because the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * has been reached, the value <code>-1</code> is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * This method blocks until input data is available, the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * the stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   226
     * @return  the next character of data, or <code>-1</code> if the end of the
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   227
     *          stream is reached.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   228
     * @throws  IOException  if the pipe is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     *          <a href=PipedInputStream.html#BROKEN> <code>broken</code></a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *          {@link #connect(java.io.PipedWriter) unconnected}, closed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *          or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    public synchronized int read()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } else if (closedByReader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            throw new IOException("Pipe closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } else if (writeSide != null && !writeSide.isAlive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                   && !closedByWriter && (in < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            throw new IOException("Write end dead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        readSide = Thread.currentThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int trials = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        while (in < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            if (closedByWriter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                /* closed by writer, return EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if ((writeSide != null) && (!writeSide.isAlive()) && (--trials < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                throw new IOException("Pipe broken");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            /* might be a writer waiting */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                wait(1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                throw new java.io.InterruptedIOException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        int ret = buffer[out++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        if (out >= buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (in == out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            /* now empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * Reads up to <code>len</code> characters of data from this piped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * stream into an array of characters. Less than <code>len</code> characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * will be read if the end of the data stream is reached or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <code>len</code> exceeds the pipe's buffer size. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * blocks until at least one character of input is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * @param      cbuf     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * @param      off   the start offset of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * @param      len   the maximum number of characters read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @return     the total number of characters read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *             the stream has been reached.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   285
     * @throws     IOException  if the pipe is
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   286
     *             <a href=PipedInputStream.html#BROKEN> <code>broken</code></a>,
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   287
     *             {@link #connect(java.io.PipedWriter) unconnected}, closed,
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   288
     *             or an I/O error occurs.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   289
     * @throws     IndexOutOfBoundsException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    public synchronized int read(char cbuf[], int off, int len)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        } else if (closedByReader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            throw new IOException("Pipe closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        } else if (writeSide != null && !writeSide.isAlive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                   && !closedByWriter && (in < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            throw new IOException("Write end dead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        /* possibly wait on the first character */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        int c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (c < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        cbuf[off] =  (char)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        int rlen = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        while ((in >= 0) && (--len > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            cbuf[off + rlen] = buffer[out++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            rlen++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if (out >= buffer.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            if (in == out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                /* now empty */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return rlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Tell whether this stream is ready to be read.  A piped character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * stream is ready if the circular buffer is not empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   333
     * @throws     IOException  if the pipe is
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   334
     *             <a href=PipedInputStream.html#BROKEN> <code>broken</code></a>,
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   335
     *             {@link #connect(java.io.PipedWriter) unconnected}, or closed.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public synchronized boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        } else if (closedByReader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new IOException("Pipe closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        } else if (writeSide != null && !writeSide.isAlive()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                   && !closedByWriter && (in < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            throw new IOException("Write end dead");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (in < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * Closes this piped stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   357
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public void close()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        closedByReader = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
}