rt/emul/compact/src/main/java/java/io/OutputStreamWriter.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Sep 2013 13:55:09 +0200
changeset 1259 d257b7a37635
parent 1258 emul/compact/src/main/java/java/io/OutputStreamWriter.java@724f3e1ea53e
child 1260 fe3567c7b522
permissions -rw-r--r--
Merge of JDK1.7 build 147 version of classes
jaroslav@1258
     1
/*
jaroslav@1258
     2
 * Copyright (c) 1996, 2006, 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
import java.nio.charset.Charset;
jaroslav@1258
    29
import java.nio.charset.CharsetEncoder;
jaroslav@1258
    30
import sun.nio.cs.StreamEncoder;
jaroslav@1258
    31
jaroslav@1258
    32
jaroslav@1258
    33
/**
jaroslav@1258
    34
 * An OutputStreamWriter is a bridge from character streams to byte streams:
jaroslav@1258
    35
 * Characters written to it are encoded into bytes using a specified {@link
jaroslav@1258
    36
 * java.nio.charset.Charset <code>charset</code>}.  The charset that it uses
jaroslav@1258
    37
 * may be specified by name or may be given explicitly, or the platform's
jaroslav@1258
    38
 * default charset may be accepted.
jaroslav@1258
    39
 *
jaroslav@1258
    40
 * <p> Each invocation of a write() method causes the encoding converter to be
jaroslav@1258
    41
 * invoked on the given character(s).  The resulting bytes are accumulated in a
jaroslav@1258
    42
 * buffer before being written to the underlying output stream.  The size of
jaroslav@1258
    43
 * this buffer may be specified, but by default it is large enough for most
jaroslav@1258
    44
 * purposes.  Note that the characters passed to the write() methods are not
jaroslav@1258
    45
 * buffered.
jaroslav@1258
    46
 *
jaroslav@1258
    47
 * <p> For top efficiency, consider wrapping an OutputStreamWriter within a
jaroslav@1258
    48
 * BufferedWriter so as to avoid frequent converter invocations.  For example:
jaroslav@1258
    49
 *
jaroslav@1258
    50
 * <pre>
jaroslav@1258
    51
 * Writer out
jaroslav@1258
    52
 *   = new BufferedWriter(new OutputStreamWriter(System.out));
jaroslav@1258
    53
 * </pre>
jaroslav@1258
    54
 *
jaroslav@1258
    55
 * <p> A <i>surrogate pair</i> is a character represented by a sequence of two
jaroslav@1258
    56
 * <tt>char</tt> values: A <i>high</i> surrogate in the range '&#92;uD800' to
jaroslav@1258
    57
 * '&#92;uDBFF' followed by a <i>low</i> surrogate in the range '&#92;uDC00' to
jaroslav@1258
    58
 * '&#92;uDFFF'.
jaroslav@1258
    59
 *
jaroslav@1258
    60
 * <p> A <i>malformed surrogate element</i> is a high surrogate that is not
jaroslav@1258
    61
 * followed by a low surrogate or a low surrogate that is not preceded by a
jaroslav@1258
    62
 * high surrogate.
jaroslav@1258
    63
 *
jaroslav@1258
    64
 * <p> This class always replaces malformed surrogate elements and unmappable
jaroslav@1258
    65
 * character sequences with the charset's default <i>substitution sequence</i>.
jaroslav@1258
    66
 * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
jaroslav@1258
    67
 * control over the encoding process is required.
jaroslav@1258
    68
 *
jaroslav@1258
    69
 * @see BufferedWriter
jaroslav@1258
    70
 * @see OutputStream
jaroslav@1258
    71
 * @see java.nio.charset.Charset
jaroslav@1258
    72
 *
jaroslav@1258
    73
 * @author      Mark Reinhold
jaroslav@1258
    74
 * @since       JDK1.1
jaroslav@1258
    75
 */
jaroslav@1258
    76
jaroslav@1258
    77
