Source Code Cross Referenced for OutputStreamWriter.java in  » 6.0-JDK-Core » io-nio » java » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » io nio » java.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.io;
027
028        import java.nio.charset.Charset;
029        import java.nio.charset.CharsetEncoder;
030        import sun.nio.cs.StreamEncoder;
031
032        /**
033         * An OutputStreamWriter is a bridge from character streams to byte streams:
034         * Characters written to it are encoded into bytes using a specified {@link
035         * java.nio.charset.Charset <code>charset</code>}.  The charset that it uses
036         * may be specified by name or may be given explicitly, or the platform's
037         * default charset may be accepted.
038         *
039         * <p> Each invocation of a write() method causes the encoding converter to be
040         * invoked on the given character(s).  The resulting bytes are accumulated in a
041         * buffer before being written to the underlying output stream.  The size of
042         * this buffer may be specified, but by default it is large enough for most
043         * purposes.  Note that the characters passed to the write() methods are not
044         * buffered.
045         *
046         * <p> For top efficiency, consider wrapping an OutputStreamWriter within a
047         * BufferedWriter so as to avoid frequent converter invocations.  For example:
048         *
049         * <pre>
050         * Writer out
051         *   = new BufferedWriter(new OutputStreamWriter(System.out));
052         * </pre>
053         *
054         * <p> A <i>surrogate pair</i> is a character represented by a sequence of two
055         * <tt>char</tt> values: A <i>high</i> surrogate in the range '&#92;uD800' to
056         * '&#92;uDBFF' followed by a <i>low</i> surrogate in the range '&#92;uDC00' to
057         * '&#92;uDFFF'. 
058         *
059         * <p> A <i>malformed surrogate element</i> is a high surrogate that is not
060         * followed by a low surrogate or a low surrogate that is not preceded by a
061         * high surrogate.  
062         * 
063         * <p> This class always replaces malformed surrogate elements and unmappable
064         * character sequences with the charset's default <i>substitution sequence</i>.
065         * The {@linkplain java.nio.charset.CharsetEncoder} class should be used when more
066         * control over the encoding process is required.
067         *
068         * @see BufferedWriter
069         * @see OutputStream
070         * @see java.nio.charset.Charset
071         *
072         * @version 	1.56, 07/05/05
073         * @author	Mark Reinhold
074         * @since	JDK1.1
075         */
076
077        public class OutputStreamWriter extends Writer {
078
079            private final StreamEncoder se;
080
081            /**
082             * Creates an OutputStreamWriter that uses the named charset.
083             *
084             * @param  out
085             *         An OutputStream
086             *
087             * @param  charsetName
088             *         The name of a supported
089             *         {@link java.nio.charset.Charset </code>charset<code>}
090             *
091             * @exception  UnsupportedEncodingException
092             *             If the named encoding is not supported
093             */
094            public OutputStreamWriter(OutputStream out, String charsetName)
095                    throws UnsupportedEncodingException {
096                super (out);
097                if (charsetName == null)
098                    throw new NullPointerException("charsetName");
099                se = StreamEncoder
100                        .forOutputStreamWriter(out, this , charsetName);
101            }
102
103            /**
104             * Creates an OutputStreamWriter that uses the default character encoding.
105             *
106             * @param  out  An OutputStream
107             */
108            public OutputStreamWriter(OutputStream out) {
109                super (out);
110                try {
111                    se = StreamEncoder.forOutputStreamWriter(out, this ,
112                            (String) null);
113                } catch (UnsupportedEncodingException e) {
114                    throw new Error(e);
115                }
116            }
117
118            /**
119             * Creates an OutputStreamWriter that uses the given charset. </p>
120             *
121             * @param  out
122             *         An OutputStream
123             *
124             * @param  cs
125             *         A charset
126             *
127             * @since 1.4
128             * @spec JSR-51
129             */
130            public OutputStreamWriter(OutputStream out, Charset cs) {
131                super (out);
132                if (cs == null)
133                    throw new NullPointerException("charset");
134                se = StreamEncoder.forOutputStreamWriter(out, this , cs);
135            }
136
137            /**
138             * Creates an OutputStreamWriter that uses the given charset encoder.  </p>
139             *
140             * @param  out
141             *         An OutputStream
142             *
143             * @param  enc
144             *         A charset encoder
145             *
146             * @since 1.4
147             * @spec JSR-51
148             */
149            public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
150                super (out);
151                if (enc == null)
152                    throw new NullPointerException("charset encoder");
153                se = StreamEncoder.forOutputStreamWriter(out, this , enc);
154            }
155
156            /**
157             * Returns the name of the character encoding being used by this stream.
158             *
159             * <p> If the encoding has an historical name then that name is returned;
160             * otherwise the encoding's canonical name is returned.
161             *
162             * <p> If this instance was created with the {@link
163             * #OutputStreamWriter(OutputStream, String)} constructor then the returned
164             * name, being unique for the encoding, may differ from the name passed to
165             * the constructor.  This method may return <tt>null</tt> if the stream has
166             * been closed. </p>
167             *
168             * @return The historical name of this encoding, or possibly
169             *         <code>null</code> if the stream has been closed
170             *
171             * @see java.nio.charset.Charset
172             *
173             * @revised 1.4
174             * @spec JSR-51
175             */
176            public String getEncoding() {
177                return se.getEncoding();
178            }
179
180            /**
181             * Flushes the output buffer to the underlying byte stream, without flushing
182             * the byte stream itself.  This method is non-private only so that it may
183             * be invoked by PrintStream.
184             */
185            void flushBuffer() throws IOException {
186                se.flushBuffer();
187            }
188
189            /**
190             * Writes a single character.
191             *
192             * @exception  IOException  If an I/O error occurs
193             */
194            public void write(int c) throws IOException {
195                se.write(c);
196            }
197
198            /**
199             * Writes a portion of an array of characters.
200             *
201             * @param  cbuf  Buffer of characters
202             * @param  off   Offset from which to start writing characters
203             * @param  len   Number of characters to write
204             *
205             * @exception  IOException  If an I/O error occurs
206             */
207            public void write(char cbuf[], int off, int len) throws IOException {
208                se.write(cbuf, off, len);
209            }
210
211            /**
212             * Writes a portion of a string.
213             *
214             * @param  str  A String
215             * @param  off  Offset from which to start writing characters
216             * @param  len  Number of characters to write
217             *
218             * @exception  IOException  If an I/O error occurs
219             */
220            public void write(String str, int off, int len) throws IOException {
221                se.write(str, off, len);
222            }
223
224            /**
225             * Flushes the stream.
226             *
227             * @exception  IOException  If an I/O error occurs
228             */
229            public void flush() throws IOException {
230                se.flush();
231            }
232
233            public void close() throws IOException {
234                se.close();
235            }
236        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.