src/java.base/share/classes/java/io/PipedWriter.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 47216 71c04702a3d5
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    48     private boolean closed = false;
    48     private boolean closed = false;
    49 
    49 
    50     /**
    50     /**
    51      * Creates a piped writer connected to the specified piped
    51      * Creates a piped writer connected to the specified piped
    52      * reader. Data characters written to this stream will then be
    52      * reader. Data characters written to this stream will then be
    53      * available as input from <code>snk</code>.
    53      * available as input from {@code snk}.
    54      *
    54      *
    55      * @param      snk   The piped reader to connect to.
    55      * @param      snk   The piped reader to connect to.
    56      * @exception  IOException  if an I/O error occurs.
    56      * @throws     IOException  if an I/O error occurs.
    57      */
    57      */
    58     public PipedWriter(PipedReader snk)  throws IOException {
    58     public PipedWriter(PipedReader snk)  throws IOException {
    59         connect(snk);
    59         connect(snk);
    60     }
    60     }
    61 
    61 
    71     }
    71     }
    72 
    72 
    73     /**
    73     /**
    74      * Connects this piped writer to a receiver. If this object
    74      * Connects this piped writer to a receiver. If this object
    75      * is already connected to some other piped reader, an
    75      * is already connected to some other piped reader, an
    76      * <code>IOException</code> is thrown.
    76      * {@code IOException} is thrown.
    77      * <p>
    77      * <p>
    78      * If <code>snk</code> is an unconnected piped reader and
    78      * If {@code snk} is an unconnected piped reader and
    79      * <code>src</code> is an unconnected piped writer, they may
    79      * {@code src} is an unconnected piped writer, they may
    80      * be connected by either the call:
    80      * be connected by either the call:
    81      * <blockquote><pre>
    81      * <blockquote><pre>
    82      * src.connect(snk)</pre></blockquote>
    82      * src.connect(snk)</pre></blockquote>
    83      * or the call:
    83      * or the call:
    84      * <blockquote><pre>
    84      * <blockquote><pre>
    85      * snk.connect(src)</pre></blockquote>
    85      * snk.connect(src)</pre></blockquote>
    86      * The two calls have the same effect.
    86      * The two calls have the same effect.
    87      *
    87      *
    88      * @param      snk   the piped reader to connect to.
    88      * @param      snk   the piped reader to connect to.
    89      * @exception  IOException  if an I/O error occurs.
    89      * @throws     IOException  if an I/O error occurs.
    90      */
    90      */
    91     public synchronized void connect(PipedReader snk) throws IOException {
    91     public synchronized void connect(PipedReader snk) throws IOException {
    92         if (snk == null) {
    92         if (snk == null) {
    93             throw new NullPointerException();
    93             throw new NullPointerException();
    94         } else if (sink != null || snk.connected) {
    94         } else if (sink != null || snk.connected) {
   102         snk.out = 0;
   102         snk.out = 0;
   103         snk.connected = true;
   103         snk.connected = true;
   104     }
   104     }
   105 
   105 
   106     /**
   106     /**
   107      * Writes the specified <code>char</code> to the piped output stream.
   107      * Writes the specified {@code char} to the piped output stream.
   108      * If a thread was reading data characters from the connected piped input
   108      * If a thread was reading data characters from the connected piped input
   109      * stream, but the thread is no longer alive, then an
   109      * stream, but the thread is no longer alive, then an
   110      * <code>IOException</code> is thrown.
   110      * {@code IOException} is thrown.
   111      * <p>
   111      * <p>
   112      * Implements the <code>write</code> method of <code>Writer</code>.
   112      * Implements the {@code write} method of {@code Writer}.
   113      *
   113      *
   114      * @param      c   the <code>char</code> to be written.
   114      * @param   c   the {@code char} to be written.
   115      * @exception  IOException  if the pipe is
   115      * @throw   IOException  if the pipe is
   116      *          <a href=PipedOutputStream.html#BROKEN> <code>broken</code></a>,
   116      *          <a href=PipedOutputStream.html#BROKEN> {@code broken}</a>,
   117      *          {@link #connect(java.io.PipedReader) unconnected}, closed
   117      *          {@link #connect(java.io.PipedReader) unconnected}, closed
   118      *          or an I/O error occurs.
   118      *          or an I/O error occurs.
   119      */
   119      */
   120     public void write(int c)  throws IOException {
   120     public void write(int c)  throws IOException {
   121         if (sink == null) {
   121         if (sink == null) {
   131      * stream.
   131      * stream.
   132      * If a thread was reading data characters from the connected piped input
   132      * If a thread was reading data characters from the connected piped input
   133      * stream, but the thread is no longer alive, then an
   133      * stream, but the thread is no longer alive, then an
   134      * {@code IOException} is thrown.
   134      * {@code IOException} is thrown.
   135      *
   135      *
   136      * @param      cbuf  the data.
   136      * @param   cbuf  the data.
   137      * @param      off   the start offset in the data.
   137      * @param   off   the start offset in the data.
   138      * @param      len   the number of characters to write.
   138      * @param   len   the number of characters to write.
   139      *
   139      *
   140      * @throws  IndexOutOfBoundsException
   140      * @throws  IndexOutOfBoundsException
   141      *          If {@code off} is negative, or {@code len} is negative,
   141      *          If {@code off} is negative, or {@code len} is negative,
   142      *          or {@code off + len} is negative or greater than the length
   142      *          or {@code off + len} is negative or greater than the length
   143      *          of the given array
   143      *          of the given array
   144      *
   144      *
   145      * @throws  IOException  if the pipe is
   145      * @throws  IOException  if the pipe is
   146      *          <a href=PipedOutputStream.html#BROKEN><code>broken</code></a>,
   146      *          <a href=PipedOutputStream.html#BROKEN>{@code broken}</a>,
   147      *          {@link #connect(java.io.PipedReader) unconnected}, closed
   147      *          {@link #connect(java.io.PipedReader) unconnected}, closed
   148      *          or an I/O error occurs.
   148      *          or an I/O error occurs.
   149      */
   149      */
   150     public void write(char cbuf[], int off, int len) throws IOException {
   150     public void write(char cbuf[], int off, int len) throws IOException {
   151         if (sink == null) {
   151         if (sink == null) {
   159     /**
   159     /**
   160      * Flushes this output stream and forces any buffered output characters
   160      * Flushes this output stream and forces any buffered output characters
   161      * to be written out.
   161      * to be written out.
   162      * This will notify any readers that characters are waiting in the pipe.
   162      * This will notify any readers that characters are waiting in the pipe.
   163      *
   163      *
   164      * @exception  IOException  if the pipe is closed, or an I/O error occurs.
   164      * @throws     IOException  if the pipe is closed, or an I/O error occurs.
   165      */
   165      */
   166     public synchronized void flush() throws IOException {
   166     public synchronized void flush() throws IOException {
   167         if (sink != null) {
   167         if (sink != null) {
   168             if (sink.closedByReader || closed) {
   168             if (sink.closedByReader || closed) {
   169                 throw new IOException("Pipe closed");
   169                 throw new IOException("Pipe closed");
   177     /**
   177     /**
   178      * Closes this piped output stream and releases any system resources
   178      * Closes this piped output stream and releases any system resources
   179      * associated with this stream. This stream may no longer be used for
   179      * associated with this stream. This stream may no longer be used for
   180      * writing characters.
   180      * writing characters.
   181      *
   181      *
   182      * @exception  IOException  if an I/O error occurs.
   182      * @throws     IOException  if an I/O error occurs.
   183      */
   183      */
   184     public void close()  throws IOException {
   184     public void close()  throws IOException {
   185         closed = true;
   185         closed = true;
   186         if (sink != null) {
   186         if (sink != null) {
   187             sink.receivedLast();
   187             sink.receivedLast();