1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/io/BufferedWriter.java Sat Sep 07 13:51:24 2013 +0200
1.3 @@ -0,0 +1,272 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.io;
1.30 +
1.31 +
1.32 +/**
1.33 + * Writes text to a character-output stream, buffering characters so as to
1.34 + * provide for the efficient writing of single characters, arrays, and strings.
1.35 + *
1.36 + * <p> The buffer size may be specified, or the default size may be accepted.
1.37 + * The default is large enough for most purposes.
1.38 + *
1.39 + * <p> A newLine() method is provided, which uses the platform's own notion of
1.40 + * line separator as defined by the system property <tt>line.separator</tt>.
1.41 + * Not all platforms use the newline character ('\n') to terminate lines.
1.42 + * Calling this method to terminate each output line is therefore preferred to
1.43 + * writing a newline character directly.
1.44 + *
1.45 + * <p> In general, a Writer sends its output immediately to the underlying
1.46 + * character or byte stream. Unless prompt output is required, it is advisable
1.47 + * to wrap a BufferedWriter around any Writer whose write() operations may be
1.48 + * costly, such as FileWriters and OutputStreamWriters. For example,
1.49 + *
1.50 + * <pre>
1.51 + * PrintWriter out
1.52 + * = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
1.53 + * </pre>
1.54 + *
1.55 + * will buffer the PrintWriter's output to the file. Without buffering, each
1.56 + * invocation of a print() method would cause characters to be converted into
1.57 + * bytes that would then be written immediately to the file, which can be very
1.58 + * inefficient.
1.59 + *
1.60 + * @see PrintWriter
1.61 + * @see FileWriter
1.62 + * @see OutputStreamWriter
1.63 + * @see java.nio.file.Files#newBufferedWriter
1.64 + *
1.65 + * @author Mark Reinhold
1.66 + * @since JDK1.1
1.67 + */
1.68 +
1.69 +public class BufferedWriter extends Writer {
1.70 +
1.71 + private Writer out;
1.72 +
1.73 + private char cb[];
1.74 + private int nChars, nextChar;
1.75 +
1.76 + private static int defaultCharBufferSize = 8192;
1.77 +
1.78 + /**
1.79 + * Line separator string. This is the value of the line.separator
1.80 + * property at the moment that the stream was created.
1.81 + */
1.82 + private String lineSeparator;
1.83 +
1.84 + /**
1.85 + * Creates a buffered character-output stream that uses a default-sized
1.86 + * output buffer.
1.87 + *
1.88 + * @param out A Writer
1.89 + */
1.90 + public BufferedWriter(Writer out) {
1.91 + this(out, defaultCharBufferSize);
1.92 + }
1.93 +
1.94 + /**
1.95 + * Creates a new buffered character-output stream that uses an output
1.96 + * buffer of the given size.
1.97 + *
1.98 + * @param out A Writer
1.99 + * @param sz Output-buffer size, a positive integer
1.100 + *
1.101 + * @exception IllegalArgumentException If sz is <= 0
1.102 + */
1.103 + public BufferedWriter(Writer out, int sz) {
1.104 + super(out);
1.105 + if (sz <= 0)
1.106 + throw new IllegalArgumentException("Buffer size <= 0");
1.107 + this.out = out;
1.108 + cb = new char[sz];
1.109 + nChars = sz;
1.110 + nextChar = 0;
1.111 +
1.112 + lineSeparator = java.security.AccessController.doPrivileged(
1.113 + new sun.security.action.GetPropertyAction("line.separator"));
1.114 + }
1.115 +
1.116 + /** Checks to make sure that the stream has not been closed */
1.117 + private void ensureOpen() throws IOException {
1.118 + if (out == null)
1.119 + throw new IOException("Stream closed");
1.120 + }
1.121 +
1.122 + /**
1.123 + * Flushes the output buffer to the underlying character stream, without
1.124 + * flushing the stream itself. This method is non-private only so that it
1.125 + * may be invoked by PrintStream.
1.126 + */
1.127 + void flushBuffer() throws IOException {
1.128 + synchronized (lock) {
1.129 + ensureOpen();
1.130 + if (nextChar == 0)
1.131 + return;
1.132 + out.write(cb, 0, nextChar);
1.133 + nextChar = 0;
1.134 + }
1.135 + }
1.136 +
1.137 + /**
1.138 + * Writes a single character.
1.139 + *
1.140 + * @exception IOException If an I/O error occurs
1.141 + */
1.142 + public void write(int c) throws IOException {
1.143 + synchronized (lock) {
1.144 + ensureOpen();
1.145 + if (nextChar >= nChars)
1.146 + flushBuffer();
1.147 + cb[nextChar++] = (char) c;
1.148 + }
1.149 + }
1.150 +
1.151 + /**
1.152 + * Our own little min method, to avoid loading java.lang.Math if we've run
1.153 + * out of file descriptors and we're trying to print a stack trace.
1.154 + */
1.155 + private int min(int a, int b) {
1.156 + if (a < b) return a;
1.157 + return b;
1.158 + }
1.159 +
1.160 + /**
1.161 + * Writes a portion of an array of characters.
1.162 + *
1.163 + * <p> Ordinarily this method stores characters from the given array into
1.164 + * this stream's buffer, flushing the buffer to the underlying stream as
1.165 + * needed. If the requested length is at least as large as the buffer,
1.166 + * however, then this method will flush the buffer and write the characters
1.167 + * directly to the underlying stream. Thus redundant
1.168 + * <code>BufferedWriter</code>s will not copy data unnecessarily.
1.169 + *
1.170 + * @param cbuf A character array
1.171 + * @param off Offset from which to start reading characters
1.172 + * @param len Number of characters to write
1.173 + *
1.174 + * @exception IOException If an I/O error occurs
1.175 + */
1.176 + public void write(char cbuf[], int off, int len) throws IOException {
1.177 + synchronized (lock) {
1.178 + ensureOpen();
1.179 + if ((off < 0) || (off > cbuf.length) || (len < 0) ||
1.180 + ((off + len) > cbuf.length) || ((off + len) < 0)) {
1.181 + throw new IndexOutOfBoundsException();
1.182 + } else if (len == 0) {
1.183 + return;
1.184 + }
1.185 +
1.186 + if (len >= nChars) {
1.187 + /* If the request length exceeds the size of the output buffer,
1.188 + flush the buffer and then write the data directly. In this
1.189 + way buffered streams will cascade harmlessly. */
1.190 + flushBuffer();
1.191 + out.write(cbuf, off, len);
1.192 + return;
1.193 + }
1.194 +
1.195 + int b = off, t = off + len;
1.196 + while (b < t) {
1.197 + int d = min(nChars - nextChar, t - b);
1.198 + System.arraycopy(cbuf, b, cb, nextChar, d);
1.199 + b += d;
1.200 + nextChar += d;
1.201 + if (nextChar >= nChars)
1.202 + flushBuffer();
1.203 + }
1.204 + }
1.205 + }
1.206 +
1.207 + /**
1.208 + * Writes a portion of a String.
1.209 + *
1.210 + * <p> If the value of the <tt>len</tt> parameter is negative then no
1.211 + * characters are written. This is contrary to the specification of this
1.212 + * method in the {@linkplain java.io.Writer#write(java.lang.String,int,int)
1.213 + * superclass}, which requires that an {@link IndexOutOfBoundsException} be
1.214 + * thrown.
1.215 + *
1.216 + * @param s String to be written
1.217 + * @param off Offset from which to start reading characters
1.218 + * @param len Number of characters to be written
1.219 + *
1.220 + * @exception IOException If an I/O error occurs
1.221 + */
1.222 + public void write(String s, int off, int len) throws IOException {
1.223 + synchronized (lock) {
1.224 + ensureOpen();
1.225 +
1.226 + int b = off, t = off + len;
1.227 + while (b < t) {
1.228 + int d = min(nChars - nextChar, t - b);
1.229 + s.getChars(b, b + d, cb, nextChar);
1.230 + b += d;
1.231 + nextChar += d;
1.232 + if (nextChar >= nChars)
1.233 + flushBuffer();
1.234 + }
1.235 + }
1.236 + }
1.237 +
1.238 + /**
1.239 + * Writes a line separator. The line separator string is defined by the
1.240 + * system property <tt>line.separator</tt>, and is not necessarily a single
1.241 + * newline ('\n') character.
1.242 + *
1.243 + * @exception IOException If an I/O error occurs
1.244 + */
1.245 + public void newLine() throws IOException {
1.246 + write(lineSeparator);
1.247 + }
1.248 +
1.249 + /**
1.250 + * Flushes the stream.
1.251 + *
1.252 + * @exception IOException If an I/O error occurs
1.253 + */
1.254 + public void flush() throws IOException {
1.255 + synchronized (lock) {
1.256 + flushBuffer();
1.257 + out.flush();
1.258 + }
1.259 + }
1.260 +
1.261 + public void close() throws IOException {
1.262 + synchronized (lock) {
1.263 + if (out == null) {
1.264 + return;
1.265 + }
1.266 + try {
1.267 + flushBuffer();
1.268 + } finally {
1.269 + out.close();
1.270 + out = null;
1.271 + cb = null;
1.272 + }
1.273 + }
1.274 + }
1.275 +}