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.
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.
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).
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.
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
28 import java.nio.charset.Charset;
29 import java.nio.charset.CharsetEncoder;
30 import sun.nio.cs.StreamEncoder;
34 * An OutputStreamWriter is a bridge from character streams to byte streams:
35 * Characters written to it are encoded into bytes using a specified {@link
36 * java.nio.charset.Charset <code>charset</code>}. The charset that it uses
37 * may be specified by name or may be given explicitly, or the platform's
38 * default charset may be accepted.
40 * <p> Each invocation of a write() method causes the encoding converter to be
41 * invoked on the given character(s). The resulting bytes are accumulated in a
42 * buffer before being written to the underlying output stream. The size of
43 * this buffer may be specified, but by default it is large enough for most
44 * purposes. Note that the characters passed to the write() methods are not
47 * <p> For top efficiency, consider wrapping an OutputStreamWriter within a
48 * BufferedWriter so as to avoid frequent converter invocations. For example:
52 * = new BufferedWriter(new OutputStreamWriter(System.out));
55 * <p> A <i>surrogate pair</i> is a character represented by a sequence of two
56 * <tt>char</tt> values: A <i>high</i> surrogate in the range '\uD800' to
57 * '\uDBFF' followed by a <i>low</i> surrogate in the range '\uDC00' to
60 * <p> A <i>malformed surrogate element</i> is a high surrogate that is not
61 * followed by a low surrogate or a low surrogate that is not preceded by a
64 * <p> This class always replaces malformed surrogate elements and unmappable
65 * character sequences with the charset's default <i>substitution sequence</i>.
66 * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
67 * control over the encoding process is required.
71 * @see java.nio.charset.Charset
73 * @author Mark Reinhold
77 public class OutputStreamWriter extends Writer {
79 private final StreamEncoder se;
82 * Creates an OutputStreamWriter that uses the named charset.
88 * The name of a supported
89 * {@link java.nio.charset.Charset </code>charset<code>}
91 * @exception UnsupportedEncodingException
92 * If the named encoding is not supported
94 public OutputStreamWriter(OutputStream out, String charsetName)
95 throws UnsupportedEncodingException
98 if (charsetName == null)
99 throw new NullPointerException("charsetName");
100 se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
104 * Creates an OutputStreamWriter that uses the default character encoding.
106 * @param out An OutputStream
108 public OutputStreamWriter(OutputStream out) {
111 se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
112 } catch (UnsupportedEncodingException e) {
118 * Creates an OutputStreamWriter that uses the given charset. </p>
129 public OutputStreamWriter(OutputStream out, Charset cs) {
132 throw new NullPointerException("charset");
133 se = StreamEncoder.forOutputStreamWriter(out, this, cs);
137 * Creates an OutputStreamWriter that uses the given charset encoder. </p>
148 public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
151 throw new NullPointerException("charset encoder");
152 se = StreamEncoder.forOutputStreamWriter(out, this, enc);
156 * Returns the name of the character encoding being used by this stream.
158 * <p> If the encoding has an historical name then that name is returned;
159 * otherwise the encoding's canonical name is returned.
161 * <p> If this instance was created with the {@link
162 * #OutputStreamWriter(OutputStream, String)} constructor then the returned
163 * name, being unique for the encoding, may differ from the name passed to
164 * the constructor. This method may return <tt>null</tt> if the stream has
167 * @return The historical name of this encoding, or possibly
168 * <code>null</code> if the stream has been closed
170 * @see java.nio.charset.Charset
175 public String getEncoding() {
176 return se.getEncoding();
180 * Flushes the output buffer to the underlying byte stream, without flushing
181 * the byte stream itself. This method is non-private only so that it may
182 * be invoked by PrintStream.
184 void flushBuffer() throws IOException {
189 * Writes a single character.
191 * @exception IOException If an I/O error occurs
193 public void write(int c) throws IOException {
198 * Writes a portion of an array of characters.
200 * @param cbuf Buffer of characters
201 * @param off Offset from which to start writing characters
202 * @param len Number of characters to write
204 * @exception IOException If an I/O error occurs
206 public void write(char cbuf[], int off, int len) throws IOException {
207 se.write(cbuf, off, len);
211 * Writes a portion of a string.
213 * @param str A String
214 * @param off Offset from which to start writing characters
215 * @param len Number of characters to write
217 * @exception IOException If an I/O error occurs
219 public void write(String str, int off, int len) throws IOException {
220 se.write(str, off, len);
224 * Flushes the stream.
226 * @exception IOException If an I/O error occurs
228 public void flush() throws IOException {
232 public void close() throws IOException {