jdk/src/share/classes/java/io/BufferedWriter.java
author alanb
Mon, 10 Jun 2013 12:58:32 +0100
changeset 18156 edb590d448c5
parent 10347 1c9efe1ec7d3
child 23010 6dadb192ad81
permissions -rw-r--r--
8016217: More javadoc warnings Reviewed-by: lancea, chegar, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8540
diff changeset
     2
 * Copyright (c) 1996, 2011, 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
 * Writes text to a character-output stream, buffering characters so as to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * provide for the efficient writing of single characters, arrays, and strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p> The buffer size may be specified, or the default size may be accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * The default is large enough for most purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p> A newLine() method is provided, which uses the platform's own notion of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * line separator as defined by the system property <tt>line.separator</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Not all platforms use the newline character ('\n') to terminate lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Calling this method to terminate each output line is therefore preferred to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * writing a newline character directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> In general, a Writer sends its output immediately to the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * character or byte stream.  Unless prompt output is required, it is advisable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * to wrap a BufferedWriter around any Writer whose write() operations may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * costly, such as FileWriters and OutputStreamWriters.  For example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * PrintWriter out
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * will buffer the PrintWriter's output to the file.  Without buffering, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * invocation of a print() method would cause characters to be converted into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * bytes that would then be written immediately to the file, which can be very
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * inefficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @see PrintWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @see FileWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see OutputStreamWriter
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    60
 * @see java.nio.file.Files#newBufferedWriter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
public class BufferedWriter extends Writer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private Writer out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private char cb[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private int nChars, nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static int defaultCharBufferSize = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Line separator string.  This is the value of the line.separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * property at the moment that the stream was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private String lineSeparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Creates a buffered character-output stream that uses a default-sized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param  out  A Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public BufferedWriter(Writer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        this(out, defaultCharBufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Creates a new buffered character-output stream that uses an output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * buffer of the given size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param  out  A Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param  sz   Output-buffer size, a positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 10347
diff changeset
    98
     * @exception  IllegalArgumentException  If {@code sz <= 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public BufferedWriter(Writer out, int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (sz <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new IllegalArgumentException("Buffer size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        cb = new char[sz];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        nChars = sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        nextChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        lineSeparator = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            new sun.security.action.GetPropertyAction("line.separator"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /** Checks to make sure that the stream has not been closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (out == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Flushes the output buffer to the underlying character stream, without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * flushing the stream itself.  This method is non-private only so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * may be invoked by PrintStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    void flushBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (nextChar == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            out.write(cb, 0, nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            nextChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * Writes a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void write(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            cb[nextChar++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Our own little min method, to avoid loading java.lang.Math if we've run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * out of file descriptors and we're trying to print a stack trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private int min(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (a < b) return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Writes a portion of an array of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * <p> Ordinarily this method stores characters from the given array into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * this stream's buffer, flushing the buffer to the underlying stream as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * needed.  If the requested length is at least as large as the buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * however, then this method will flush the buffer and write the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * directly to the underlying stream.  Thus redundant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * <code>BufferedWriter</code>s will not copy data unnecessarily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param  cbuf  A character array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param  off   Offset from which to start reading characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param  len   Number of characters to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public void write(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (len >= nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                /* If the request length exceeds the size of the output buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                   flush the buffer and then write the data directly.  In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                   way buffered streams will cascade harmlessly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                out.write(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            int b = off, t = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            while (b < t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                int d = min(nChars - nextChar, t - b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                System.arraycopy(cbuf, b, cb, nextChar, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                b += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                nextChar += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Writes a portion of a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <p> If the value of the <tt>len</tt> parameter is negative then no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * characters are written.  This is contrary to the specification of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * superclass}, which requires that an {@link IndexOutOfBoundsException} be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param  s     String to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param  off   Offset from which to start reading characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param  len   Number of characters to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public void write(String s, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            int b = off, t = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            while (b < t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                int d = min(nChars - nextChar, t - b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                s.getChars(b, b + d, cb, nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                b += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                nextChar += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * Writes a line separator.  The line separator string is defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * system property <tt>line.separator</tt>, and is not necessarily a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * newline ('\n') character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    public void newLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        write(lineSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Flushes the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
10347
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   258
    @SuppressWarnings("try")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        synchronized (lock) {
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   261
            if (out == null) {
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   262
                return;
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   263
            }
10347
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   264
            try (Writer w = out) {
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   265
                flushBuffer();
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   266
            } finally {
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   267
                out = null;
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   268
                cb = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
}