0N/A/*
2362N/A * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/Aimport java.nio.charset.Charset;
0N/Aimport java.nio.charset.CharsetDecoder;
0N/Aimport sun.nio.cs.StreamDecoder;
0N/A
0N/A
0N/A/**
0N/A * An InputStreamReader is a bridge from byte streams to character streams: It
0N/A * reads bytes and decodes them into characters using a specified {@link
0N/A * java.nio.charset.Charset <code>charset</code>}. The charset that it uses
0N/A * may be specified by name or may be given explicitly, or the platform's
0N/A * default charset may be accepted.
0N/A *
0N/A * <p> Each invocation of one of an InputStreamReader's read() methods may
0N/A * cause one or more bytes to be read from the underlying byte-input stream.
0N/A * To enable the efficient conversion of bytes to characters, more bytes may
0N/A * be read ahead from the underlying stream than are necessary to satisfy the
0N/A * current read operation.
0N/A *
0N/A * <p> For top efficiency, consider wrapping an InputStreamReader within a
0N/A * BufferedReader. For example:
0N/A *
0N/A * <pre>
0N/A * BufferedReader in
0N/A * = new BufferedReader(new InputStreamReader(System.in));
0N/A * </pre>
0N/A *
0N/A * @see BufferedReader
0N/A * @see InputStream
0N/A * @see java.nio.charset.Charset
0N/A *
0N/A * @author Mark Reinhold
0N/A * @since JDK1.1
0N/A */
0N/A
0N/Apublic class InputStreamReader extends Reader {
0N/A
0N/A private final StreamDecoder sd;
0N/A
0N/A /**
0N/A * Creates an InputStreamReader that uses the default charset.
0N/A *
0N/A * @param in An InputStream
0N/A */
0N/A public InputStreamReader(InputStream in) {
0N/A super(in);
0N/A try {
0N/A sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
0N/A } catch (UnsupportedEncodingException e) {
0N/A // The default encoding should always be available
0N/A throw new Error(e);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates an InputStreamReader that uses the named charset.
0N/A *
0N/A * @param in
0N/A * An InputStream
0N/A *
0N/A * @param charsetName
0N/A * The name of a supported
0N/A * {@link java.nio.charset.Charset </code>charset<code>}
0N/A *
0N/A * @exception UnsupportedEncodingException
0N/A * If the named charset is not supported
0N/A */
0N/A public InputStreamReader(InputStream in, String charsetName)
0N/A throws UnsupportedEncodingException
0N/A {
0N/A super(in);
0N/A if (charsetName == null)
0N/A throw new NullPointerException("charsetName");
0N/A sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
0N/A }
0N/A
0N/A /**
0N/A * Creates an InputStreamReader that uses the given charset. </p>
0N/A *
0N/A * @param in An InputStream
0N/A * @param cs A charset
0N/A *
0N/A * @since 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public InputStreamReader(InputStream in, Charset cs) {
0N/A super(in);
0N/A if (cs == null)
0N/A throw new NullPointerException("charset");
0N/A sd = StreamDecoder.forInputStreamReader(in, this, cs);
0N/A }
0N/A
0N/A /**
0N/A * Creates an InputStreamReader that uses the given charset decoder. </p>
0N/A *
0N/A * @param in An InputStream
0N/A * @param dec A charset decoder
0N/A *
0N/A * @since 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public InputStreamReader(InputStream in, CharsetDecoder dec) {
0N/A super(in);
0N/A if (dec == null)
0N/A throw new NullPointerException("charset decoder");
0N/A sd = StreamDecoder.forInputStreamReader(in, this, dec);
0N/A }
0N/A
0N/A /**
0N/A * Returns the name of the character encoding being used by this stream.
0N/A *
0N/A * <p> If the encoding has an historical name then that name is returned;
0N/A * otherwise the encoding's canonical name is returned.
0N/A *
0N/A * <p> If this instance was created with the {@link
0N/A * #InputStreamReader(InputStream, String)} constructor then the returned
0N/A * name, being unique for the encoding, may differ from the name passed to
0N/A * the constructor. This method will return <code>null</code> if the
0N/A * stream has been closed.
0N/A * </p>
0N/A * @return The historical name of this encoding, or
0N/A * <code>null</code> if the stream has been closed
0N/A *
0N/A * @see java.nio.charset.Charset
0N/A *
0N/A * @revised 1.4
0N/A * @spec JSR-51
0N/A */
0N/A public String getEncoding() {
0N/A return sd.getEncoding();
0N/A }
0N/A
0N/A /**
0N/A * Reads a single character.
0N/A *
0N/A * @return The character read, or -1 if the end of the stream has been
0N/A * reached
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public int read() throws IOException {
0N/A return sd.read();
0N/A }
0N/A
0N/A /**
0N/A * Reads characters into a portion of an array.
0N/A *
0N/A * @param cbuf Destination buffer
0N/A * @param offset Offset at which to start storing characters
0N/A * @param length Maximum number of characters to read
0N/A *
0N/A * @return The number of characters read, or -1 if the end of the
0N/A * stream has been reached
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public int read(char cbuf[], int offset, int length) throws IOException {
0N/A return sd.read(cbuf, offset, length);
0N/A }
0N/A
0N/A /**
0N/A * Tells whether this stream is ready to be read. An InputStreamReader is
0N/A * ready if its input buffer is not empty, or if bytes are available to be
0N/A * read from the underlying byte stream.
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public boolean ready() throws IOException {
0N/A return sd.ready();
0N/A }
0N/A
0N/A public void close() throws IOException {
0N/A sd.close();
0N/A }
0N/A}