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
     1 /*
     2  * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.io;
    27 
    28 import java.nio.charset.Charset;
    29 
    30 /**
    31  * An OutputStreamWriter is a bridge from character streams to byte streams:
    32  * Characters written to it are encoded into bytes using a specified {@link
    33  * java.nio.charset.Charset <code>charset</code>}.  The charset that it uses
    34  * may be specified by name or may be given explicitly, or the platform's
    35  * default charset may be accepted.
    36  *
    37  * <p> Each invocation of a write() method causes the encoding converter to be
    38  * invoked on the given character(s).  The resulting bytes are accumulated in a
    39  * buffer before being written to the underlying output stream.  The size of
    40  * this buffer may be specified, but by default it is large enough for most
    41  * purposes.  Note that the characters passed to the write() methods are not
    42  * buffered.
    43  *
    44  * <p> For top efficiency, consider wrapping an OutputStreamWriter within a
    45  * BufferedWriter so as to avoid frequent converter invocations.  For example:
    46  *
    47  * <pre>
    48  * Writer out
    49  *   = new BufferedWriter(new OutputStreamWriter(System.out));
    50  * </pre>
    51  *
    52  * <p> A <i>surrogate pair</i> is a character represented by a sequence of two
    53  * <tt>char</tt> values: A <i>high</i> surrogate in the range '&#92;uD800' to
    54  * '&#92;uDBFF' followed by a <i>low</i> surrogate in the range '&#92;uDC00' to
    55  * '&#92;uDFFF'.
    56  *
    57  * <p> A <i>malformed surrogate element</i> is a high surrogate that is not
    58  * followed by a low surrogate or a low surrogate that is not preceded by a
    59  * high surrogate.
    60  *
    61  * <p> This class always replaces malformed surrogate elements and unmappable
    62  * character sequences with the charset's default <i>substitution sequence</i>.
    63  * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
    64  * control over the encoding process is required.
    65  *
    66  * @see BufferedWriter
    67  * @see OutputStream
    68  * @see java.nio.charset.Charset
    69  *
    70  * @author      Mark Reinhold
    71  * @since       JDK1.1
    72  */
    73 
    74 public class OutputStreamWriter extends Writer {
    75     
    76     /**
    77      * Creates an OutputStreamWriter that uses the named charset.
    78      *
    79      * @param  out
    80      *         An OutputStream
    81      *
    82      * @param  charsetName
    83      *         The name of a supported
    84      *         {@link java.nio.charset.Charset </code>charset<code>}
    85      *
    86      * @exception  UnsupportedEncodingException
    87      *             If the named encoding is not supported
    88      */
    89     public OutputStreamWriter(OutputStream out, String charsetName)
    90         throws UnsupportedEncodingException
    91     {
    92         super(out);
    93         if (charsetName == null)
    94             throw new NullPointerException("charsetName");
    95         if (!charsetName.toUpperCase().equals("UTF-8")) {
    96             throw new UnsupportedEncodingException(charsetName);
    97         }
    98     }
    99 
   100     /**
   101      * Creates an OutputStreamWriter that uses the default character encoding.
   102      *
   103      * @param  out  An OutputStream
   104      */
   105     public OutputStreamWriter(OutputStream out) {
   106         super(out);
   107     }
   108 
   109     /**
   110      * Creates an OutputStreamWriter that uses the given charset. </p>
   111      *
   112      * @param  out
   113      *         An OutputStream
   114      *
   115      * @param  cs
   116      *         A charset
   117      *
   118      * @since 1.4
   119      * @spec JSR-51
   120      */
   121     public OutputStreamWriter(OutputStream out, Charset cs) {
   122         this(out);
   123     }
   124 
   125     /**
   126      * Creates an OutputStreamWriter that uses the given charset encoder.  </p>
   127      *
   128      * @param  out
   129      *         An OutputStream
   130      *
   131      * @param  enc
   132      *         A charset encoder
   133      *
   134      * @since 1.4
   135      * @spec JSR-51
   136      */
   137 //    public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
   138 //        super(out);
   139 //        if (enc == null)
   140 //            throw new NullPointerException("charset encoder");
   141 //        se = StreamEncoder.forOutputStreamWriter(out, this, enc);
   142 //    }
   143 
   144     /**
   145      * Returns the name of the character encoding being used by this stream.
   146      *
   147      * <p> If the encoding has an historical name then that name is returned;
   148      * otherwise the encoding's canonical name is returned.
   149      *
   150      * <p> If this instance was created with the {@link
   151      * #OutputStreamWriter(OutputStream, String)} constructor then the returned
   152      * name, being unique for the encoding, may differ from the name passed to
   153      * the constructor.  This method may return <tt>null</tt> if the stream has
   154      * been closed. </p>
   155      *
   156      * @return The historical name of this encoding, or possibly
   157      *         <code>null</code> if the stream has been closed
   158      *
   159      * @see java.nio.charset.Charset
   160      *
   161      * @revised 1.4
   162      * @spec JSR-51
   163      */
   164     public String getEncoding() {
   165         return "UTF-8";
   166     }
   167 
   168     /**
   169      * Flushes the output buffer to the underlying byte stream, without flushing
   170      * the byte stream itself.  This method is non-private only so that it may
   171      * be invoked by PrintStream.
   172      */
   173     void flushBuffer() throws IOException {
   174         out().flush();
   175     }
   176 
   177     /**
   178      * Writes a single character.
   179      *
   180      * @exception  IOException  If an I/O error occurs
   181      */
   182     public void write(int c) throws IOException {
   183         if (c <= 0x7F) {
   184             out().write(c);
   185         } else if (c <= 0x7FF) {
   186             out().write(0xC0 | (c >> 6));
   187             out().write(0x80 | (c & 0x3F));
   188         } else {
   189             out().write(0xE0 | (c >> 12));
   190             out().write(0x80 | ((c >> 6) & 0x3F));
   191             out().write(0x80 | (c & 0x3F));
   192         }
   193     }
   194 
   195     /**
   196      * Writes a portion of an array of characters.
   197      *
   198      * @param  cbuf  Buffer of characters
   199      * @param  off   Offset from which to start writing characters
   200      * @param  len   Number of characters to write
   201      *
   202      * @exception  IOException  If an I/O error occurs
   203      */
   204     public void write(char cbuf[], int off, int len) throws IOException {
   205         while (len-- > 0) {
   206             write(cbuf[off++]);
   207         }
   208     }
   209 
   210     /**
   211      * Writes a portion of a string.
   212      *
   213      * @param  str  A String
   214      * @param  off  Offset from which to start writing characters
   215      * @param  len  Number of characters to write
   216      *
   217      * @exception  IOException  If an I/O error occurs
   218      */
   219     public void write(String str, int off, int len) throws IOException {
   220         while (len-- > 0) {
   221             write(str.charAt(off++));
   222         }
   223     }
   224 
   225     /**
   226      * Flushes the stream.
   227      *
   228      * @exception  IOException  If an I/O error occurs
   229      */
   230     public void flush() throws IOException {
   231         out().flush();
   232     }
   233 
   234     public void close() throws IOException {
   235         out().close();
   236     }
   237     
   238     private OutputStream out() {
   239         return (OutputStream) lock;
   240     }
   241 }