src/java.base/share/classes/java/io/PipedOutputStream.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
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A piped output stream can be connected to a piped input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * to create a communications pipe. The piped output stream is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * sending end of the pipe. Typically, data is written to a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    34
 * {@code PipedOutputStream} object by one thread and data is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    35
 * read from the connected {@code PipedInputStream} by some
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * other thread. Attempting to use both objects from a single thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * is not recommended as it may deadlock the thread.
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 25859
diff changeset
    38
 * The pipe is said to be <a id=BROKEN> <i>broken</i> </a> if a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * thread that was reading data bytes from the connected piped input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * stream is no longer alive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see     java.io.PipedInputStream
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 5506
diff changeset
    44
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
class PipedOutputStream extends OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        /* REMIND: identification of the read and write sides needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
           more sophisticated.  Either using thread groups (but what about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
           pipes within a thread?) or using finalization (but it may be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
           long time until the next GC). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private PipedInputStream sink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Creates a piped output stream connected to the specified piped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * input stream. Data bytes written to this stream will then be
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    58
     * available as input from {@code snk}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @param      snk   The piped input stream to connect to.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    61
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public PipedOutputStream(PipedInputStream snk)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        connect(snk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Creates a piped output stream that is not yet connected to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * piped input stream. It must be connected to a piped input stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * either by the receiver or the sender, before being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @see     java.io.PipedInputStream#connect(java.io.PipedOutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @see     java.io.PipedOutputStream#connect(java.io.PipedInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    public PipedOutputStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Connects this piped output stream to a receiver. If this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * is already connected to some other piped input stream, an
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    81
     * {@code IOException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    83
     * If {@code snk} is an unconnected piped input stream and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    84
     * {@code src} is an unconnected piped output stream, they may
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * be connected by either the call:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * src.connect(snk)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * or the call:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * snk.connect(src)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * The two calls have the same effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param      snk   the piped input stream to connect to.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    94
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public synchronized void connect(PipedInputStream snk) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (snk == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } else if (sink != null || snk.connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new IOException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        sink = snk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        snk.in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        snk.out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        snk.connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   109
     * Writes the specified {@code byte} to the piped output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   111
     * Implements the {@code write} method of {@code OutputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   113
     * @param   b   the {@code byte} to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   114
     * @throws  IOException if the pipe is <a href=#BROKEN> broken</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *          {@link #connect(java.io.PipedInputStream) unconnected},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *          closed, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public void write(int b)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (sink == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        sink.receive(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   126
     * Writes {@code len} bytes from the specified byte array
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   127
     * starting at offset {@code off} to this piped output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * This method blocks until all the bytes are written to the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   131
     * @param   b     the data.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   132
     * @param   off   the start offset in the data.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   133
     * @param   len   the number of bytes to write.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   134
     * @throws  IOException if the pipe is <a href=#BROKEN> broken</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *          {@link #connect(java.io.PipedInputStream) unconnected},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *          closed, or if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (sink == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } else if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } else if ((off < 0) || (off > b.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                   ((off + len) > b.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        sink.receive(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Flushes this output stream and forces any buffered output bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * to be written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * This will notify any readers that bytes are waiting in the pipe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   157
     * @throws    IOException if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public synchronized void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if (sink != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            synchronized (sink) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                sink.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Closes this piped output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * associated with this stream. This stream may no longer be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * writing bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   172
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    public void close()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (sink != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            sink.receivedLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}