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