emul/compact/src/main/java/java/io/InputStreamReader.java
branchjdk7-b147
changeset 557 5be31d9fa455
child 560 53fafe384803
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/compact/src/main/java/java/io/InputStreamReader.java	Wed Jan 23 22:32:27 2013 +0100
     1.3 @@ -0,0 +1,201 @@
     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 +import java.nio.charset.Charset;
    1.32 +import java.nio.charset.CharsetDecoder;
    1.33 +import sun.nio.cs.StreamDecoder;
    1.34 +
    1.35 +
    1.36 +/**
    1.37 + * An InputStreamReader is a bridge from byte streams to character streams: It
    1.38 + * reads bytes and decodes them into characters using a specified {@link
    1.39 + * java.nio.charset.Charset <code>charset</code>}.  The charset that it uses
    1.40 + * may be specified by name or may be given explicitly, or the platform's
    1.41 + * default charset may be accepted.
    1.42 + *
    1.43 + * <p> Each invocation of one of an InputStreamReader's read() methods may
    1.44 + * cause one or more bytes to be read from the underlying byte-input stream.
    1.45 + * To enable the efficient conversion of bytes to characters, more bytes may
    1.46 + * be read ahead from the underlying stream than are necessary to satisfy the
    1.47 + * current read operation.
    1.48 + *
    1.49 + * <p> For top efficiency, consider wrapping an InputStreamReader within a
    1.50 + * BufferedReader.  For example:
    1.51 + *
    1.52 + * <pre>
    1.53 + * BufferedReader in
    1.54 + *   = new BufferedReader(new InputStreamReader(System.in));
    1.55 + * </pre>
    1.56 + *
    1.57 + * @see BufferedReader
    1.58 + * @see InputStream
    1.59 + * @see java.nio.charset.Charset
    1.60 + *
    1.61 + * @author      Mark Reinhold
    1.62 + * @since       JDK1.1
    1.63 + */
    1.64 +
    1.65 +public class InputStreamReader extends Reader {
    1.66 +
    1.67 +    private final StreamDecoder sd;
    1.68 +
    1.69 +    /**
    1.70 +     * Creates an InputStreamReader that uses the default charset.
    1.71 +     *
    1.72 +     * @param  in   An InputStream
    1.73 +     */
    1.74 +    public InputStreamReader(InputStream in) {
    1.75 +        super(in);
    1.76 +        try {
    1.77 +            sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
    1.78 +        } catch (UnsupportedEncodingException e) {
    1.79 +            // The default encoding should always be available
    1.80 +            throw new Error(e);
    1.81 +        }
    1.82 +    }
    1.83 +
    1.84 +    /**
    1.85 +     * Creates an InputStreamReader that uses the named charset.
    1.86 +     *
    1.87 +     * @param  in
    1.88 +     *         An InputStream
    1.89 +     *
    1.90 +     * @param  charsetName
    1.91 +     *         The name of a supported
    1.92 +     *         {@link java.nio.charset.Charset </code>charset<code>}
    1.93 +     *
    1.94 +     * @exception  UnsupportedEncodingException
    1.95 +     *             If the named charset is not supported
    1.96 +     */
    1.97 +    public InputStreamReader(InputStream in, String charsetName)
    1.98 +        throws UnsupportedEncodingException
    1.99 +    {
   1.100 +        super(in);
   1.101 +        if (charsetName == null)
   1.102 +            throw new NullPointerException("charsetName");
   1.103 +        sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
   1.104 +    }
   1.105 +
   1.106 +    /**
   1.107 +     * Creates an InputStreamReader that uses the given charset. </p>
   1.108 +     *
   1.109 +     * @param  in       An InputStream
   1.110 +     * @param  cs       A charset
   1.111 +     *
   1.112 +     * @since 1.4
   1.113 +     * @spec JSR-51
   1.114 +     */
   1.115 +    public InputStreamReader(InputStream in, Charset cs) {
   1.116 +        super(in);
   1.117 +        if (cs == null)
   1.118 +            throw new NullPointerException("charset");
   1.119 +        sd = StreamDecoder.forInputStreamReader(in, this, cs);
   1.120 +    }
   1.121 +
   1.122 +    /**
   1.123 +     * Creates an InputStreamReader that uses the given charset decoder.  </p>
   1.124 +     *
   1.125 +     * @param  in       An InputStream
   1.126 +     * @param  dec      A charset decoder
   1.127 +     *
   1.128 +     * @since 1.4
   1.129 +     * @spec JSR-51
   1.130 +     */
   1.131 +    public InputStreamReader(InputStream in, CharsetDecoder dec) {
   1.132 +        super(in);
   1.133 +        if (dec == null)
   1.134 +            throw new NullPointerException("charset decoder");
   1.135 +        sd = StreamDecoder.forInputStreamReader(in, this, dec);
   1.136 +    }
   1.137 +
   1.138 +    /**
   1.139 +     * Returns the name of the character encoding being used by this stream.
   1.140 +     *
   1.141 +     * <p> If the encoding has an historical name then that name is returned;
   1.142 +     * otherwise the encoding's canonical name is returned.
   1.143 +     *
   1.144 +     * <p> If this instance was created with the {@link
   1.145 +     * #InputStreamReader(InputStream, String)} constructor then the returned
   1.146 +     * name, being unique for the encoding, may differ from the name passed to
   1.147 +     * the constructor. This method will return <code>null</code> if the
   1.148 +     * stream has been closed.
   1.149 +     * </p>
   1.150 +     * @return The historical name of this encoding, or
   1.151 +     *         <code>null</code> if the stream has been closed
   1.152 +     *
   1.153 +     * @see java.nio.charset.Charset
   1.154 +     *
   1.155 +     * @revised 1.4
   1.156 +     * @spec JSR-51
   1.157 +     */
   1.158 +    public String getEncoding() {
   1.159 +        return sd.getEncoding();
   1.160 +    }
   1.161 +
   1.162 +    /**
   1.163 +     * Reads a single character.
   1.164 +     *
   1.165 +     * @return The character read, or -1 if the end of the stream has been
   1.166 +     *         reached
   1.167 +     *
   1.168 +     * @exception  IOException  If an I/O error occurs
   1.169 +     */
   1.170 +    public int read() throws IOException {
   1.171 +        return sd.read();
   1.172 +    }
   1.173 +
   1.174 +    /**
   1.175 +     * Reads characters into a portion of an array.
   1.176 +     *
   1.177 +     * @param      cbuf     Destination buffer
   1.178 +     * @param      offset   Offset at which to start storing characters
   1.179 +     * @param      length   Maximum number of characters to read
   1.180 +     *
   1.181 +     * @return     The number of characters read, or -1 if the end of the
   1.182 +     *             stream has been reached
   1.183 +     *
   1.184 +     * @exception  IOException  If an I/O error occurs
   1.185 +     */
   1.186 +    public int read(char cbuf[], int offset, int length) throws IOException {
   1.187 +        return sd.read(cbuf, offset, length);
   1.188 +    }
   1.189 +
   1.190 +    /**
   1.191 +     * Tells whether this stream is ready to be read.  An InputStreamReader is
   1.192 +     * ready if its input buffer is not empty, or if bytes are available to be
   1.193 +     * read from the underlying byte stream.
   1.194 +     *
   1.195 +     * @exception  IOException  If an I/O error occurs
   1.196 +     */
   1.197 +    public boolean ready() throws IOException {
   1.198 +        return sd.ready();
   1.199 +    }
   1.200 +
   1.201 +    public void close() throws IOException {
   1.202 +        sd.close();
   1.203 +    }
   1.204 +}