src/java.base/share/classes/java/io/PipedWriter.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) 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-output 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: 5506
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 PipedWriter extends Writer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /* REMIND: identification of the read and write sides needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
       more sophisticated.  Either using thread groups (but what about
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
       pipes within a thread?) or using finalization (but it may be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
       long time until the next GC). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private PipedReader sink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /* This flag records the open status of this particular writer. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * is independent of the status flags defined in PipedReader. It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * used to do a sanity check on connect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * Creates a piped writer connected to the specified piped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * reader. Data characters 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
    53
     * available as input from {@code snk}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param      snk   The piped reader to connect to.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    56
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public PipedWriter(PipedReader snk)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        connect(snk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Creates a piped writer that is not yet connected to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * piped reader. It must be connected to a piped reader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * either by the receiver or the sender, before being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @see     java.io.PipedReader#connect(java.io.PipedWriter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @see     java.io.PipedWriter#connect(java.io.PipedReader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public PipedWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Connects this piped writer to a receiver. If this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * is already connected to some other piped reader, an
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    76
     * {@code IOException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    78
     * If {@code snk} is an unconnected piped reader and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    79
     * {@code src} is an unconnected piped writer, they may
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * be connected by either the call:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * src.connect(snk)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * or the call:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * snk.connect(src)</pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * The two calls have the same effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param      snk   the piped reader to connect to.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    89
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public synchronized void connect(PipedReader snk) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (snk == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } else if (sink != null || snk.connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new IOException("Already connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else if (snk.closedByReader || closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IOException("Pipe closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        sink = snk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        snk.in = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        snk.out = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        snk.connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   107
     * Writes the specified {@code char} to the piped output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * If a thread was reading data characters from the connected piped input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * stream, but the thread is no longer alive, then an
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   110
     * {@code IOException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   112
     * Implements the {@code write} method of {@code Writer}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   114
     * @param   c   the {@code char} to be written.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   115
     * @throw   IOException  if the pipe is
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   116
     *          <a href=PipedOutputStream.html#BROKEN> {@code broken}</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *          {@link #connect(java.io.PipedReader) unconnected}, closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *          or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public void write(int c)  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (sink == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        sink.receive(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
38373
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   128
     * Writes {@code len} characters from the specified character array
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   129
     * starting at offset {@code off} to this piped output stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * This method blocks until all the characters are written to the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * If a thread was reading data characters from the connected piped input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * stream, but the thread is no longer alive, then an
38373
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   134
     * {@code IOException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   136
     * @param   cbuf  the data.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   137
     * @param   off   the start offset in the data.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   138
     * @param   len   the number of characters to write.
38373
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   139
     *
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   140
     * @throws  IndexOutOfBoundsException
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   141
     *          If {@code off} is negative, or {@code len} is negative,
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   142
     *          or {@code off + len} is negative or greater than the length
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   143
     *          of the given array
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   144
     *
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 25859
diff changeset
   145
     * @throws  IOException  if the pipe is
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   146
     *          <a href=PipedOutputStream.html#BROKEN>{@code broken}</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *          {@link #connect(java.io.PipedReader) unconnected}, closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *          or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    public void write(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (sink == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new IOException("Pipe not connected");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } else if ((off | len | (off + len) | (cbuf.length - (off + len))) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        sink.receive(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Flushes this output stream and forces any buffered output characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * to be written out.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * This will notify any readers that characters are waiting in the pipe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   164
     * @throws     IOException  if the pipe is closed, or an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public synchronized void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (sink != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (sink.closedByReader || closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                throw new IOException("Pipe closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            synchronized (sink) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                sink.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Closes this piped output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * associated with this stream. This stream may no longer be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * writing characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   182
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public void close()  throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (sink != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            sink.receivedLast();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}