emul/compact/src/main/java/java/io/Writer.java
branchjdk7-b147
changeset 1258 724f3e1ea53e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/compact/src/main/java/java/io/Writer.java	Sat Sep 07 13:51:24 2013 +0200
     1.3 @@ -0,0 +1,325 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2005, 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 + * Abstract class for writing to character streams.  The only methods that a
    1.34 + * subclass must implement are write(char[], int, int), flush(), and close().
    1.35 + * Most subclasses, however, will override some of the methods defined here in
    1.36 + * order to provide higher efficiency, additional functionality, or both.
    1.37 + *
    1.38 + * @see Writer
    1.39 + * @see   BufferedWriter
    1.40 + * @see   CharArrayWriter
    1.41 + * @see   FilterWriter
    1.42 + * @see   OutputStreamWriter
    1.43 + * @see     FileWriter
    1.44 + * @see   PipedWriter
    1.45 + * @see   PrintWriter
    1.46 + * @see   StringWriter
    1.47 + * @see Reader
    1.48 + *
    1.49 + * @author      Mark Reinhold
    1.50 + * @since       JDK1.1
    1.51 + */
    1.52 +
    1.53 +public abstract class Writer implements Appendable, Closeable, Flushable {
    1.54 +
    1.55 +    /**
    1.56 +     * Temporary buffer used to hold writes of strings and single characters
    1.57 +     */
    1.58 +    private char[] writeBuffer;
    1.59 +
    1.60 +    /**
    1.61 +     * Size of writeBuffer, must be >= 1
    1.62 +     */
    1.63 +    private final int writeBufferSize = 1024;
    1.64 +
    1.65 +    /**
    1.66 +     * The object used to synchronize operations on this stream.  For
    1.67 +     * efficiency, a character-stream object may use an object other than
    1.68 +     * itself to protect critical sections.  A subclass should therefore use
    1.69 +     * the object in this field rather than <tt>this</tt> or a synchronized
    1.70 +     * method.
    1.71 +     */
    1.72 +    protected Object lock;
    1.73 +
    1.74 +    /**
    1.75 +     * Creates a new character-stream writer whose critical sections will
    1.76 +     * synchronize on the writer itself.
    1.77 +     */
    1.78 +    protected Writer() {
    1.79 +        this.lock = this;
    1.80 +    }
    1.81 +
    1.82 +    /**
    1.83 +     * Creates a new character-stream writer whose critical sections will
    1.84 +     * synchronize on the given object.
    1.85 +     *
    1.86 +     * @param  lock
    1.87 +     *         Object to synchronize on
    1.88 +     */
    1.89 +    protected Writer(Object lock) {
    1.90 +        if (lock == null) {
    1.91 +            throw new NullPointerException();
    1.92 +        }
    1.93 +        this.lock = lock;
    1.94 +    }
    1.95 +
    1.96 +    /**
    1.97 +     * Writes a single character.  The character to be written is contained in
    1.98 +     * the 16 low-order bits of the given integer value; the 16 high-order bits
    1.99 +     * are ignored.
   1.100 +     *
   1.101 +     * <p> Subclasses that intend to support efficient single-character output
   1.102 +     * should override this method.
   1.103 +     *
   1.104 +     * @param  c
   1.105 +     *         int specifying a character to be written
   1.106 +     *
   1.107 +     * @throws  IOException
   1.108 +     *          If an I/O error occurs
   1.109 +     */
   1.110 +    public void write(int c) throws IOException {
   1.111 +        synchronized (lock) {
   1.112 +            if (writeBuffer == null){
   1.113 +                writeBuffer = new char[writeBufferSize];
   1.114 +            }
   1.115 +            writeBuffer[0] = (char) c;
   1.116 +            write(writeBuffer, 0, 1);
   1.117 +        }
   1.118 +    }
   1.119 +
   1.120 +    /**
   1.121 +     * Writes an array of characters.
   1.122 +     *
   1.123 +     * @param  cbuf
   1.124 +     *         Array of characters to be written
   1.125 +     *
   1.126 +     * @throws  IOException
   1.127 +     *          If an I/O error occurs
   1.128 +     */
   1.129 +    public void write(char cbuf[]) throws IOException {
   1.130 +        write(cbuf, 0, cbuf.length);
   1.131 +    }
   1.132 +
   1.133 +    /**
   1.134 +     * Writes a portion of an array of characters.
   1.135 +     *
   1.136 +     * @param  cbuf
   1.137 +     *         Array of characters
   1.138 +     *
   1.139 +     * @param  off
   1.140 +     *         Offset from which to start writing characters
   1.141 +     *
   1.142 +     * @param  len
   1.143 +     *         Number of characters to write
   1.144 +     *
   1.145 +     * @throws  IOException
   1.146 +     *          If an I/O error occurs
   1.147 +     */
   1.148 +    abstract public void write(char cbuf[], int off, int len) throws IOException;
   1.149 +
   1.150 +    /**
   1.151 +     * Writes a string.
   1.152 +     *
   1.153 +     * @param  str
   1.154 +     *         String to be written
   1.155 +     *
   1.156 +     * @throws  IOException
   1.157 +     *          If an I/O error occurs
   1.158 +     */
   1.159 +    public void write(String str) throws IOException {
   1.160 +        write(str, 0, str.length());
   1.161 +    }
   1.162 +
   1.163 +    /**
   1.164 +     * Writes a portion of a string.
   1.165 +     *
   1.166 +     * @param  str
   1.167 +     *         A String
   1.168 +     *
   1.169 +     * @param  off
   1.170 +     *         Offset from which to start writing characters
   1.171 +     *
   1.172 +     * @param  len
   1.173 +     *         Number of characters to write
   1.174 +     *
   1.175 +     * @throws  IndexOutOfBoundsException
   1.176 +     *          If <tt>off</tt> is negative, or <tt>len</tt> is negative,
   1.177 +     *          or <tt>off+len</tt> is negative or greater than the length
   1.178 +     *          of the given string
   1.179 +     *
   1.180 +     * @throws  IOException
   1.181 +     *          If an I/O error occurs
   1.182 +     */
   1.183 +    public void write(String str, int off, int len) throws IOException {
   1.184 +        synchronized (lock) {
   1.185 +            char cbuf[];
   1.186 +            if (len <= writeBufferSize) {
   1.187 +                if (writeBuffer == null) {
   1.188 +                    writeBuffer = new char[writeBufferSize];
   1.189 +                }
   1.190 +                cbuf = writeBuffer;
   1.191 +            } else {    // Don't permanently allocate very large buffers.
   1.192 +                cbuf = new char[len];
   1.193 +            }
   1.194 +            str.getChars(off, (off + len), cbuf, 0);
   1.195 +            write(cbuf, 0, len);
   1.196 +        }
   1.197 +    }
   1.198 +
   1.199 +    /**
   1.200 +     * Appends the specified character sequence to this writer.
   1.201 +     *
   1.202 +     * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
   1.203 +     * behaves in exactly the same way as the invocation
   1.204 +     *
   1.205 +     * <pre>
   1.206 +     *     out.write(csq.toString()) </pre>
   1.207 +     *
   1.208 +     * <p> Depending on the specification of <tt>toString</tt> for the
   1.209 +     * character sequence <tt>csq</tt>, the entire sequence may not be
   1.210 +     * appended. For instance, invoking the <tt>toString</tt> method of a
   1.211 +     * character buffer will return a subsequence whose content depends upon
   1.212 +     * the buffer's position and limit.
   1.213 +     *
   1.214 +     * @param  csq
   1.215 +     *         The character sequence to append.  If <tt>csq</tt> is
   1.216 +     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
   1.217 +     *         appended to this writer.
   1.218 +     *
   1.219 +     * @return  This writer
   1.220 +     *
   1.221 +     * @throws  IOException
   1.222 +     *          If an I/O error occurs
   1.223 +     *
   1.224 +     * @since  1.5
   1.225 +     */
   1.226 +    public Writer append(CharSequence csq) throws IOException {
   1.227 +        if (csq == null)
   1.228 +            write("null");
   1.229 +        else
   1.230 +            write(csq.toString());
   1.231 +        return this;
   1.232 +    }
   1.233 +
   1.234 +    /**
   1.235 +     * Appends a subsequence of the specified character sequence to this writer.
   1.236 +     * <tt>Appendable</tt>.
   1.237 +     *
   1.238 +     * <p> An invocation of this method of the form <tt>out.append(csq, start,
   1.239 +     * end)</tt> when <tt>csq</tt> is not <tt>null</tt> behaves in exactly the
   1.240 +     * same way as the invocation
   1.241 +     *
   1.242 +     * <pre>
   1.243 +     *     out.write(csq.subSequence(start, end).toString()) </pre>
   1.244 +     *
   1.245 +     * @param  csq
   1.246 +     *         The character sequence from which a subsequence will be
   1.247 +     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
   1.248 +     *         will be appended as if <tt>csq</tt> contained the four
   1.249 +     *         characters <tt>"null"</tt>.
   1.250 +     *
   1.251 +     * @param  start
   1.252 +     *         The index of the first character in the subsequence
   1.253 +     *
   1.254 +     * @param  end
   1.255 +     *         The index of the character following the last character in the
   1.256 +     *         subsequence
   1.257 +     *
   1.258 +     * @return  This writer
   1.259 +     *
   1.260 +     * @throws  IndexOutOfBoundsException
   1.261 +     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
   1.262 +     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
   1.263 +     *          <tt>csq.length()</tt>
   1.264 +     *
   1.265 +     * @throws  IOException
   1.266 +     *          If an I/O error occurs
   1.267 +     *
   1.268 +     * @since  1.5
   1.269 +     */
   1.270 +    public Writer append(CharSequence csq, int start, int end) throws IOException {
   1.271 +        CharSequence cs = (csq == null ? "null" : csq);
   1.272 +        write(cs.subSequence(start, end).toString());
   1.273 +        return this;
   1.274 +    }
   1.275 +
   1.276 +    /**
   1.277 +     * Appends the specified character to this writer.
   1.278 +     *
   1.279 +     * <p> An invocation of this method of the form <tt>out.append(c)</tt>
   1.280 +     * behaves in exactly the same way as the invocation
   1.281 +     *
   1.282 +     * <pre>
   1.283 +     *     out.write(c) </pre>
   1.284 +     *
   1.285 +     * @param  c
   1.286 +     *         The 16-bit character to append
   1.287 +     *
   1.288 +     * @return  This writer
   1.289 +     *
   1.290 +     * @throws  IOException
   1.291 +     *          If an I/O error occurs
   1.292 +     *
   1.293 +     * @since 1.5
   1.294 +     */
   1.295 +    public Writer append(char c) throws IOException {
   1.296 +        write(c);
   1.297 +        return this;
   1.298 +    }
   1.299 +
   1.300 +    /**
   1.301 +     * Flushes the stream.  If the stream has saved any characters from the
   1.302 +     * various write() methods in a buffer, write them immediately to their
   1.303 +     * intended destination.  Then, if that destination is another character or
   1.304 +     * byte stream, flush it.  Thus one flush() invocation will flush all the
   1.305 +     * buffers in a chain of Writers and OutputStreams.
   1.306 +     *
   1.307 +     * <p> If the intended destination of this stream is an abstraction provided
   1.308 +     * by the underlying operating system, for example a file, then flushing the
   1.309 +     * stream guarantees only that bytes previously written to the stream are
   1.310 +     * passed to the operating system for writing; it does not guarantee that
   1.311 +     * they are actually written to a physical device such as a disk drive.
   1.312 +     *
   1.313 +     * @throws  IOException
   1.314 +     *          If an I/O error occurs
   1.315 +     */
   1.316 +    abstract public void flush() throws IOException;
   1.317 +
   1.318 +    /**
   1.319 +     * Closes the stream, flushing it first. Once the stream has been closed,
   1.320 +     * further write() or flush() invocations will cause an IOException to be
   1.321 +     * thrown. Closing a previously closed stream has no effect.
   1.322 +     *
   1.323 +     * @throws  IOException
   1.324 +     *          If an I/O error occurs
   1.325 +     */
   1.326 +    abstract public void close() throws IOException;
   1.327 +
   1.328 +}