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;
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.
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
44 * <p> For top efficiency, consider wrapping an OutputStreamWriter within a
45 * BufferedWriter so as to avoid frequent converter invocations. For example:
49 * = new BufferedWriter(new OutputStreamWriter(System.out));
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 '\uD800' to
54 * '\uDBFF' followed by a <i>low</i> surrogate in the range '\uDC00' to
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
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.
68 * @see java.nio.charset.Charset
70 * @author Mark Reinhold
74 public class OutputStreamWriter extends Writer {
77 * Creates an OutputStreamWriter that uses the named charset.
83 * The name of a supported
84 * {@link java.nio.charset.Charset </code>charset<code>}
86 * @exception UnsupportedEncodingException
87 * If the named encoding is not supported
89 public OutputStreamWriter(OutputStream out, String charsetName)
90 throws UnsupportedEncodingException
93 if (charsetName == null)
94 throw new NullPointerException("charsetName");
95 if (!charsetName.toUpperCase().equals("UTF-8")) {
96 throw new UnsupportedEncodingException(charsetName);
101 * Creates an OutputStreamWriter that uses the default character encoding.
103 * @param out An OutputStream
105 public OutputStreamWriter(OutputStream out) {
110 * Creates an OutputStreamWriter that uses the given charset. </p>
121 public OutputStreamWriter(OutputStream out, Charset cs) {
126 * Creates an OutputStreamWriter that uses the given charset encoder. </p>
137 // public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
140 // throw new NullPointerException("charset encoder");
141 // se = StreamEncoder.forOutputStreamWriter(out, this, enc);
145 * Returns the name of the character encoding being used by this stream.
147 * <p> If the encoding has an historical name then that name is returned;
148 * otherwise the encoding's canonical name is returned.
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
156 * @return The historical name of this encoding, or possibly
157 * <code>null</code> if the stream has been closed
159 * @see java.nio.charset.Charset
164 public String getEncoding() {
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.
173 void flushBuffer() throws IOException {
178 * Writes a single character.
180 * @exception IOException If an I/O error occurs
182 public void write(int c) throws IOException {
185 } else if (c <= 0x7FF) {
186 out().write(0xC0 | (c >> 6));
187 out().write(0x80 | (c & 0x3F));
189 out().write(0xE0 | (c >> 12));
190 out().write(0x80 | ((c >> 6) & 0x3F));
191 out().write(0x80 | (c & 0x3F));
196 * Writes a portion of an array of characters.
198 * @param cbuf Buffer of characters
199 * @param off Offset from which to start writing characters
200 * @param len Number of characters to write
202 * @exception IOException If an I/O error occurs
204 public void write(char cbuf[], int off, int len) throws IOException {
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 {
221 write(str.charAt(off++));
226 * Flushes the stream.
228 * @exception IOException If an I/O error occurs
230 public void flush() throws IOException {
234 public void close() throws IOException {
238 private OutputStream out() {
239 return (OutputStream) lock;