src/java.base/share/classes/java/io/BufferedWriter.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 47216 71c04702a3d5
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
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: 47216
diff changeset
     2
 * Copyright (c) 1996, 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
/**
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
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 28422
diff changeset
    37
 * line separator as defined by the system property {@code line.separator}.
2
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
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    63
 * @since       1.1
2
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
     * Creates a buffered character-output stream that uses a default-sized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param  out  A Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public BufferedWriter(Writer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        this(out, defaultCharBufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Creates a new buffered character-output stream that uses an output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * buffer of the given size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param  out  A Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param  sz   Output-buffer size, a positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    92
     * @throws     IllegalArgumentException  If {@code sz <= 0}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public BufferedWriter(Writer out, int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (sz <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IllegalArgumentException("Buffer size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        cb = new char[sz];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        nChars = sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        nextChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /** Checks to make sure that the stream has not been closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (out == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Flushes the output buffer to the underlying character stream, without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * flushing the stream itself.  This method is non-private only so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * may be invoked by PrintStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    void flushBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            if (nextChar == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            out.write(cb, 0, nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            nextChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Writes a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   128
     * @throws     IOException  If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public void write(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            cb[nextChar++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Our own little min method, to avoid loading java.lang.Math if we've run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * out of file descriptors and we're trying to print a stack trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private int min(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (a < b) return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return b;
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
     * Writes a portion of an array of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * <p> Ordinarily this method stores characters from the given array into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * this stream's buffer, flushing the buffer to the underlying stream as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * needed.  If the requested length is at least as large as the buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * however, then this method will flush the buffer and write the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * directly to the underlying stream.  Thus redundant
38373
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   156
     * {@code BufferedWriter}s will not copy data unnecessarily.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * @param  cbuf  A character array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @param  off   Offset from which to start reading characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @param  len   Number of characters to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
38373
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   162
     * @throws  IndexOutOfBoundsException
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   163
     *          If {@code off} is negative, or {@code len} is negative,
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   164
     *          or {@code off + len} is negative or greater than the length
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   165
     *          of the given array
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   166
     *
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   167
     * @throws  IOException  If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void write(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (len >= nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                /* If the request length exceeds the size of the output buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                   flush the buffer and then write the data directly.  In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                   way buffered streams will cascade harmlessly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                out.write(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            int b = off, t = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            while (b < t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                int d = min(nChars - nextChar, t - b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                System.arraycopy(cbuf, b, cb, nextChar, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                b += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                nextChar += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Writes a portion of a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
38373
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   203
     * @implSpec
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   204
     * While the specification of this method in the
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   205
     * {@linkplain java.io.Writer#write(java.lang.String,int,int) superclass}
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   206
     * recommends that an {@link IndexOutOfBoundsException} be thrown
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   207
     * if {@code len} is negative or {@code off + len} is negative,
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   208
     * the implementation in this class does not throw such an exception in
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   209
     * these cases but instead simply writes no characters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param  s     String to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param  off   Offset from which to start reading characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param  len   Number of characters to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
38373
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   215
     * @throws  IndexOutOfBoundsException
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   216
     *          If {@code off} is negative,
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   217
     *          or {@code off + len} is greater than the length
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   218
     *          of the given string
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   219
     *
21f4f5eee7cc 8130679: Writer/StringWriter.write methods do not specify index out bounds
bpb
parents: 32033
diff changeset
   220
     * @throws  IOException  If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void write(String s, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            int b = off, t = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            while (b < t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                int d = min(nChars - nextChar, t - b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                s.getChars(b, b + d, cb, nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                b += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                nextChar += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                    flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Writes a line separator.  The line separator string is defined by the
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 28422
diff changeset
   240
     * system property {@code line.separator}, and is not necessarily a single
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * newline ('\n') character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   243
     * @throws     IOException  If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public void newLine() throws IOException {
28422
a571480286c5 8068498: Remove constructor dependency on line.separator from PrintWriter and BufferedWriter
redestad
parents: 25859
diff changeset
   246
        write(System.lineSeparator());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Flushes the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   252
     * @throws     IOException  If an I/O error occurs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
10347
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   261
    @SuppressWarnings("try")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        synchronized (lock) {
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   264
            if (out == null) {
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   265
                return;
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   266
            }
10347
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   267
            try (Writer w = out) {
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   268
                flushBuffer();
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   269
            } finally {
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   270
                out = null;
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   271
                cb = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
}