emul/compact/src/main/java/java/io/BufferedWriter.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Sep 2013 13:51:24 +0200
branchjdk7-b147
changeset 1258 724f3e1ea53e
permissions -rw-r--r--
Additional set of classes to make porting of lookup library more easier
jaroslav@1258
     1
/*
jaroslav@1258
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@1258
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1258
     4
 *
jaroslav@1258
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1258
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1258
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1258
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1258
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1258
    10
 *
jaroslav@1258
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1258
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1258
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1258
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1258
    15
 * accompanied this code).
jaroslav@1258
    16
 *
jaroslav@1258
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1258
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1258
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1258
    20
 *
jaroslav@1258
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1258
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1258
    23
 * questions.
jaroslav@1258
    24
 */
jaroslav@1258
    25
jaroslav@1258
    26
package java.io;
jaroslav@1258
    27
jaroslav@1258
    28
jaroslav@1258
    29
/**
jaroslav@1258
    30
 * Writes text to a character-output stream, buffering characters so as to
jaroslav@1258
    31
 * provide for the efficient writing of single characters, arrays, and strings.
jaroslav@1258
    32
 *
jaroslav@1258
    33
 * <p> The buffer size may be specified, or the default size may be accepted.
jaroslav@1258
    34
 * The default is large enough for most purposes.
jaroslav@1258
    35
 *
jaroslav@1258
    36
 * <p> A newLine() method is provided, which uses the platform's own notion of
jaroslav@1258
    37
 * line separator as defined by the system property <tt>line.separator</tt>.
jaroslav@1258
    38
 * Not all platforms use the newline character ('\n') to terminate lines.
jaroslav@1258
    39
 * Calling this method to terminate each output line is therefore preferred to
jaroslav@1258
    40
 * writing a newline character directly.
jaroslav@1258
    41
 *
jaroslav@1258
    42
 * <p> In general, a Writer sends its output immediately to the underlying
jaroslav@1258
    43
 * character or byte stream.  Unless prompt output is required, it is advisable
jaroslav@1258
    44
 * to wrap a BufferedWriter around any Writer whose write() operations may be
jaroslav@1258
    45
 * costly, such as FileWriters and OutputStreamWriters.  For example,
jaroslav@1258
    46
 *
jaroslav@1258
    47
 * <pre>
jaroslav@1258
    48
 * PrintWriter out
jaroslav@1258
    49
 *   = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
jaroslav@1258
    50
 * </pre>
jaroslav@1258
    51
 *
jaroslav@1258
    52
 * will buffer the PrintWriter's output to the file.  Without buffering, each
jaroslav@1258
    53
 * invocation of a print() method would cause characters to be converted into
jaroslav@1258
    54
 * bytes that would then be written immediately to the file, which can be very
jaroslav@1258
    55
 * inefficient.
jaroslav@1258
    56
 *
jaroslav@1258
    57
 * @see PrintWriter
jaroslav@1258
    58
 * @see FileWriter
jaroslav@1258
    59
 * @see OutputStreamWriter
jaroslav@1258
    60
 * @see java.nio.file.Files#newBufferedWriter
jaroslav@1258
    61
 *
jaroslav@1258
    62
 * @author      Mark Reinhold
jaroslav@1258
    63
 * @since       JDK1.1
jaroslav@1258
    64
 */
jaroslav@1258
    65
jaroslav@1258
    66
