jdk/src/share/classes/java/io/Writer.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
 * Abstract class for writing to character streams.  The only methods that a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * subclass must implement are write(char[], int, int), flush(), and close().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Most subclasses, however, will override some of the methods defined here in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * order to provide higher efficiency, additional functionality, or both.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @see Writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see   BufferedWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see   CharArrayWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see   FilterWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see   OutputStreamWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see     FileWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see   PipedWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see   PrintWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see   StringWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public abstract class Writer implements Appendable, Closeable, Flushable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Temporary buffer used to hold writes of strings and single characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private char[] writeBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Size of writeBuffer, must be >= 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final int writeBufferSize = 1024;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * The object used to synchronize operations on this stream.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * efficiency, a character-stream object may use an object other than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * itself to protect critical sections.  A subclass should therefore use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * the object in this field rather than <tt>this</tt> or a synchronized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Creates a new character-stream writer whose critical sections will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * synchronize on the writer itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected Writer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.lock = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * Creates a new character-stream writer whose critical sections will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * synchronize on the given object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param  lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *         Object to synchronize on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected Writer(Object lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if (lock == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        this.lock = lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Writes a single character.  The character to be written is contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * the 16 low-order bits of the given integer value; the 16 high-order bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <p> Subclasses that intend to support efficient single-character output
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * should override this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *         int specifying a character to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public void write(int c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (writeBuffer == null){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                writeBuffer = new char[writeBufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            writeBuffer[0] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            write(writeBuffer, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Writes an array of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param  cbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *         Array of characters to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void write(char cbuf[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        write(cbuf, 0, cbuf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Writes a portion of an array of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param  cbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *         Array of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *         Offset from which to start writing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *         Number of characters to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    abstract public void write(char cbuf[], int off, int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Writes a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param  str
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *         String to be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public void write(String str) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        write(str, 0, str.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Writes a portion of a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param  str
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *         A String
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *         Offset from which to start writing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *         Number of characters to write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *          If <tt>off</tt> is negative, or <tt>len</tt> is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *          or <tt>off+len</tt> is negative or greater than the length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *          of the given string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void write(String str, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            char cbuf[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            if (len <= writeBufferSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                if (writeBuffer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    writeBuffer = new char[writeBufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                cbuf = writeBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            } else {    // Don't permanently allocate very large buffers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                cbuf = new char[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            str.getChars(off, (off + len), cbuf, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            write(cbuf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * Appends the specified character sequence to this writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <p> An invocation of this method of the form <tt>out.append(csq)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *     out.write(csq.toString()) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * <p> Depending on the specification of <tt>toString</tt> for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * character sequence <tt>csq</tt>, the entire sequence may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * appended. For instance, invoking the <tt>toString</tt> method of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * character buffer will return a subsequence whose content depends upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * the buffer's position and limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *         The character sequence to append.  If <tt>csq</tt> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *         <tt>null</tt>, then the four characters <tt>"null"</tt> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *         appended to this writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @return  This writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public Writer append(CharSequence csq) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (csq == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            write("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            write(csq.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Appends a subsequence of the specified character sequence to this writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <tt>Appendable</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p> An invocation of this method of the form <tt>out.append(csq, start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * end)</tt> when <tt>csq</tt> is not <tt>null</tt> behaves in exactly the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *     out.write(csq.subSequence(start, end).toString()) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param  csq
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *         The character sequence from which a subsequence will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *         appended.  If <tt>csq</tt> is <tt>null</tt>, then characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *         will be appended as if <tt>csq</tt> contained the four
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *         characters <tt>"null"</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @param  start
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *         The index of the first character in the subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * @param  end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *         The index of the character following the last character in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *         subsequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @return  This writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @throws  IndexOutOfBoundsException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *          If <tt>start</tt> or <tt>end</tt> are negative, <tt>start</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *          is greater than <tt>end</tt>, or <tt>end</tt> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *          <tt>csq.length()</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public Writer append(CharSequence csq, int start, int end) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        CharSequence cs = (csq == null ? "null" : csq);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        write(cs.subSequence(start, end).toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Appends the specified character to this writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * <p> An invocation of this method of the form <tt>out.append(c)</tt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * behaves in exactly the same way as the invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *     out.write(c) </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @param  c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *         The 16-bit character to append
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return  This writer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public Writer append(char c) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Flushes the stream.  If the stream has saved any characters from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * various write() methods in a buffer, write them immediately to their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * intended destination.  Then, if that destination is another character or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * byte stream, flush it.  Thus one flush() invocation will flush all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * buffers in a chain of Writers and OutputStreams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * <p> If the intended destination of this stream is an abstraction provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * by the underlying operating system, for example a file, then flushing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * stream guarantees only that bytes previously written to the stream are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * passed to the operating system for writing; it does not guarantee that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * they are actually written to a physical device such as a disk drive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    abstract public void flush() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Closes the stream, flushing it first. Once the stream has been closed,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * further write() or flush() invocations will cause an IOException to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * thrown. Closing a previously closed stream has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    abstract public void close() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}