1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/emul/compact/src/main/java/java/io/OutputStreamWriter.java Sat Sep 07 13:51:24 2013 +0200
1.3 @@ -0,0 +1,235 @@
1.4 +/*
1.5 + * Copyright (c) 1996, 2006, 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 +import java.nio.charset.Charset;
1.32 +import java.nio.charset.CharsetEncoder;
1.33 +import sun.nio.cs.StreamEncoder;
1.34 +
1.35 +
1.36 +/**
1.37 + * An OutputStreamWriter is a bridge from character streams to byte streams:
1.38 + * Characters written to it are encoded into bytes using a specified {@link
1.39 + * java.nio.charset.Charset <code>charset</code>}. The charset that it uses
1.40 + * may be specified by name or may be given explicitly, or the platform's
1.41 + * default charset may be accepted.
1.42 + *
1.43 + * <p> Each invocation of a write() method causes the encoding converter to be
1.44 + * invoked on the given character(s). The resulting bytes are accumulated in a
1.45 + * buffer before being written to the underlying output stream. The size of
1.46 + * this buffer may be specified, but by default it is large enough for most
1.47 + * purposes. Note that the characters passed to the write() methods are not
1.48 + * buffered.
1.49 + *
1.50 + * <p> For top efficiency, consider wrapping an OutputStreamWriter within a
1.51 + * BufferedWriter so as to avoid frequent converter invocations. For example:
1.52 + *
1.53 + * <pre>
1.54 + * Writer out
1.55 + * = new BufferedWriter(new OutputStreamWriter(System.out));
1.56 + * </pre>
1.57 + *
1.58 + * <p> A <i>surrogate pair</i> is a character represented by a sequence of two
1.59 + * <tt>char</tt> values: A <i>high</i> surrogate in the range '\uD800' to
1.60 + * '\uDBFF' followed by a <i>low</i> surrogate in the range '\uDC00' to
1.61 + * '\uDFFF'.
1.62 + *
1.63 + * <p> A <i>malformed surrogate element</i> is a high surrogate that is not
1.64 + * followed by a low surrogate or a low surrogate that is not preceded by a
1.65 + * high surrogate.
1.66 + *
1.67 + * <p> This class always replaces malformed surrogate elements and unmappable
1.68 + * character sequences with the charset's default <i>substitution sequence</i>.
1.69 + * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
1.70 + * control over the encoding process is required.
1.71 + *
1.72 + * @see BufferedWriter
1.73 + * @see OutputStream
1.74 + * @see java.nio.charset.Charset
1.75 + *
1.76 + * @author Mark Reinhold
1.77 + * @since JDK1.1
1.78 + */
1.79 +
1.80 +public class OutputStreamWriter extends Writer {
1.81 +
1.82 + private final StreamEncoder se;
1.83 +
1.84 + /**
1.85 + * Creates an OutputStreamWriter that uses the named charset.
1.86 + *
1.87 + * @param out
1.88 + * An OutputStream
1.89 + *
1.90 + * @param charsetName
1.91 + * The name of a supported
1.92 + * {@link java.nio.charset.Charset </code>charset<code>}
1.93 + *
1.94 + * @exception UnsupportedEncodingException
1.95 + * If the named encoding is not supported
1.96 + */
1.97 + public OutputStreamWriter(OutputStream out, String charsetName)
1.98 + throws UnsupportedEncodingException
1.99 + {
1.100 + super(out);
1.101 + if (charsetName == null)
1.102 + throw new NullPointerException("charsetName");
1.103 + se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
1.104 + }
1.105 +
1.106 + /**
1.107 + * Creates an OutputStreamWriter that uses the default character encoding.
1.108 + *
1.109 + * @param out An OutputStream
1.110 + */
1.111 + public OutputStreamWriter(OutputStream out) {
1.112 + super(out);
1.113 + try {
1.114 + se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
1.115 + } catch (UnsupportedEncodingException e) {
1.116 + throw new Error(e);
1.117 + }
1.118 + }
1.119 +
1.120 + /**
1.121 + * Creates an OutputStreamWriter that uses the given charset. </p>
1.122 + *
1.123 + * @param out
1.124 + * An OutputStream
1.125 + *
1.126 + * @param cs
1.127 + * A charset
1.128 + *
1.129 + * @since 1.4
1.130 + * @spec JSR-51
1.131 + */
1.132 + public OutputStreamWriter(OutputStream out, Charset cs) {
1.133 + super(out);
1.134 + if (cs == null)
1.135 + throw new NullPointerException("charset");
1.136 + se = StreamEncoder.forOutputStreamWriter(out, this, cs);
1.137 + }
1.138 +
1.139 + /**
1.140 + * Creates an OutputStreamWriter that uses the given charset encoder. </p>
1.141 + *
1.142 + * @param out
1.143 + * An OutputStream
1.144 + *
1.145 + * @param enc
1.146 + * A charset encoder
1.147 + *
1.148 + * @since 1.4
1.149 + * @spec JSR-51
1.150 + */
1.151 + public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
1.152 + super(out);
1.153 + if (enc == null)
1.154 + throw new NullPointerException("charset encoder");
1.155 + se = StreamEncoder.forOutputStreamWriter(out, this, enc);
1.156 + }
1.157 +
1.158 + /**
1.159 + * Returns the name of the character encoding being used by this stream.
1.160 + *
1.161 + * <p> If the encoding has an historical name then that name is returned;
1.162 + * otherwise the encoding's canonical name is returned.
1.163 + *
1.164 + * <p> If this instance was created with the {@link
1.165 + * #OutputStreamWriter(OutputStream, String)} constructor then the returned
1.166 + * name, being unique for the encoding, may differ from the name passed to
1.167 + * the constructor. This method may return <tt>null</tt> if the stream has
1.168 + * been closed. </p>
1.169 + *
1.170 + * @return The historical name of this encoding, or possibly
1.171 + * <code>null</code> if the stream has been closed
1.172 + *
1.173 + * @see java.nio.charset.Charset
1.174 + *
1.175 + * @revised 1.4
1.176 + * @spec JSR-51
1.177 + */
1.178 + public String getEncoding() {
1.179 + return se.getEncoding();
1.180 + }
1.181 +
1.182 + /**
1.183 + * Flushes the output buffer to the underlying byte stream, without flushing
1.184 + * the byte stream itself. This method is non-private only so that it may
1.185 + * be invoked by PrintStream.
1.186 + */
1.187 + void flushBuffer() throws IOException {
1.188 + se.flushBuffer();
1.189 + }
1.190 +
1.191 + /**
1.192 + * Writes a single character.
1.193 + *
1.194 + * @exception IOException If an I/O error occurs
1.195 + */
1.196 + public void write(int c) throws IOException {
1.197 + se.write(c);
1.198 + }
1.199 +
1.200 + /**
1.201 + * Writes a portion of an array of characters.
1.202 + *
1.203 + * @param cbuf Buffer of characters
1.204 + * @param off Offset from which to start writing characters
1.205 + * @param len Number of characters to write
1.206 + *
1.207 + * @exception IOException If an I/O error occurs
1.208 + */
1.209 + public void write(char cbuf[], int off, int len) throws IOException {
1.210 + se.write(cbuf, off, len);
1.211 + }
1.212 +
1.213 + /**
1.214 + * Writes a portion of a string.
1.215 + *
1.216 + * @param str A String
1.217 + * @param off Offset from which to start writing characters
1.218 + * @param len Number of characters to write
1.219 + *
1.220 + * @exception IOException If an I/O error occurs
1.221 + */
1.222 + public void write(String str, int off, int len) throws IOException {
1.223 + se.write(str, off, len);
1.224 + }
1.225 +
1.226 + /**
1.227 + * Flushes the stream.
1.228 + *
1.229 + * @exception IOException If an I/O error occurs
1.230 + */
1.231 + public void flush() throws IOException {
1.232 + se.flush();
1.233 + }
1.234 +
1.235 + public void close() throws IOException {
1.236 + se.close();
1.237 + }
1.238 +}