Source Code Cross Referenced for InputStreamReader.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-2005 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.CharsetDecoder;
030        import sun.nio.cs.StreamDecoder;
031
032        /**
033         * An InputStreamReader is a bridge from byte streams to character streams: It
034         * reads bytes and decodes them into characters 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 one of an InputStreamReader's read() methods may
040         * cause one or more bytes to be read from the underlying byte-input stream.
041         * To enable the efficient conversion of bytes to characters, more bytes may
042         * be read ahead from the underlying stream than are necessary to satisfy the
043         * current read operation.
044         *
045         * <p> For top efficiency, consider wrapping an InputStreamReader within a
046         * BufferedReader.  For example:
047         *
048         * <pre>
049         * BufferedReader in
050         *   = new BufferedReader(new InputStreamReader(System.in));
051         * </pre>
052         *
053         * @see BufferedReader
054         * @see InputStream
055         * @see java.nio.charset.Charset
056         *
057         * @version     1.53, 07/05/05
058         * @author      Mark Reinhold
059         * @since       JDK1.1
060         */
061
062        public class InputStreamReader extends Reader {
063
064            private final StreamDecoder sd;
065
066            /**
067             * Creates an InputStreamReader that uses the default charset.
068             *
069             * @param  in   An InputStream
070             */
071            public InputStreamReader(InputStream in) {
072                super (in);
073                try {
074                    sd = StreamDecoder.forInputStreamReader(in, this ,
075                            (String) null); // ## check lock object
076                } catch (UnsupportedEncodingException e) {
077                    // The default encoding should always be available
078                    throw new Error(e);
079                }
080            }
081
082            /**
083             * Creates an InputStreamReader that uses the named charset.
084             *
085             * @param  in
086             *         An InputStream
087             *
088             * @param  charsetName
089             *         The name of a supported
090             *         {@link java.nio.charset.Charset </code>charset<code>}
091             *
092             * @exception  UnsupportedEncodingException
093             *             If the named charset is not supported
094             */
095            public InputStreamReader(InputStream in, String charsetName)
096                    throws UnsupportedEncodingException {
097                super (in);
098                if (charsetName == null)
099                    throw new NullPointerException("charsetName");
100                sd = StreamDecoder.forInputStreamReader(in, this , charsetName);
101            }
102
103            /**
104             * Creates an InputStreamReader that uses the given charset. </p>
105             *
106             * @param  in       An InputStream
107             * @param  cs       A charset
108             *
109             * @since 1.4
110             * @spec JSR-51
111             */
112            public InputStreamReader(InputStream in, Charset cs) {
113                super (in);
114                if (cs == null)
115                    throw new NullPointerException("charset");
116                sd = StreamDecoder.forInputStreamReader(in, this , cs);
117            }
118
119            /**
120             * Creates an InputStreamReader that uses the given charset decoder.  </p>
121             *
122             * @param  in       An InputStream
123             * @param  dec      A charset decoder
124             *
125             * @since 1.4
126             * @spec JSR-51
127             */
128            public InputStreamReader(InputStream in, CharsetDecoder dec) {
129                super (in);
130                if (dec == null)
131                    throw new NullPointerException("charset decoder");
132                sd = StreamDecoder.forInputStreamReader(in, this , dec);
133            }
134
135            /**
136             * Returns the name of the character encoding being used by this stream.
137             *
138             * <p> If the encoding has an historical name then that name is returned;
139             * otherwise the encoding's canonical name is returned.
140             *
141             * <p> If this instance was created with the {@link
142             * #InputStreamReader(InputStream, String)} constructor then the returned
143             * name, being unique for the encoding, may differ from the name passed to
144             * the constructor. This method will return <code>null</code> if the
145             * stream has been closed.
146             * </p>
147             * @return The historical name of this encoding, or
148             *         <code>null</code> if the stream has been closed
149             *
150             * @see java.nio.charset.Charset
151             *
152             * @revised 1.4
153             * @spec JSR-51
154             */
155            public String getEncoding() {
156                return sd.getEncoding();
157            }
158
159            /**
160             * Reads a single character.
161             *
162             * @return The character read, or -1 if the end of the stream has been
163             *         reached
164             *
165             * @exception  IOException  If an I/O error occurs
166             */
167            public int read() throws IOException {
168                return sd.read();
169            }
170
171            /**
172             * Reads characters into a portion of an array.
173             *
174             * @param      cbuf     Destination buffer
175             * @param      offset   Offset at which to start storing characters
176             * @param      length   Maximum number of characters to read
177             *
178             * @return     The number of characters read, or -1 if the end of the 
179             *             stream has been reached
180             *
181             * @exception  IOException  If an I/O error occurs
182             */
183            public int read(char cbuf[], int offset, int length)
184                    throws IOException {
185                return sd.read(cbuf, offset, length);
186            }
187
188            /**
189             * Tells whether this stream is ready to be read.  An InputStreamReader is
190             * ready if its input buffer is not empty, or if bytes are available to be
191             * read from the underlying byte stream.
192             *
193             * @exception  IOException  If an I/O error occurs
194             */
195            public boolean ready() throws IOException {
196                return sd.ready();
197            }
198
199            public void close() throws IOException {
200                sd.close();
201            }
202        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.