jaroslav@1258: /*
jaroslav@1258: * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
jaroslav@1258: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1258: *
jaroslav@1258: * This code is free software; you can redistribute it and/or modify it
jaroslav@1258: * under the terms of the GNU General Public License version 2 only, as
jaroslav@1258: * published by the Free Software Foundation. Oracle designates this
jaroslav@1258: * particular file as subject to the "Classpath" exception as provided
jaroslav@1258: * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1258: *
jaroslav@1258: * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1258: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1258: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jaroslav@1258: * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1258: * accompanied this code).
jaroslav@1258: *
jaroslav@1258: * You should have received a copy of the GNU General Public License version
jaroslav@1258: * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1258: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1258: *
jaroslav@1258: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1258: * or visit www.oracle.com if you need additional information or have any
jaroslav@1258: * questions.
jaroslav@1258: */
jaroslav@1258:
jaroslav@1258: package java.io;
jaroslav@1258:
jaroslav@1258: import java.nio.charset.Charset;
jaroslav@1258: import java.nio.charset.CharsetEncoder;
jaroslav@1258: import sun.nio.cs.StreamEncoder;
jaroslav@1258:
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * An OutputStreamWriter is a bridge from character streams to byte streams:
jaroslav@1258: * Characters written to it are encoded into bytes using a specified {@link
jaroslav@1258: * java.nio.charset.Charset charset
}. The charset that it uses
jaroslav@1258: * may be specified by name or may be given explicitly, or the platform's
jaroslav@1258: * default charset may be accepted.
jaroslav@1258: *
jaroslav@1258: *
Each invocation of a write() method causes the encoding converter to be jaroslav@1258: * invoked on the given character(s). The resulting bytes are accumulated in a jaroslav@1258: * buffer before being written to the underlying output stream. The size of jaroslav@1258: * this buffer may be specified, but by default it is large enough for most jaroslav@1258: * purposes. Note that the characters passed to the write() methods are not jaroslav@1258: * buffered. jaroslav@1258: * jaroslav@1258: *
For top efficiency, consider wrapping an OutputStreamWriter within a jaroslav@1258: * BufferedWriter so as to avoid frequent converter invocations. For example: jaroslav@1258: * jaroslav@1258: *
jaroslav@1258: * Writer out jaroslav@1258: * = new BufferedWriter(new OutputStreamWriter(System.out)); jaroslav@1258: *jaroslav@1258: * jaroslav@1258: *
A surrogate pair is a character represented by a sequence of two jaroslav@1258: * char values: A high surrogate in the range '\uD800' to jaroslav@1258: * '\uDBFF' followed by a low surrogate in the range '\uDC00' to jaroslav@1258: * '\uDFFF'. jaroslav@1258: * jaroslav@1258: *
A malformed surrogate element is a high surrogate that is not jaroslav@1258: * followed by a low surrogate or a low surrogate that is not preceded by a jaroslav@1258: * high surrogate. jaroslav@1258: * jaroslav@1258: *
This class always replaces malformed surrogate elements and unmappable
jaroslav@1258: * character sequences with the charset's default substitution sequence.
jaroslav@1258: * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
jaroslav@1258: * control over the encoding process is required.
jaroslav@1258: *
jaroslav@1258: * @see BufferedWriter
jaroslav@1258: * @see OutputStream
jaroslav@1258: * @see java.nio.charset.Charset
jaroslav@1258: *
jaroslav@1258: * @author Mark Reinhold
jaroslav@1258: * @since JDK1.1
jaroslav@1258: */
jaroslav@1258:
jaroslav@1258: public class OutputStreamWriter extends Writer {
jaroslav@1258:
jaroslav@1258: private final StreamEncoder se;
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Creates an OutputStreamWriter that uses the named charset.
jaroslav@1258: *
jaroslav@1258: * @param out
jaroslav@1258: * An OutputStream
jaroslav@1258: *
jaroslav@1258: * @param charsetName
jaroslav@1258: * The name of a supported
jaroslav@1258: * {@link java.nio.charset.Charset charset}
jaroslav@1258: *
jaroslav@1258: * @exception UnsupportedEncodingException
jaroslav@1258: * If the named encoding is not supported
jaroslav@1258: */
jaroslav@1258: public OutputStreamWriter(OutputStream out, String charsetName)
jaroslav@1258: throws UnsupportedEncodingException
jaroslav@1258: {
jaroslav@1258: super(out);
jaroslav@1258: if (charsetName == null)
jaroslav@1258: throw new NullPointerException("charsetName");
jaroslav@1258: se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Creates an OutputStreamWriter that uses the default character encoding.
jaroslav@1258: *
jaroslav@1258: * @param out An OutputStream
jaroslav@1258: */
jaroslav@1258: public OutputStreamWriter(OutputStream out) {
jaroslav@1258: super(out);
jaroslav@1258: try {
jaroslav@1258: se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
jaroslav@1258: } catch (UnsupportedEncodingException e) {
jaroslav@1258: throw new Error(e);
jaroslav@1258: }
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Creates an OutputStreamWriter that uses the given charset.
If the encoding has an historical name then that name is returned; jaroslav@1258: * otherwise the encoding's canonical name is returned. jaroslav@1258: * jaroslav@1258: *
If this instance was created with the {@link jaroslav@1258: * #OutputStreamWriter(OutputStream, String)} constructor then the returned jaroslav@1258: * name, being unique for the encoding, may differ from the name passed to jaroslav@1258: * the constructor. This method may return null if the stream has jaroslav@1258: * been closed.
jaroslav@1258: * jaroslav@1258: * @return The historical name of this encoding, or possibly jaroslav@1258: *null
if the stream has been closed
jaroslav@1258: *
jaroslav@1258: * @see java.nio.charset.Charset
jaroslav@1258: *
jaroslav@1258: * @revised 1.4
jaroslav@1258: * @spec JSR-51
jaroslav@1258: */
jaroslav@1258: public String getEncoding() {
jaroslav@1258: return se.getEncoding();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Flushes the output buffer to the underlying byte stream, without flushing
jaroslav@1258: * the byte stream itself. This method is non-private only so that it may
jaroslav@1258: * be invoked by PrintStream.
jaroslav@1258: */
jaroslav@1258: void flushBuffer() throws IOException {
jaroslav@1258: se.flushBuffer();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Writes a single character.
jaroslav@1258: *
jaroslav@1258: * @exception IOException If an I/O error occurs
jaroslav@1258: */
jaroslav@1258: public void write(int c) throws IOException {
jaroslav@1258: se.write(c);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Writes a portion of an array of characters.
jaroslav@1258: *
jaroslav@1258: * @param cbuf Buffer of characters
jaroslav@1258: * @param off Offset from which to start writing characters
jaroslav@1258: * @param len Number of characters to write
jaroslav@1258: *
jaroslav@1258: * @exception IOException If an I/O error occurs
jaroslav@1258: */
jaroslav@1258: public void write(char cbuf[], int off, int len) throws IOException {
jaroslav@1258: se.write(cbuf, off, len);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Writes a portion of a string.
jaroslav@1258: *
jaroslav@1258: * @param str A String
jaroslav@1258: * @param off Offset from which to start writing characters
jaroslav@1258: * @param len Number of characters to write
jaroslav@1258: *
jaroslav@1258: * @exception IOException If an I/O error occurs
jaroslav@1258: */
jaroslav@1258: public void write(String str, int off, int len) throws IOException {
jaroslav@1258: se.write(str, off, len);
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: /**
jaroslav@1258: * Flushes the stream.
jaroslav@1258: *
jaroslav@1258: * @exception IOException If an I/O error occurs
jaroslav@1258: */
jaroslav@1258: public void flush() throws IOException {
jaroslav@1258: se.flush();
jaroslav@1258: }
jaroslav@1258:
jaroslav@1258: public void close() throws IOException {
jaroslav@1258: se.close();
jaroslav@1258: }
jaroslav@1258: }