public class BufferedWriter extends Writer {
jaroslav@1258
    67
jaroslav@1258
    68
    private Writer out;
jaroslav@1258
    69
jaroslav@1258
    70
    private char cb[];
jaroslav@1258
    71
    private int nChars, nextChar;
jaroslav@1258
    72
jaroslav@1258
    73
    private static int defaultCharBufferSize = 8192;
jaroslav@1258
    74
jaroslav@1258
    75
    /**
jaroslav@1258
    76
     * Line separator string.  This is the value of the line.separator
jaroslav@1258
    77
     * property at the moment that the stream was created.
jaroslav@1258
    78
     */
jaroslav@1258
    79
    private String lineSeparator;
jaroslav@1258
    80
jaroslav@1258
    81
    /**
jaroslav@1258
    82
     * Creates a buffered character-output stream that uses a default-sized
jaroslav@1258
    83
     * output buffer.
jaroslav@1258
    84
     *
jaroslav@1258
    85
     * @param  out  A Writer
jaroslav@1258
    86
     */
jaroslav@1258
    87
    public BufferedWriter(Writer out) {
jaroslav@1258
    88
        this(out, defaultCharBufferSize);
jaroslav@1258
    89
    }
jaroslav@1258
    90
jaroslav@1258
    91
    /**
jaroslav@1258
    92
     * Creates a new buffered character-output stream that uses an output
jaroslav@1258
    93
     * buffer of the given size.
jaroslav@1258
    94
     *
jaroslav@1258
    95
     * @param  out  A Writer
jaroslav@1258
    96
     * @param  sz   Output-buffer size, a positive integer
jaroslav@1258
    97
     *
jaroslav@1258
    98
     * @exception  IllegalArgumentException  If sz is <= 0
jaroslav@1258
    99
     */
jaroslav@1258
   100
    public BufferedWriter(Writer out, int sz) {
jaroslav@1258
   101
        super(out);
jaroslav@1258
   102
        if (sz <= 0)
jaroslav@1258
   103
            throw new IllegalArgumentException("Buffer size <= 0");
jaroslav@1258
   104
        this.out = out;
jaroslav@1258
   105
        cb = new char[sz];
jaroslav@1258
   106
        nChars = sz;
jaroslav@1258
   107
        nextChar = 0;
jaroslav@1258
   108
jaroslav@1258
   109
        lineSeparator = java.security.AccessController.doPrivileged(
jaroslav@1258
   110
            new sun.security.action.GetPropertyAction("line.separator"));
jaroslav@1258
   111
    }
jaroslav@1258
   112
jaroslav@1258
   113
    /** Checks to make sure that the stream has not been closed */
jaroslav@1258
   114
    private void ensureOpen() throws IOException {
jaroslav@1258
   115
        if (out == null)
jaroslav@1258
   116
            throw new IOException("Stream closed");
jaroslav@1258
   117
    }
jaroslav@1258
   118
jaroslav@1258
   119
    /**
jaroslav@1258
   120
     * Flushes the output buffer to the underlying character stream, without
jaroslav@1258
   121
     * flushing the stream itself.  This method is non-private only so that it
jaroslav@1258
   122
     * may be invoked by PrintStream.
jaroslav@1258
   123
     */
jaroslav@1258
   124
    void flushBuffer() throws IOException {
jaroslav@1258
   125
        synchronized (lock) {
jaroslav@1258
   126
            ensureOpen();
jaroslav@1258
   127
            if (nextChar == 0)
jaroslav@1258
   128
                return;
jaroslav@1258
   129
            out.write(cb, 0, nextChar);
jaroslav@1258
   130
            nextChar = 0;
jaroslav@1258
   131
        }
jaroslav@1258
   132
    }
jaroslav@1258
   133
jaroslav@1258
   134
    /**
jaroslav@1258
   135
     * Writes a single character.
jaroslav@1258
   136
     *
jaroslav@1258
   137
     * @exception  IOException  If an I/O error occurs
jaroslav@1258
   138
     */
jaroslav@1258
   139
    public void write(int c) throws IOException {
jaroslav@1258
   140
        synchronized (lock) {
jaroslav@1258
   141
            ensureOpen();
jaroslav@1258
   142
            if (nextChar >= nChars)
jaroslav@1258
   143
                flushBuffer();
jaroslav@1258
   144
            cb[nextChar++] = (char) c;
jaroslav@1258
   145
        }
jaroslav@1258
   146
    }
jaroslav@1258
   147
jaroslav@1258
   148
    /**
jaroslav@1258
   149
     * Our own little min method, to avoid loading java.lang.Math if we've run
jaroslav@1258
   150
     * out of file descriptors and we're trying to print a stack trace.
jaroslav@1258
   151
     */
jaroslav@1258
   152
    private int min(int a, int b) {
jaroslav@1258
   153
        if (a < b) return a;
jaroslav@1258
   154
        return b;
jaroslav@1258
   155
    }
jaroslav@1258
   156
jaroslav@1258
   157
    /**
jaroslav@1258
   158
     * Writes a portion of an array of characters.
jaroslav@1258
   159
     *
jaroslav@1258
   160
     * <p> Ordinarily this method stores characters from the given array into
jaroslav@1258
   161
     * this stream's buffer, flushing the buffer to the underlying stream as
jaroslav@1258
   162
     * needed.  If the requested length is at least as large as the buffer,
jaroslav@1258
   163
     * however, then this method will flush the buffer and write the characters
jaroslav@1258
   164
     * directly to the underlying stream.  Thus redundant
jaroslav@1258
   165
     * <code>BufferedWriter</code>s will not copy data unnecessarily.
jaroslav@1258
   166
     *
jaroslav@1258
   167
     * @param  cbuf  A character array
jaroslav@1258
   168
     * @param  off   Offset from which to start reading characters
jaroslav@1258
   169
     * @param  len   Number of characters to write
jaroslav@1258
   170
     *
jaroslav@1258
   171
     * @exception  IOException  If an I/O error occurs
jaroslav@1258
   172
     */
jaroslav@1258
   173
    public void write(char cbuf[], int off, int len) throws IOException {
jaroslav@1258
   174
        synchronized (lock) {
jaroslav@1258
   175
            ensureOpen();
jaroslav@1258
   176
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
jaroslav@1258
   177
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
jaroslav@1258
   178
                throw new IndexOutOfBoundsException();
jaroslav@1258
   179
            } else if (len == 0) {
jaroslav@1258
   180
                return;
jaroslav@1258
   181
            }
jaroslav@1258
   182
jaroslav@1258
   183
            if (len >= nChars) {
jaroslav@1258
   184
                /* If the request length exceeds the size of the output buffer,
jaroslav@1258
   185
                   flush the buffer and then write the data directly.  In this
jaroslav@1258
   186
                   way buffered streams will cascade harmlessly. */
jaroslav@1258
   187
                flushBuffer();
jaroslav@1258
   188
                out.write(cbuf, off, len);
jaroslav@1258
   189
                return;
jaroslav@1258
   190
            }
jaroslav@1258
   191
jaroslav@1258
   192
            int b = off, t = off + len;
jaroslav@1258
   193
            while (b < t) {
jaroslav@1258
   194
                int d = min(nChars - nextChar, t - b);
jaroslav@1258
   195
                System.arraycopy(cbuf, b, cb, nextChar, d);
jaroslav@1258
   196
                b += d;
jaroslav@1258
   197
                nextChar += d;
jaroslav@1258
   198
                if (nextChar >= nChars)
jaroslav@1258
   199
                    flushBuffer();
jaroslav@1258
   200
            }
jaroslav@1258
   201
        }
jaroslav@1258
   202
    }
