src/java.base/share/classes/java/io/FilterOutputStream.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: 55741
diff changeset
     2
 * Copyright (c) 1994, 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
 * This class is the superclass of all classes that filter output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * streams. These streams sit on top of an already existing output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * stream (the <i>underlying</i> output stream) which it uses as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * basic sink of data, but possibly transforming the data along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * way or providing additional functionality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * methods as well as provide additional methods and fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * The underlying output stream to be filtered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    protected OutputStream out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
    53
    private volatile boolean closed;
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
    54
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
    55
    /**
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
    56
     * Object used to prevent a race on the 'closed' instance variable.
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
    57
     */
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Creates an output stream filter built on top of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * underlying output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *                created without an underlying stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public FilterOutputStream(OutputStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public void write(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        out.write(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see        java.io.FilterOutputStream#write(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void write(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        write(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * this output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @param      b     the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param      off   the start offset in the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @see        java.io.FilterOutputStream#write(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public void write(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if ((off | len | (b.length - (len + off)) | (off + len)) < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        for (int i = 0 ; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            write(b[off + i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Flushes this output stream and forces any buffered output bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * to be written out to the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Closes this output stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see        java.io.FilterOutputStream#flush()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see        java.io.FilterOutputStream#out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   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
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
   173
        synchronized (closeLock) {
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
   174
            if (closed) {
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
   175
                return;
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
   176
            }
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
   177
            closed = true;
ef191ce8f33f 8185092: Data race in FilterOutputStream.close
bpb
parents: 43794
diff changeset
   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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
}