Path: blob/master/src/java.base/share/classes/java/io/InputStreamReader.java
41152 views
/*1* Copyright (c) 1996, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627import java.nio.CharBuffer;28import java.nio.charset.Charset;29import java.nio.charset.CharsetDecoder;30import sun.nio.cs.StreamDecoder;313233/**34* An InputStreamReader is a bridge from byte streams to character streams: It35* reads bytes and decodes them into characters using a specified {@link36* java.nio.charset.Charset charset}. The charset that it uses37* may be specified by name or may be given explicitly, or the platform's38* {@link Charset#defaultCharset() default charset} may be accepted.39*40* <p> Each invocation of one of an InputStreamReader's read() methods may41* cause one or more bytes to be read from the underlying byte-input stream.42* To enable the efficient conversion of bytes to characters, more bytes may43* be read ahead from the underlying stream than are necessary to satisfy the44* current read operation.45*46* <p> For top efficiency, consider wrapping an InputStreamReader within a47* BufferedReader. For example:48*49* <pre>50* BufferedReader in51* = new BufferedReader(new InputStreamReader(anInputStream));52* </pre>53*54* @see BufferedReader55* @see InputStream56* @see java.nio.charset.Charset57*58* @author Mark Reinhold59* @since 1.160*/6162public class InputStreamReader extends Reader {6364private final StreamDecoder sd;6566/**67* Creates an InputStreamReader that uses the68* {@link Charset#defaultCharset() default charset}.69*70* @param in An InputStream71*72* @see Charset#defaultCharset()73*/74public InputStreamReader(InputStream in) {75super(in);76sd = StreamDecoder.forInputStreamReader(in, this,77Charset.defaultCharset()); // ## check lock object78}7980/**81* Creates an InputStreamReader that uses the named charset.82*83* @param in84* An InputStream85*86* @param charsetName87* The name of a supported88* {@link java.nio.charset.Charset charset}89*90* @throws UnsupportedEncodingException91* If the named charset is not supported92*/93public InputStreamReader(InputStream in, String charsetName)94throws UnsupportedEncodingException95{96super(in);97if (charsetName == null)98throw new NullPointerException("charsetName");99sd = StreamDecoder.forInputStreamReader(in, this, charsetName);100}101102/**103* Creates an InputStreamReader that uses the given charset.104*105* @param in An InputStream106* @param cs A charset107*108* @since 1.4109*/110public InputStreamReader(InputStream in, Charset cs) {111super(in);112if (cs == null)113throw new NullPointerException("charset");114sd = StreamDecoder.forInputStreamReader(in, this, cs);115}116117/**118* Creates an InputStreamReader that uses the given charset decoder.119*120* @param in An InputStream121* @param dec A charset decoder122*123* @since 1.4124*/125public InputStreamReader(InputStream in, CharsetDecoder dec) {126super(in);127if (dec == null)128throw new NullPointerException("charset decoder");129sd = StreamDecoder.forInputStreamReader(in, this, dec);130}131132/**133* Returns the name of the character encoding being used by this stream.134*135* <p> If the encoding has an historical name then that name is returned;136* otherwise the encoding's canonical name is returned.137*138* <p> If this instance was created with the {@link139* #InputStreamReader(InputStream, String)} constructor then the returned140* name, being unique for the encoding, may differ from the name passed to141* the constructor. This method will return {@code null} if the142* stream has been closed.143* </p>144* @return The historical name of this encoding, or145* {@code null} if the stream has been closed146*147* @see java.nio.charset.Charset148*149* @revised 1.4150*/151public String getEncoding() {152return sd.getEncoding();153}154155public int read(CharBuffer target) throws IOException {156return sd.read(target);157}158159/**160* Reads a single character.161*162* @return The character read, or -1 if the end of the stream has been163* reached164*165* @throws IOException If an I/O error occurs166*/167public int read() throws IOException {168return sd.read();169}170171/**172* {@inheritDoc}173* @throws IndexOutOfBoundsException {@inheritDoc}174*/175public int read(char[] cbuf, int off, int len) throws IOException {176return sd.read(cbuf, off, len);177}178179/**180* Tells whether this stream is ready to be read. An InputStreamReader is181* ready if its input buffer is not empty, or if bytes are available to be182* read from the underlying byte stream.183*184* @throws IOException If an I/O error occurs185*/186public boolean ready() throws IOException {187return sd.ready();188}189190public void close() throws IOException {191sd.close();192}193}194195196