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: }
|