author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58288 | 48e480e56aad |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
2 |
* Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
28 |
/** |
|
29 |
* This class is the superclass of all classes that filter output |
|
30 |
* streams. These streams sit on top of an already existing output |
|
31 |
* stream (the <i>underlying</i> output stream) which it uses as its |
|
32 |
* basic sink of data, but possibly transforming the data along the |
|
33 |
* way or providing additional functionality. |
|
34 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
35 |
* The class {@code FilterOutputStream} itself simply overrides |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
36 |
* all methods of {@code OutputStream} with versions that pass |
2 | 37 |
* all requests to the underlying output stream. Subclasses of |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
38 |
* {@code FilterOutputStream} may further override some of these |
2 | 39 |
* methods as well as provide additional methods and fields. |
40 |
* |
|
41 |
* @author Jonathan Payne |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
10347
diff
changeset
|
42 |
* @since 1.0 |
2 | 43 |
*/ |
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
44 |
public class FilterOutputStream extends OutputStream { |
2 | 45 |
/** |
46 |
* The underlying output stream to be filtered. |
|
47 |
*/ |
|
48 |
protected OutputStream out; |
|
49 |
||
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
50 |
/** |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
51 |
* Whether the stream is closed; implicitly initialized to false. |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
52 |
*/ |
46045 | 53 |
private volatile boolean closed; |
54 |
||
55 |
/** |
|
56 |
* Object used to prevent a race on the 'closed' instance variable. |
|
57 |
*/ |
|
58 |
private final Object closeLock = new Object(); |
|
28292
140e7e529be6
8054565: FilterOutputStream.close may throw IOException if called twice and underlying flush or close fails
chegar
parents:
25859
diff
changeset
|
59 |
|
2 | 60 |
/** |
61 |
* Creates an output stream filter built on top of the specified |
|
62 |
* underlying output stream. |
|
63 |
* |
|
64 |
* @param out the underlying output stream to be assigned to |
|
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31421
diff
changeset
|
65 |
* the field {@code this.out} for later use, or |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
66 |
* {@code null} if this instance is to be |
2 | 67 |
* created without an underlying stream. |
68 |
*/ |
|
69 |
public FilterOutputStream(OutputStream out) { |
|
70 |
this.out = out; |
|
71 |
} |
|
72 |
||
73 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
74 |
* Writes the specified {@code byte} to this output stream. |
2 | 75 |
* <p> |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
76 |
* The {@code write} method of {@code FilterOutputStream} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
77 |
* calls the {@code write} method of its underlying output stream, |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31421
diff
changeset
|
78 |
* that is, it performs {@code out.write(b)}. |
2 | 79 |
* <p> |
32033
bf24e33c7919
8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents:
31421
diff
changeset
|
80 |
* Implements the abstract {@code write} method of {@code OutputStream}. |
2 | 81 |
* |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
82 |
* @param b the {@code byte}. |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
83 |
* @throws IOException if an I/O error occurs. |
2 | 84 |
*/ |
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
85 |
@Override |
2 | 86 |
public void write(int b) throws IOException { |
87 |
out.write(b); |
|
88 |
} |
|
89 |
||
90 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
91 |
* Writes {@code b.length} bytes to this output stream. |
2 | 92 |
* <p> |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
93 |
* The {@code write} method of {@code FilterOutputStream} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
94 |
* calls its {@code write} method of three arguments with the |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
95 |
* arguments {@code b}, {@code 0}, and |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
96 |
* {@code b.length}. |
2 | 97 |
* <p> |
98 |
* Note that this method does not call the one-argument |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
99 |
* {@code write} method of its underlying output stream with |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
100 |
* the single argument {@code b}. |
2 | 101 |
* |
102 |
* @param b the data to be written. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
103 |
* @throws IOException if an I/O error occurs. |
2 | 104 |
* @see java.io.FilterOutputStream#write(byte[], int, int) |
105 |
*/ |
|
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
106 |
@Override |
2 | 107 |
public void write(byte b[]) throws IOException { |
108 |
write(b, 0, b.length); |
|
109 |
} |
|
110 |
||
111 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
112 |
* Writes {@code len} bytes from the specified |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
113 |
* {@code byte} array starting at offset {@code off} to |
2 | 114 |
* this output stream. |
115 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
116 |
* The {@code write} method of {@code FilterOutputStream} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
117 |
* calls the {@code write} method of one argument on each |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
118 |
* {@code byte} to output. |
2 | 119 |
* <p> |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
120 |
* Note that this method does not call the {@code write} method |
43794
497288c158bf
8173094: Error in API documentation for SwingWorker
coffeys
parents:
32033
diff
changeset
|
121 |
* of its underlying output stream with the same arguments. Subclasses |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
122 |
* of {@code FilterOutputStream} should provide a more efficient |
2 | 123 |
* implementation of this method. |
124 |
* |
|
125 |
* @param b the data. |
|
126 |
* @param off the start offset in the data. |
|
127 |
* @param len the number of bytes to write. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
128 |
* @throws IOException if an I/O error occurs. |
2 | 129 |
* @see java.io.FilterOutputStream#write(int) |
130 |
*/ |
|
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
131 |
@Override |
2 | 132 |
public void write(byte b[], int off, int len) throws IOException { |
133 |
if ((off | len | (b.length - (len + off)) | (off + len)) < 0) |
|
134 |
throw new IndexOutOfBoundsException(); |
|
135 |
||
136 |
for (int i = 0 ; i < len ; i++) { |
|
137 |
write(b[off + i]); |
|
138 |
} |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Flushes this output stream and forces any buffered output bytes |
|
143 |
* to be written out to the stream. |
|
144 |
* <p> |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
145 |
* The {@code flush} method of {@code FilterOutputStream} |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
146 |
* calls the {@code flush} method of its underlying output stream. |
2 | 147 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
148 |
* @throws IOException if an I/O error occurs. |
2 | 149 |
* @see java.io.FilterOutputStream#out |
150 |
*/ |
|
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
151 |
@Override |
2 | 152 |
public void flush() throws IOException { |
153 |
out.flush(); |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Closes this output stream and releases any system resources |
|
158 |
* associated with the stream. |
|
159 |
* <p> |
|
28292
140e7e529be6
8054565: FilterOutputStream.close may throw IOException if called twice and underlying flush or close fails
chegar
parents:
25859
diff
changeset
|
160 |
* When not already closed, the {@code close} method of {@code |
140e7e529be6
8054565: FilterOutputStream.close may throw IOException if called twice and underlying flush or close fails
chegar
parents:
25859
diff
changeset
|
161 |
* FilterOutputStream} calls its {@code flush} method, and then |
140e7e529be6
8054565: FilterOutputStream.close may throw IOException if called twice and underlying flush or close fails
chegar
parents:
25859
diff
changeset
|
162 |
* calls the {@code close} method of its underlying output stream. |
2 | 163 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
55741
diff
changeset
|
164 |
* @throws IOException if an I/O error occurs. |
2 | 165 |
* @see java.io.FilterOutputStream#flush() |
166 |
* @see java.io.FilterOutputStream#out |
|
167 |
*/ |
|
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
168 |
@Override |
2 | 169 |
public void close() throws IOException { |
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
170 |
if (closed) { |
28292
140e7e529be6
8054565: FilterOutputStream.close may throw IOException if called twice and underlying flush or close fails
chegar
parents:
25859
diff
changeset
|
171 |
return; |
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
172 |
} |
46045 | 173 |
synchronized (closeLock) { |
174 |
if (closed) { |
|
175 |
return; |
|
176 |
} |
|
177 |
closed = true; |
|
178 |
} |
|
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
179 |
|
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
180 |
Throwable flushException = null; |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
181 |
try { |
10347
1c9efe1ec7d3
7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents:
9035
diff
changeset
|
182 |
flush(); |
31421
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
183 |
} catch (Throwable e) { |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
184 |
flushException = e; |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
185 |
throw e; |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
186 |
} finally { |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
187 |
if (flushException == null) { |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
188 |
out.close(); |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
189 |
} else { |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
190 |
try { |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
191 |
out.close(); |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
192 |
} catch (Throwable closeException) { |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
193 |
// evaluate possible precedence of flushException over closeException |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
194 |
if ((flushException instanceof ThreadDeath) && |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
195 |
!(closeException instanceof ThreadDeath)) { |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
196 |
flushException.addSuppressed(closeException); |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
197 |
throw (ThreadDeath) flushException; |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
198 |
} |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
199 |
|
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
200 |
if (flushException != closeException) { |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
201 |
closeException.addSuppressed(flushException); |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
202 |
} |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
203 |
|
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
204 |
throw closeException; |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
205 |
} |
d6ddd21f6df8
8042377: BufferedWriter and FilteredOutputStream.close throw IAE if flush and close throw equal exceptions
bpb
parents:
28292
diff
changeset
|
206 |
} |
2 | 207 |
} |
208 |
} |
|
209 |
} |