jaroslav@557: /* jaroslav@557: * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved. jaroslav@557: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@557: * jaroslav@557: * This code is free software; you can redistribute it and/or modify it jaroslav@557: * under the terms of the GNU General Public License version 2 only, as jaroslav@557: * published by the Free Software Foundation. Oracle designates this jaroslav@557: * particular file as subject to the "Classpath" exception as provided jaroslav@557: * by Oracle in the LICENSE file that accompanied this code. jaroslav@557: * jaroslav@557: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@557: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@557: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@557: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@557: * accompanied this code). jaroslav@557: * jaroslav@557: * You should have received a copy of the GNU General Public License version jaroslav@557: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@557: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@557: * jaroslav@557: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@557: * or visit www.oracle.com if you need additional information or have any jaroslav@557: * questions. jaroslav@557: */ jaroslav@557: jaroslav@557: package java.io; jaroslav@557: jaroslav@557: import java.nio.charset.Charset; jaroslav@557: import java.nio.charset.CharsetDecoder; jaroslav@557: import sun.nio.cs.StreamDecoder; jaroslav@557: jaroslav@557: jaroslav@557: /** jaroslav@557: * An InputStreamReader is a bridge from byte streams to character streams: It jaroslav@557: * reads bytes and decodes them into characters using a specified {@link jaroslav@557: * java.nio.charset.Charset charset}. The charset that it uses jaroslav@557: * may be specified by name or may be given explicitly, or the platform's jaroslav@557: * default charset may be accepted. jaroslav@557: * jaroslav@557: *

Each invocation of one of an InputStreamReader's read() methods may jaroslav@557: * cause one or more bytes to be read from the underlying byte-input stream. jaroslav@557: * To enable the efficient conversion of bytes to characters, more bytes may jaroslav@557: * be read ahead from the underlying stream than are necessary to satisfy the jaroslav@557: * current read operation. jaroslav@557: * jaroslav@557: *

For top efficiency, consider wrapping an InputStreamReader within a jaroslav@557: * BufferedReader. For example: jaroslav@557: * jaroslav@557: *

jaroslav@557:  * BufferedReader in
jaroslav@557:  *   = new BufferedReader(new InputStreamReader(System.in));
jaroslav@557:  * 
jaroslav@557: * jaroslav@557: * @see BufferedReader jaroslav@557: * @see InputStream jaroslav@557: * @see java.nio.charset.Charset jaroslav@557: * jaroslav@557: * @author Mark Reinhold jaroslav@557: * @since JDK1.1 jaroslav@557: */ jaroslav@557: jaroslav@557: public class InputStreamReader extends Reader { jaroslav@557: jaroslav@557: private final StreamDecoder sd; jaroslav@557: jaroslav@557: /** jaroslav@557: * Creates an InputStreamReader that uses the default charset. jaroslav@557: * jaroslav@557: * @param in An InputStream jaroslav@557: */ jaroslav@557: public InputStreamReader(InputStream in) { jaroslav@557: super(in); jaroslav@557: try { jaroslav@557: sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object jaroslav@557: } catch (UnsupportedEncodingException e) { jaroslav@557: // The default encoding should always be available jaroslav@557: throw new Error(e); jaroslav@557: } jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Creates an InputStreamReader that uses the named charset. jaroslav@557: * jaroslav@557: * @param in jaroslav@557: * An InputStream jaroslav@557: * jaroslav@557: * @param charsetName jaroslav@557: * The name of a supported jaroslav@557: * {@link java.nio.charset.Charset charset} jaroslav@557: * jaroslav@557: * @exception UnsupportedEncodingException jaroslav@557: * If the named charset is not supported jaroslav@557: */ jaroslav@557: public InputStreamReader(InputStream in, String charsetName) jaroslav@557: throws UnsupportedEncodingException jaroslav@557: { jaroslav@557: super(in); jaroslav@557: if (charsetName == null) jaroslav@557: throw new NullPointerException("charsetName"); jaroslav@557: sd = StreamDecoder.forInputStreamReader(in, this, charsetName); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Creates an InputStreamReader that uses the given charset.

jaroslav@557: * jaroslav@557: * @param in An InputStream jaroslav@557: * @param cs A charset jaroslav@557: * jaroslav@557: * @since 1.4 jaroslav@557: * @spec JSR-51 jaroslav@557: */ jaroslav@560: /* XXX: jaroslav@557: public InputStreamReader(InputStream in, Charset cs) { jaroslav@557: super(in); jaroslav@557: if (cs == null) jaroslav@557: throw new NullPointerException("charset"); jaroslav@557: sd = StreamDecoder.forInputStreamReader(in, this, cs); jaroslav@557: } jaroslav@560: */ jaroslav@557: /** jaroslav@557: * Creates an InputStreamReader that uses the given charset decoder.

jaroslav@557: * jaroslav@557: * @param in An InputStream jaroslav@557: * @param dec A charset decoder jaroslav@557: * jaroslav@557: * @since 1.4 jaroslav@557: * @spec JSR-51 jaroslav@557: */ jaroslav@560: /* XXX: jaroslav@557: public InputStreamReader(InputStream in, CharsetDecoder dec) { jaroslav@557: super(in); jaroslav@557: if (dec == null) jaroslav@557: throw new NullPointerException("charset decoder"); jaroslav@557: sd = StreamDecoder.forInputStreamReader(in, this, dec); jaroslav@557: } jaroslav@560: */ jaroslav@560: jaroslav@557: /** jaroslav@557: * Returns the name of the character encoding being used by this stream. jaroslav@557: * jaroslav@557: *

If the encoding has an historical name then that name is returned; jaroslav@557: * otherwise the encoding's canonical name is returned. jaroslav@557: * jaroslav@557: *

If this instance was created with the {@link jaroslav@557: * #InputStreamReader(InputStream, String)} constructor then the returned jaroslav@557: * name, being unique for the encoding, may differ from the name passed to jaroslav@557: * the constructor. This method will return null if the jaroslav@557: * stream has been closed. jaroslav@557: *

jaroslav@557: * @return The historical name of this encoding, or jaroslav@557: * null if the stream has been closed jaroslav@557: * jaroslav@557: * @see java.nio.charset.Charset jaroslav@557: * jaroslav@557: * @revised 1.4 jaroslav@557: * @spec JSR-51 jaroslav@557: */ jaroslav@557: public String getEncoding() { jaroslav@557: return sd.getEncoding(); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Reads a single character. jaroslav@557: * jaroslav@557: * @return The character read, or -1 if the end of the stream has been jaroslav@557: * reached jaroslav@557: * jaroslav@557: * @exception IOException If an I/O error occurs jaroslav@557: */ jaroslav@557: public int read() throws IOException { jaroslav@557: return sd.read(); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Reads characters into a portion of an array. jaroslav@557: * jaroslav@557: * @param cbuf Destination buffer jaroslav@557: * @param offset Offset at which to start storing characters jaroslav@557: * @param length Maximum number of characters to read jaroslav@557: * jaroslav@557: * @return The number of characters read, or -1 if the end of the jaroslav@557: * stream has been reached jaroslav@557: * jaroslav@557: * @exception IOException If an I/O error occurs jaroslav@557: */ jaroslav@557: public int read(char cbuf[], int offset, int length) throws IOException { jaroslav@557: return sd.read(cbuf, offset, length); jaroslav@557: } jaroslav@557: jaroslav@557: /** jaroslav@557: * Tells whether this stream is ready to be read. An InputStreamReader is jaroslav@557: * ready if its input buffer is not empty, or if bytes are available to be jaroslav@557: * read from the underlying byte stream. jaroslav@557: * jaroslav@557: * @exception IOException If an I/O error occurs jaroslav@557: */ jaroslav@557: public boolean ready() throws IOException { jaroslav@557: return sd.ready(); jaroslav@557: } jaroslav@557: jaroslav@557: public void close() throws IOException { jaroslav@557: sd.close(); jaroslav@557: } jaroslav@557: }