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: 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: /** 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: } 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@561: if (!charsetName.toUpperCase().equals("UTF-8")) { jaroslav@561: throw new UnsupportedEncodingException(charsetName); jaroslav@561: } 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@561: return "UTF-8"; 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@595: final InputStream is = (InputStream)lock; jaroslav@595: int c = is.read(); jaroslav@595: if (c == -1) { jaroslav@595: return -1; jaroslav@595: } jaroslav@595: c = (int) c & 0xff; jaroslav@595: switch (c >> 4) { jaroslav@595: case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: jaroslav@595: /* 0xxxxxxx*/ jaroslav@595: return c; jaroslav@595: case 12: case 13: { jaroslav@595: /* 110x xxxx 10xx xxxx*/ jaroslav@595: int char2 = (int) is.read(); jaroslav@595: if ((char2 & 0xC0) != 0x80) jaroslav@595: throw new UTFDataFormatException("malformed input"); jaroslav@595: return (((c & 0x1F) << 6) | (char2 & 0x3F)); jaroslav@595: } jaroslav@595: case 14: { jaroslav@595: /* 1110 xxxx 10xx xxxx 10xx xxxx */ jaroslav@595: int char2 = is.read(); jaroslav@595: int char3 = is.read(); jaroslav@595: if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) jaroslav@595: throw new UTFDataFormatException("malformed input"); jaroslav@595: return (((c & 0x0F) << 12) | jaroslav@595: ((char2 & 0x3F) << 6) | jaroslav@595: ((char3 & 0x3F) << 0)); jaroslav@595: } jaroslav@595: default: jaroslav@595: /* 10xx xxxx, 1111 xxxx */ jaroslav@595: throw new UTFDataFormatException("malformed input"); jaroslav@595: } jaroslav@557: } jaroslav@595: 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@561: for (int i = 0; i < length; i++) { jaroslav@561: int ch = read(); jaroslav@561: if (ch == -1) { jaroslav@561: if (i == 0) return -1; jaroslav@561: return i; jaroslav@561: } jaroslav@561: cbuf[offset++] = (char) ch; jaroslav@561: } jaroslav@561: return 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@561: return ((InputStream)lock).available() > 0; jaroslav@557: } jaroslav@557: jaroslav@557: public void close() throws IOException { jaroslav@561: ((InputStream)lock).close(); jaroslav@557: } jaroslav@557: }