1 /*
  2  * Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  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.
 10  *
 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).
 16  *
 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.
 20  *
 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
 23  * questions.
 24  */
 25 
 26 package java.io;
 27 
 28 import java.nio.CharBuffer;
 29 import java.nio.charset.Charset;
 30 import java.nio.charset.CharsetDecoder;
 31 import java.util.Objects;
 32 import sun.nio.cs.StreamDecoder;
 33 
 34 /**
 35  * An InputStreamReader is a bridge from byte streams to character streams: It
 36  * reads bytes and decodes them into characters using a specified {@link
 37  * Charset charset}.  The charset that it uses
 38  * may be specified by name or may be given explicitly, or the
 39  * {@link Charset#defaultCharset() default charset} may be used.
 40  *
 41  * <p> Each invocation of one of an InputStreamReader's read() methods may
 42  * cause one or more bytes to be read from the underlying byte-input stream.
 43  * To enable the efficient conversion of bytes to characters, more bytes may
 44  * be read ahead from the underlying stream than are necessary to satisfy the
 45  * current read operation.
 46  *
 47  * <p> For top efficiency, consider wrapping an InputStreamReader within a
 48  * BufferedReader.  For example:
 49  *
 50  * <pre>
 51  * BufferedReader in
 52  *   = new BufferedReader(new InputStreamReader(anInputStream));
 53  * </pre>
 54  *
 55  * @see BufferedReader
 56  * @see InputStream
 57  * @see Charset
 58  *
 59  * @author      Mark Reinhold
 60  * @since       1.1
 61  */
 62 
 63 public class InputStreamReader extends Reader {
 64     private final StreamDecoder sd;
 65 
 66     /**
 67      * Creates an InputStreamReader that uses the
 68      * {@link Charset#defaultCharset() default charset}.
 69      *
 70      * @param  in   An InputStream
 71      *
 72      * @see Charset#defaultCharset()
 73      */
 74     public InputStreamReader(InputStream in) {
 75         Objects.requireNonNull(in);
 76         sd = StreamDecoder.forInputStreamReader(in, lock, Charset.defaultCharset());
 77     }
 78 
 79     /**
 80      * Creates an InputStreamReader that uses the named charset.
 81      *
 82      * @param  in
 83      *         An InputStream
 84      *
 85      * @param  charsetName
 86      *         The name of a supported {@link Charset charset}
 87      *
 88      * @throws     UnsupportedEncodingException
 89      *             If the named charset is not supported
 90      */
 91     public InputStreamReader(InputStream in, String charsetName)
 92         throws UnsupportedEncodingException
 93     {
 94         Objects.requireNonNull(in);
 95         Objects.requireNonNull(charsetName, "charsetName");
 96         sd = StreamDecoder.forInputStreamReader(in, lock, charsetName);
 97     }
 98 
 99     /**
100      * Creates an InputStreamReader that uses the given charset.
101      *
102      * @param  in       An InputStream
103      * @param  cs       A charset
104      *
105      * @since 1.4
106      */
107     public InputStreamReader(InputStream in, Charset cs) {
108         Objects.requireNonNull(in);
109         Objects.requireNonNull(cs, "charset");
110         sd = StreamDecoder.forInputStreamReader(in, lock, cs);
111     }
112 
113     /**
114      * Creates an InputStreamReader that uses the given charset decoder.
115      *
116      * @param  in       An InputStream
117      * @param  dec      A charset decoder
118      *
119      * @since 1.4
120      */
121     public InputStreamReader(InputStream in, CharsetDecoder dec) {
122         Objects.requireNonNull(dec, "charset decoder");
123         sd = StreamDecoder.forInputStreamReader(in, lock, dec);
124     }
125 
126     /**
127      * Returns the name of the character encoding being used by this stream.
128      *
129      * <p> If the encoding has an historical name then that name is returned;
130      * otherwise the encoding's canonical name is returned.
131      *
132      * <p> If this instance was created with the {@link
133      * #InputStreamReader(InputStream, String)} constructor then the returned
134      * name, being unique for the encoding, may differ from the name passed to
135      * the constructor. This method will return {@code null} if the
136      * stream has been closed.
137      * </p>
138      * @return The historical name of this encoding, or
139      *         {@code null} if the stream has been closed
140      *
141      * @see Charset
142      *
143      * @revised 1.4
144      */
145     public String getEncoding() {
146         return sd.getEncoding();
147     }
148 
149     public int read(CharBuffer target) throws IOException {
150         return sd.read(target);
151     }
152 
153     /**
154      * Reads a single character.
155      *
156      * @return The character read, or -1 if the end of the stream has been
157      *         reached
158      *
159      * @throws     IOException  If an I/O error occurs
160      */
161     public int read() throws IOException {
162         return sd.read();
163     }
164 
165     /**
166      * {@inheritDoc}
167      * @throws     IndexOutOfBoundsException  {@inheritDoc}
168      */
169     public int read(char[] cbuf, int off, int len) throws IOException {
170         return sd.read(cbuf, off, len);
171     }
172 
173     /**
174      * Tells whether this stream is ready to be read.  An InputStreamReader is
175      * ready if its input buffer is not empty, or if bytes are available to be
176      * read from the underlying byte stream.
177      *
178      * @throws     IOException  If an I/O error occurs
179      */
180     public boolean ready() throws IOException {
181         return sd.ready();
182     }
183 
184     public void close() throws IOException {
185         sd.close();
186     }
187 }