jaroslav@1258
   203
jaroslav@1258
   204
    /**
jaroslav@1258
   205
     * Writes a portion of a String.
jaroslav@1258
   206
     *
jaroslav@1258
   207
     * <p> If the value of the <tt>len</tt> parameter is negative then no
jaroslav@1258
   208
     * characters are written.  This is contrary to the specification of this
jaroslav@1258
   209
     * method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
jaroslav@1258
   210
     * superclass}, which requires that an {@link IndexOutOfBoundsException} be
jaroslav@1258
   211
     * thrown.
jaroslav@1258
   212
     *
jaroslav@1258
   213
     * @param  s     String to be written
jaroslav@1258
   214
     * @param  off   Offset from which to start reading characters
jaroslav@1258
   215
     * @param  len   Number of characters to be written
jaroslav@1258
   216
     *
jaroslav@1258
   217
     * @exception  IOException  If an I/O error occurs
jaroslav@1258
   218
     */
jaroslav@1258
   219
    public void write(String s, int off, int len) throws IOException {
jaroslav@1258
   220
        synchronized (lock) {
jaroslav@1258
   221
            ensureOpen();
jaroslav@1258
   222
jaroslav@1258
   223
            int b = off, t = off + len;
jaroslav@1258
   224
            while (b < t) {
jaroslav@1258
   225
                int d = min(nChars - nextChar, t - b);
jaroslav@1258
   226
                s.getChars(b, b + d, cb, nextChar);
jaroslav@1258
   227
                b += d;
jaroslav@1258
   228
                nextChar += d;
jaroslav@1258
   229
                if (nextChar >= nChars)
jaroslav@1258
   230
                    flushBuffer();
jaroslav@1258
   231
            }
jaroslav@1258
   232
        }
jaroslav@1258
   233
    }
jaroslav@1258
   234
jaroslav@1258
   235
    /**
jaroslav@1258
   236
     * Writes a line separator.  The line separator string is defined by the
jaroslav@1258
   237
     * system property <tt>line.separator</tt>, and is not necessarily a single
jaroslav@1258
   238
     * newline ('\n') character.
jaroslav@1258
   239
     *
jaroslav@1258
   240
     * @exception  IOException  If an I/O error occurs
jaroslav@1258
   241
     */
jaroslav@1258
   242
    public void newLine() throws IOException {
jaroslav@1258
   243
        write(lineSeparator);
jaroslav@1258
   244
    }
jaroslav@1258
   245
jaroslav@1258
   246
    /**
jaroslav@1258
   247
     * Flushes the stream.
jaroslav@1258
   248
     *
jaroslav@1258
   249
     * @exception  IOException  If an I/O error occurs
jaroslav@1258
   250
     */
jaroslav@1258
   251
    public void flush() throws IOException {
jaroslav@1258
   252
        synchronized (lock) {
jaroslav@1258
   253
            flushBuffer();
jaroslav@1258
   254
            out.flush();
jaroslav@1258
   255
        }
jaroslav@1258
   256
    }
jaroslav@1258
   257
jaroslav@1258
   258
    public void close() throws IOException {
jaroslav@1258
   259
        synchronized (lock) {
jaroslav@1258
   260
            if (out == null) {
jaroslav@1258
   261
                return;
jaroslav@1258
   262
            }
jaroslav@1258
   263
            try {
jaroslav@1258
   264
                flushBuffer();
jaroslav@1258
   265
            } finally {
jaroslav@1258
   266
                out.close();
jaroslav@1258
   267
                out = null;
jaroslav@1258
   268
                cb = null;
jaroslav@1258
   269
            }
jaroslav@1258
   270
        }
jaroslav@1258
   271
    }
jaroslav@1258
   272
}