jdk/src/share/classes/java/io/BufferedWriter.java
author mchung
Thu, 12 Mar 2009 10:27:22 -0700
changeset 2277 445a331b4a8b
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6810254: Lazily instantiate the shared secret access objects Summary: Register the shutdown hooks only when needed and remove JavaIODeleteOnExitAccess Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2005 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
public class BufferedWriter extends Writer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private Writer out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private char cb[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private int nChars, nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static int defaultCharBufferSize = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Line separator string.  This is the value of the line.separator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * property at the moment that the stream was created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private String lineSeparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Creates a buffered character-output stream that uses a default-sized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param  out  A Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public BufferedWriter(Writer out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        this(out, defaultCharBufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Creates a new buffered character-output stream that uses an output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * buffer of the given size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param  out  A Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param  sz   Output-buffer size, a positive integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception  IllegalArgumentException  If sz is <= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public BufferedWriter(Writer out, int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        super(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (sz <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new IllegalArgumentException("Buffer size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        this.out = out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        cb = new char[sz];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        nChars = sz;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        nextChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        lineSeparator = java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            new sun.security.action.GetPropertyAction("line.separator"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /** Checks to make sure that the stream has not been closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (out == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Flushes the output buffer to the underlying character stream, without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * flushing the stream itself.  This method is non-private only so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * may be invoked by PrintStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    void flushBuffer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (nextChar == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            out.write(cb, 0, nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            nextChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
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
     * Writes a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void write(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            cb[nextChar++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
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
     * Our own little min method, to avoid loading java.lang.Math if we've run
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * out of file descriptors and we're trying to print a stack trace.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private int min(int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (a < b) return a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return b;
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
     * Writes a portion of an array of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p> Ordinarily this method stores characters from the given array into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * this stream's buffer, flushing the buffer to the underlying stream as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * needed.  If the requested length is at least as large as the buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * however, then this method will flush the buffer and write the characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * directly to the underlying stream.  Thus redundant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <code>BufferedWriter</code>s will not copy data unnecessarily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param  cbuf  A character array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @param  off   Offset from which to start reading characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @param  len   Number of characters to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public void write(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            if (len >= nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                /* If the request length exceeds the size of the output buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                   flush the buffer and then write the data directly.  In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                   way buffered streams will cascade harmlessly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                out.write(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            int b = off, t = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            while (b < t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                int d = min(nChars - nextChar, t - b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                System.arraycopy(cbuf, b, cb, nextChar, d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                b += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                nextChar += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            }
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
     * Writes a portion of a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * <p> If the value of the <tt>len</tt> parameter is negative then no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * characters are written.  This is contrary to the specification of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * superclass}, which requires that an {@link IndexOutOfBoundsException} be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * @param  s     String to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param  off   Offset from which to start reading characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param  len   Number of characters to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    public void write(String s, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            int b = off, t = off + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            while (b < t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                int d = min(nChars - nextChar, t - b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                s.getChars(b, b + d, cb, nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                b += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                nextChar += d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                    flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
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
     * Writes a line separator.  The line separator string is defined by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * system property <tt>line.separator</tt>, and is not necessarily a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * newline ('\n') character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    public void newLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        write(lineSeparator);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * Flushes the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public void flush() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            out.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (out == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                flushBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                out = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                cb = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
}