Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/InputStreamReader.java
38829 views
/*1* Copyright (c) 1996, 2013, 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.charset.Charset;28import java.nio.charset.CharsetDecoder;29import sun.nio.cs.StreamDecoder;303132/**33* An InputStreamReader is a bridge from byte streams to character streams: It34* reads bytes and decodes them into characters using a specified {@link35* java.nio.charset.Charset charset}. The charset that it uses36* may be specified by name or may be given explicitly, or the platform's37* default charset may be accepted.38*39* <p> Each invocation of one of an InputStreamReader's read() methods may40* cause one or more bytes to be read from the underlying byte-input stream.41* To enable the efficient conversion of bytes to characters, more bytes may42* be read ahead from the underlying stream than are necessary to satisfy the43* current read operation.44*45* <p> For top efficiency, consider wrapping an InputStreamReader within a46* BufferedReader. For example:47*48* <pre>49* BufferedReader in50* = new BufferedReader(new InputStreamReader(System.in));51* </pre>52*53* @see BufferedReader54* @see InputStream55* @see java.nio.charset.Charset56*57* @author Mark Reinhold58* @since JDK1.159*/6061public class InputStreamReader extends Reader {6263private final StreamDecoder sd;6465/**66* Creates an InputStreamReader that uses the default charset.67*68* @param in An InputStream69*/70public InputStreamReader(InputStream in) {71super(in);72try {73sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object74} catch (UnsupportedEncodingException e) {75// The default encoding should always be available76throw new Error(e);77}78}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* @exception 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* @spec JSR-51110*/111public InputStreamReader(InputStream in, Charset cs) {112super(in);113if (cs == null)114throw new NullPointerException("charset");115sd = StreamDecoder.forInputStreamReader(in, this, cs);116}117118/**119* Creates an InputStreamReader that uses the given charset decoder.120*121* @param in An InputStream122* @param dec A charset decoder123*124* @since 1.4125* @spec JSR-51126*/127public InputStreamReader(InputStream in, CharsetDecoder dec) {128super(in);129if (dec == null)130throw new NullPointerException("charset decoder");131sd = StreamDecoder.forInputStreamReader(in, this, dec);132}133134/**135* Returns the name of the character encoding being used by this stream.136*137* <p> If the encoding has an historical name then that name is returned;138* otherwise the encoding's canonical name is returned.139*140* <p> If this instance was created with the {@link141* #InputStreamReader(InputStream, String)} constructor then the returned142* name, being unique for the encoding, may differ from the name passed to143* the constructor. This method will return <code>null</code> if the144* stream has been closed.145* </p>146* @return The historical name of this encoding, or147* <code>null</code> if the stream has been closed148*149* @see java.nio.charset.Charset150*151* @revised 1.4152* @spec JSR-51153*/154public String getEncoding() {155return sd.getEncoding();156}157158/**159* Reads a single character.160*161* @return The character read, or -1 if the end of the stream has been162* reached163*164* @exception IOException If an I/O error occurs165*/166public int read() throws IOException {167return sd.read();168}169170/**171* Reads characters into a portion of an array.172*173* @param cbuf Destination buffer174* @param offset Offset at which to start storing characters175* @param length Maximum number of characters to read176*177* @return The number of characters read, or -1 if the end of the178* stream has been reached179*180* @exception IOException If an I/O error occurs181*/182public int read(char cbuf[], int offset, int length) throws IOException {183return sd.read(cbuf, offset, length);184}185186/**187* Tells whether this stream is ready to be read. An InputStreamReader is188* ready if its input buffer is not empty, or if bytes are available to be189* read from the underlying byte stream.190*191* @exception IOException If an I/O error occurs192*/193public boolean ready() throws IOException {194return sd.ready();195}196197public void close() throws IOException {198sd.close();199}200}201202203