public class OutputStreamWriter extends Writer {
jaroslav@1258
    78
jaroslav@1258
    79
    private final StreamEncoder se;
jaroslav@1258
    80
jaroslav@1258
    81
    /**
jaroslav@1258
    82
     * Creates an OutputStreamWriter that uses the named charset.
jaroslav@1258
    83
     *
jaroslav@1258
    84
     * @param  out
jaroslav@1258
    85
     *         An OutputStream
jaroslav@1258
    86
     *
jaroslav@1258
    87
     * @param  charsetName
jaroslav@1258
    88
     *         The name of a supported
jaroslav@1258
    89
     *         {@link java.nio.charset.Charset </code>charset<code>}
jaroslav@1258
    90
     *
jaroslav@1258
    91
     * @exception  UnsupportedEncodingException
jaroslav@1258
    92
     *             If the named encoding is not supported
jaroslav@1258
    93
     */
jaroslav@1258
    94
    public OutputStreamWriter(OutputStream out, String charsetName)
jaroslav@1258
    95
        throws UnsupportedEncodingException
jaroslav@1258
    96
    {
jaroslav@1258
    97
        super(out);
jaroslav@1258
    98
        if (charsetName == null)
jaroslav@1258
    99
            throw new NullPointerException("charsetName");
jaroslav@1258
   100
        se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
jaroslav@1258
   101
    }
jaroslav@1258
   102
jaroslav@1258
   103
    /**
jaroslav@1258
   104
     * Creates an OutputStreamWriter that uses the default character encoding.
jaroslav@1258
   105
     *
jaroslav@1258
   106
     * @param  out  An OutputStream
jaroslav@1258
   107
     */
jaroslav@1258
   108
    public OutputStreamWriter(OutputStream out) {
jaroslav@1258
   109
        super(out);
jaroslav@1258
   110
        try {
jaroslav@1258
   111
            se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
jaroslav@1258
   112
        } catch (UnsupportedEncodingException e) {
jaroslav@1258
   113
            throw new Error(e);
jaroslav@1258
   114
        }
jaroslav@1258
   115
    }
jaroslav@1258
   116
jaroslav@1258
   117
    /**
jaroslav@1258
   118
     * Creates an OutputStreamWriter that uses the given charset. </p>
jaroslav@1258
   119
     *
jaroslav@1258
   120
     * @param  out
jaroslav@1258
   121
     *         An OutputStream
jaroslav@1258
   122
     *
jaroslav@1258
   123
     * @param  cs
jaroslav@1258
   124
     *         A charset
jaroslav@1258
   125
     *
jaroslav@1258
   126
     * @since 1.4
jaroslav@1258
   127
     * @spec JSR-51
jaroslav@1258
   128
     */
jaroslav@1258
   129
    public OutputStreamWriter(OutputStream out, Charset cs) {
jaroslav@1258
   130
        super(out);
jaroslav@1258
   131
        if (cs == null)
jaroslav@1258
   132
            throw new NullPointerException("charset");
jaroslav@1258
   133
        se = StreamEncoder.forOutputStreamWriter(out, this, cs);
jaroslav@1258
   134
    }
jaroslav@1258
   135
jaroslav@1258
   136
    /**
jaroslav@1258
   137
     * Creates an OutputStreamWriter that uses the given charset encoder.  </p>
jaroslav@1258
   138
     *
jaroslav@1258
   139
     * @param  out
jaroslav@1258
   140
     *         An OutputStream
jaroslav@1258
   141
     *
jaroslav@1258
   142
     * @param  enc
jaroslav@1258
   143
     *         A charset encoder
jaroslav@1258
   144
     *
jaroslav@1258
   145
     * @since 1.4
jaroslav@1258
   146
     * @spec JSR-51
jaroslav@1258
   147
     */
jaroslav@1258
   148
    public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
jaroslav@1258
   149
        super(out);
jaroslav@1258
   150
        if (enc == null)
jaroslav@1258
   151
            throw new NullPointerException("charset encoder");
jaroslav@1258
   152
        se = StreamEncoder.forOutputStreamWriter(out, this, enc);
jaroslav@1258
   153
    }
jaroslav@1258
   154
