1 /* |
1 /* |
2 * Copyright (c) 1995, 2006, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1995, 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 |
29 |
29 |
30 /** |
30 /** |
31 * A piped output stream can be connected to a piped input stream |
31 * A piped output stream can be connected to a piped input stream |
32 * to create a communications pipe. The piped output stream is the |
32 * to create a communications pipe. The piped output stream is the |
33 * sending end of the pipe. Typically, data is written to a |
33 * sending end of the pipe. Typically, data is written to a |
34 * <code>PipedOutputStream</code> object by one thread and data is |
34 * {@code PipedOutputStream} object by one thread and data is |
35 * read from the connected <code>PipedInputStream</code> by some |
35 * read from the connected {@code PipedInputStream} by some |
36 * other thread. Attempting to use both objects from a single thread |
36 * other thread. Attempting to use both objects from a single thread |
37 * is not recommended as it may deadlock the thread. |
37 * is not recommended as it may deadlock the thread. |
38 * The pipe is said to be <a id=BROKEN> <i>broken</i> </a> if a |
38 * The pipe is said to be <a id=BROKEN> <i>broken</i> </a> if a |
39 * thread that was reading data bytes from the connected piped input |
39 * thread that was reading data bytes from the connected piped input |
40 * stream is no longer alive. |
40 * stream is no longer alive. |
53 private PipedInputStream sink; |
53 private PipedInputStream sink; |
54 |
54 |
55 /** |
55 /** |
56 * Creates a piped output stream connected to the specified piped |
56 * Creates a piped output stream connected to the specified piped |
57 * input stream. Data bytes written to this stream will then be |
57 * input stream. Data bytes written to this stream will then be |
58 * available as input from <code>snk</code>. |
58 * available as input from {@code snk}. |
59 * |
59 * |
60 * @param snk The piped input stream to connect to. |
60 * @param snk The piped input stream to connect to. |
61 * @exception IOException if an I/O error occurs. |
61 * @throws IOException if an I/O error occurs. |
62 */ |
62 */ |
63 public PipedOutputStream(PipedInputStream snk) throws IOException { |
63 public PipedOutputStream(PipedInputStream snk) throws IOException { |
64 connect(snk); |
64 connect(snk); |
65 } |
65 } |
66 |
66 |
76 } |
76 } |
77 |
77 |
78 /** |
78 /** |
79 * Connects this piped output stream to a receiver. If this object |
79 * Connects this piped output stream to a receiver. If this object |
80 * is already connected to some other piped input stream, an |
80 * is already connected to some other piped input stream, an |
81 * <code>IOException</code> is thrown. |
81 * {@code IOException} is thrown. |
82 * <p> |
82 * <p> |
83 * If <code>snk</code> is an unconnected piped input stream and |
83 * If {@code snk} is an unconnected piped input stream and |
84 * <code>src</code> is an unconnected piped output stream, they may |
84 * {@code src} is an unconnected piped output stream, they may |
85 * be connected by either the call: |
85 * be connected by either the call: |
86 * <blockquote><pre> |
86 * <blockquote><pre> |
87 * src.connect(snk)</pre></blockquote> |
87 * src.connect(snk)</pre></blockquote> |
88 * or the call: |
88 * or the call: |
89 * <blockquote><pre> |
89 * <blockquote><pre> |
90 * snk.connect(src)</pre></blockquote> |
90 * snk.connect(src)</pre></blockquote> |
91 * The two calls have the same effect. |
91 * The two calls have the same effect. |
92 * |
92 * |
93 * @param snk the piped input stream to connect to. |
93 * @param snk the piped input stream to connect to. |
94 * @exception IOException if an I/O error occurs. |
94 * @throws IOException if an I/O error occurs. |
95 */ |
95 */ |
96 public synchronized void connect(PipedInputStream snk) throws IOException { |
96 public synchronized void connect(PipedInputStream snk) throws IOException { |
97 if (snk == null) { |
97 if (snk == null) { |
98 throw new NullPointerException(); |
98 throw new NullPointerException(); |
99 } else if (sink != null || snk.connected) { |
99 } else if (sink != null || snk.connected) { |
104 snk.out = 0; |
104 snk.out = 0; |
105 snk.connected = true; |
105 snk.connected = true; |
106 } |
106 } |
107 |
107 |
108 /** |
108 /** |
109 * Writes the specified <code>byte</code> to the piped output stream. |
109 * Writes the specified {@code byte} to the piped output stream. |
110 * <p> |
110 * <p> |
111 * Implements the <code>write</code> method of <code>OutputStream</code>. |
111 * Implements the {@code write} method of {@code OutputStream}. |
112 * |
112 * |
113 * @param b the <code>byte</code> to be written. |
113 * @param b the {@code byte} to be written. |
114 * @exception IOException if the pipe is <a href=#BROKEN> broken</a>, |
114 * @throws IOException if the pipe is <a href=#BROKEN> broken</a>, |
115 * {@link #connect(java.io.PipedInputStream) unconnected}, |
115 * {@link #connect(java.io.PipedInputStream) unconnected}, |
116 * closed, or if an I/O error occurs. |
116 * closed, or if an I/O error occurs. |
117 */ |
117 */ |
118 public void write(int b) throws IOException { |
118 public void write(int b) throws IOException { |
119 if (sink == null) { |
119 if (sink == null) { |
121 } |
121 } |
122 sink.receive(b); |
122 sink.receive(b); |
123 } |
123 } |
124 |
124 |
125 /** |
125 /** |
126 * Writes <code>len</code> bytes from the specified byte array |
126 * Writes {@code len} bytes from the specified byte array |
127 * starting at offset <code>off</code> to this piped output stream. |
127 * starting at offset {@code off} to this piped output stream. |
128 * This method blocks until all the bytes are written to the output |
128 * This method blocks until all the bytes are written to the output |
129 * stream. |
129 * stream. |
130 * |
130 * |
131 * @param b the data. |
131 * @param b the data. |
132 * @param off the start offset in the data. |
132 * @param off the start offset in the data. |
133 * @param len the number of bytes to write. |
133 * @param len the number of bytes to write. |
134 * @exception IOException if the pipe is <a href=#BROKEN> broken</a>, |
134 * @throws IOException if the pipe is <a href=#BROKEN> broken</a>, |
135 * {@link #connect(java.io.PipedInputStream) unconnected}, |
135 * {@link #connect(java.io.PipedInputStream) unconnected}, |
136 * closed, or if an I/O error occurs. |
136 * closed, or if an I/O error occurs. |
137 */ |
137 */ |
138 public void write(byte b[], int off, int len) throws IOException { |
138 public void write(byte b[], int off, int len) throws IOException { |
139 if (sink == null) { |
139 if (sink == null) { |
152 /** |
152 /** |
153 * Flushes this output stream and forces any buffered output bytes |
153 * Flushes this output stream and forces any buffered output bytes |
154 * to be written out. |
154 * to be written out. |
155 * This will notify any readers that bytes are waiting in the pipe. |
155 * This will notify any readers that bytes are waiting in the pipe. |
156 * |
156 * |
157 * @exception IOException if an I/O error occurs. |
157 * @throws IOException if an I/O error occurs. |
158 */ |
158 */ |
159 public synchronized void flush() throws IOException { |
159 public synchronized void flush() throws IOException { |
160 if (sink != null) { |
160 if (sink != null) { |
161 synchronized (sink) { |
161 synchronized (sink) { |
162 sink.notifyAll(); |
162 sink.notifyAll(); |