rt/emul/compact/src/main/java/java/io/InputStreamReader.java
changeset 1985 cd1cc103a03c
parent 595 784aaf9ee179
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/emul/compact/src/main/java/java/io/InputStreamReader.java	Tue Jan 17 07:04:06 2017 +0100
     1.3 @@ -0,0 +1,230 @@
     1.4 +/*
     1.5 + * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.io;
    1.30 +
    1.31 +
    1.32 +/**
    1.33 + * An InputStreamReader is a bridge from byte streams to character streams: It
    1.34 + * reads bytes and decodes them into characters using a specified {@link
    1.35 + * java.nio.charset.Charset <code>charset</code>}.  The charset that it uses
    1.36 + * may be specified by name or may be given explicitly, or the platform's
    1.37 + * default charset may be accepted.
    1.38 + *
    1.39 + * <p> Each invocation of one of an InputStreamReader's read() methods may
    1.40 + * cause one or more bytes to be read from the underlying byte-input stream.
    1.41 + * To enable the efficient conversion of bytes to characters, more bytes may
    1.42 + * be read ahead from the underlying stream than are necessary to satisfy the
    1.43 + * current read operation.
    1.44 + *
    1.45 + * <p> For top efficiency, consider wrapping an InputStreamReader within a
    1.46 + * BufferedReader.  For example:
    1.47 + *
    1.48 + * <pre>
    1.49 + * BufferedReader in
    1.50 + *   = new BufferedReader(new InputStreamReader(System.in));
    1.51 + * </pre>
    1.52 + *
    1.53 + * @see BufferedReader
    1.54 + * @see InputStream
    1.55 + * @see java.nio.charset.Charset
    1.56 + *
    1.57 + * @author      Mark Reinhold
    1.58 + * @since       JDK1.1
    1.59 + */
    1.60 +
    1.61 +public class InputStreamReader extends Reader {
    1.62 +
    1.63 +    /**
    1.64 +     * Creates an InputStreamReader that uses the default charset.
    1.65 +     *
    1.66 +     * @param  in   An InputStream
    1.67 +     */
    1.68 +    public InputStreamReader(InputStream in) {
    1.69 +        super(in);
    1.70 +    }
    1.71 +
    1.72 +    /**
    1.73 +     * Creates an InputStreamReader that uses the named charset.
    1.74 +     *
    1.75 +     * @param  in
    1.76 +     *         An InputStream
    1.77 +     *
    1.78 +     * @param  charsetName
    1.79 +     *         The name of a supported
    1.80 +     *         {@link java.nio.charset.Charset </code>charset<code>}
    1.81 +     *
    1.82 +     * @exception  UnsupportedEncodingException
    1.83 +     *             If the named charset is not supported
    1.84 +     */
    1.85 +    public InputStreamReader(InputStream in, String charsetName)
    1.86 +        throws UnsupportedEncodingException
    1.87 +    {
    1.88 +        super(in);
    1.89 +        if (!charsetName.toUpperCase().equals("UTF-8")) {
    1.90 +            throw new UnsupportedEncodingException(charsetName);
    1.91 +        }
    1.92 +    }
    1.93 +
    1.94 +    /**
    1.95 +     * Creates an InputStreamReader that uses the given charset. </p>
    1.96 +     *
    1.97 +     * @param  in       An InputStream
    1.98 +     * @param  cs       A charset
    1.99 +     *
   1.100 +     * @since 1.4
   1.101 +     * @spec JSR-51
   1.102 +     */
   1.103 +/* XXX:
   1.104 +    public InputStreamReader(InputStream in, Charset cs) {
   1.105 +        super(in);
   1.106 +        if (cs == null)
   1.107 +            throw new NullPointerException("charset");
   1.108 +        sd = StreamDecoder.forInputStreamReader(in, this, cs);
   1.109 +    }
   1.110 +*/
   1.111 +    /**
   1.112 +     * Creates an InputStreamReader that uses the given charset decoder.  </p>
   1.113 +     *
   1.114 +     * @param  in       An InputStream
   1.115 +     * @param  dec      A charset decoder
   1.116 +     *
   1.117 +     * @since 1.4
   1.118 +     * @spec JSR-51
   1.119 +     */
   1.120 +/* XXX:
   1.121 +    public InputStreamReader(InputStream in, CharsetDecoder dec) {
   1.122 +        super(in);
   1.123 +        if (dec == null)
   1.124 +            throw new NullPointerException("charset decoder");
   1.125 +        sd = StreamDecoder.forInputStreamReader(in, this, dec);
   1.126 +    }
   1.127 +*/
   1.128 +    
   1.129 +    /**
   1.130 +     * Returns the name of the character encoding being used by this stream.
   1.131 +     *
   1.132 +     * <p> If the encoding has an historical name then that name is returned;
   1.133 +     * otherwise the encoding's canonical name is returned.
   1.134 +     *
   1.135 +     * <p> If this instance was created with the {@link
   1.136 +     * #InputStreamReader(InputStream, String)} constructor then the returned
   1.137 +     * name, being unique for the encoding, may differ from the name passed to
   1.138 +     * the constructor. This method will return <code>null</code> if the
   1.139 +     * stream has been closed.
   1.140 +     * </p>
   1.141 +     * @return The historical name of this encoding, or
   1.142 +     *         <code>null</code> if the stream has been closed
   1.143 +     *
   1.144 +     * @see java.nio.charset.Charset
   1.145 +     *
   1.146 +     * @revised 1.4
   1.147 +     * @spec JSR-51
   1.148 +     */
   1.149 +    public String getEncoding() {
   1.150 +        return "UTF-8";
   1.151 +    }
   1.152 +
   1.153 +    /**
   1.154 +     * Reads a single character.
   1.155 +     *
   1.156 +     * @return The character read, or -1 if the end of the stream has been
   1.157 +     *         reached
   1.158 +     *
   1.159 +     * @exception  IOException  If an I/O error occurs
   1.160 +     */
   1.161 +    public int read() throws IOException {
   1.162 +        final InputStream is = (InputStream)lock;
   1.163 +        int c = is.read();
   1.164 +        if (c == -1) {
   1.165 +            return -1;
   1.166 +        }
   1.167 +        c = (int) c & 0xff;
   1.168 +        switch (c >> 4) {
   1.169 +            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
   1.170 +                /* 0xxxxxxx*/
   1.171 +                return c;
   1.172 +            case 12: case 13: {
   1.173 +                /* 110x xxxx   10xx xxxx*/
   1.174 +                int char2 = (int) is.read();
   1.175 +                if ((char2 & 0xC0) != 0x80)
   1.176 +                    throw new UTFDataFormatException("malformed input");
   1.177 +                return (((c & 0x1F) << 6) | (char2 & 0x3F));
   1.178 +            }
   1.179 +            case 14: {
   1.180 +                /* 1110 xxxx  10xx xxxx  10xx xxxx */
   1.181 +                int char2 = is.read();
   1.182 +                int char3 = is.read();
   1.183 +                if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
   1.184 +                    throw new UTFDataFormatException("malformed input");
   1.185 +                return (((c    & 0x0F) << 12) |
   1.186 +                       ((char2 & 0x3F) << 6)  |
   1.187 +                       ((char3 & 0x3F) << 0));
   1.188 +            }
   1.189 +            default:
   1.190 +                /* 10xx xxxx,  1111 xxxx */
   1.191 +                throw new UTFDataFormatException("malformed input");
   1.192 +        }
   1.193 +    }
   1.194 +    
   1.195 +    /**
   1.196 +     * Reads characters into a portion of an array.
   1.197 +     *
   1.198 +     * @param      cbuf     Destination buffer
   1.199 +     * @param      offset   Offset at which to start storing characters
   1.200 +     * @param      length   Maximum number of characters to read
   1.201 +     *
   1.202 +     * @return     The number of characters read, or -1 if the end of the
   1.203 +     *             stream has been reached
   1.204 +     *
   1.205 +     * @exception  IOException  If an I/O error occurs
   1.206 +     */
   1.207 +    public int read(char cbuf[], int offset, int length) throws IOException {
   1.208 +        for (int i = 0; i < length; i++) {
   1.209 +            int ch = read();
   1.210 +            if (ch == -1) {
   1.211 +                if (i == 0) return -1;
   1.212 +                return i;
   1.213 +            }
   1.214 +            cbuf[offset++] = (char) ch;
   1.215 +        }
   1.216 +        return length;
   1.217 +    }
   1.218 +
   1.219 +    /**
   1.220 +     * Tells whether this stream is ready to be read.  An InputStreamReader is
   1.221 +     * ready if its input buffer is not empty, or if bytes are available to be
   1.222 +     * read from the underlying byte stream.
   1.223 +     *
   1.224 +     * @exception  IOException  If an I/O error occurs
   1.225 +     */
   1.226 +    public boolean ready() throws IOException {
   1.227 +        return ((InputStream)lock).available() > 0;
   1.228 +    }
   1.229 +
   1.230 +    public void close() throws IOException {
   1.231 +        ((InputStream)lock).close();
   1.232 +    }
   1.233 +}