jaroslav@1258
   155
    /**
jaroslav@1258
   156
     * Returns the name of the character encoding being used by this stream.
jaroslav@1258
   157
     *
jaroslav@1258
   158
     * <p> If the encoding has an historical name then that name is returned;
jaroslav@1258
   159
     * otherwise the encoding's canonical name is returned.
jaroslav@1258
   160
     *
jaroslav@1258
   161
     * <p> If this instance was created with the {@link
jaroslav@1258
   162
     * #OutputStreamWriter(OutputStream, String)} constructor then the returned
jaroslav@1258
   163
     * name, being unique for the encoding, may differ from the name passed to
jaroslav@1258
   164
     * the constructor.  This method may return <tt>null</tt> if the stream has
jaroslav@1258
   165
     * been closed. </p>
jaroslav@1258
   166
     *
jaroslav@1258
   167
     * @return The historical name of this encoding, or possibly
jaroslav@1258
   168
     *         <code>null</code> if the stream has been closed
jaroslav@1258
   169
     *
jaroslav@1258
   170
     * @see java.nio.charset.Charset
jaroslav@1258
   171
     *
jaroslav@1258
   172
     * @revised 1.4
jaroslav@1258
   173
     * @spec JSR-51
jaroslav@1258
   174
     */
jaroslav@1258
   175
    public String getEncoding() {
jaroslav@1258
   176
        return se.getEncoding();
jaroslav@1258
   177
    }
jaroslav@1258
   178
jaroslav@1258
   179
    /**
jaroslav@1258
   180
     * Flushes the output buffer to the underlying byte stream, without flushing
jaroslav@1258
   181
     * the byte stream itself.  This method is non-private only so that it may
jaroslav@1258
   182
     * be invoked by PrintStream.
jaroslav@1258
   183
     */
jaroslav@1258
   184
    void flushBuffer() throws IOException {
jaroslav@1258
   185
        se.flushBuffer();
jaroslav@1258
   186
    }
jaroslav@1258
   187
jaroslav@1258
   188
    /**
jaroslav@1258
   189
     * Writes a single character.
jaroslav@1258
   190
     *
jaroslav@1258
   191
     * @exception  IOException  If an I/O error occurs
jaroslav@1258
   192
     */
jaroslav@1258
   193
    public void write(int c) throws IOException {
jaroslav@1258
   194
        se.write(c);
jaroslav@1258
   195
    }
jaroslav@1258
   196
jaroslav@1258
   197
    /**
jaroslav@1258
   198
     * Writes a portion of an array of characters.
jaroslav@1258
   199
     *
jaroslav@1258
   200
     * @param  cbuf  Buffer of characters
jaroslav@1258
   201
     * @param  off   Offset from which to start writing characters
jaroslav@1258
   202
     * @param  len   Number of characters to write
jaroslav@1258
   203
     *
jaroslav@1258
   204
     * @exception  IOException  If an I/O error occurs
jaroslav@1258
   205
     */
jaroslav@1258
   206
    public void write(char cbuf[], int off, int len) throws IOException {
jaroslav@1258
   207
        se.write(cbuf, off, len);
jaroslav@1258
   208
    }
jaroslav@1258
   209
jaroslav@1258
   210
    /**
jaroslav@1258
   211
     * Writes a portion of a string.
jaroslav@1258
   212
     *
jaroslav@1258
   213
     * @param  str  A String
jaroslav@1258
   214
     * @param  off  Offset from which to start writing characters
jaroslav@1258
   215
     * @param  len  Number of characters to write
jaroslav@1258
   216
     *
jaroslav@1258
   217
     * @exception  IOException  If an I/O error occurs
jaroslav@1258
   218
     */
jaroslav@1258
   219
    public void write(String str, int off, int len) throws IOException {
jaroslav@1258
   220
        se.write(str, off, len);
jaroslav@1258
   221
    }
jaroslav@1258
   222
jaroslav@1258
   223
    /**
jaroslav@1258
   224
     * Flushes the stream.
jaroslav@1258
   225
     *
jaroslav@1258
   226
     * @exception  IOException  If an I/O error occurs
jaroslav@1258
   227
     */
jaroslav@1258
   228
    public void flush() throws IOException {
jaroslav@1258
   229
        se.flush();
jaroslav@1258
   230
    }
jaroslav@1258
   231
jaroslav@1258
   232
    public void close() throws IOException {
jaroslav@1258
   233
        se.close();
jaroslav@1258
   234
    }
jaroslav@1258
   235
}