emul/compact/src/main/java/java/io/InputStreamReader.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 23 Jan 2013 22:55:28 +0100
branchemul
changeset 560 53fafe384803
parent 557 5be31d9fa455
child 561 f06b2d18eb52
permissions -rw-r--r--
Moving all arraycopy methods into one System class
jaroslav@557
     1
/*
jaroslav@557
     2
 * Copyright (c) 1996, 2005, Oracle and/or its affiliates. All rights reserved.
jaroslav@557
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@557
     4
 *
jaroslav@557
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@557
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@557
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@557
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@557
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@557
    10
 *
jaroslav@557
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@557
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@557
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@557
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@557
    15
 * accompanied this code).
jaroslav@557
    16
 *
jaroslav@557
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@557
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@557
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@557
    20
 *
jaroslav@557
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@557
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@557
    23
 * questions.
jaroslav@557
    24
 */
jaroslav@557
    25
jaroslav@557
    26
package java.io;
jaroslav@557
    27
jaroslav@557
    28
import java.nio.charset.Charset;
jaroslav@557
    29
import java.nio.charset.CharsetDecoder;
jaroslav@557
    30
import sun.nio.cs.StreamDecoder;
jaroslav@557
    31
jaroslav@557
    32
jaroslav@557
    33
/**
jaroslav@557
    34
 * An InputStreamReader is a bridge from byte streams to character streams: It
jaroslav@557
    35
 * reads bytes and decodes them into characters using a specified {@link
jaroslav@557
    36
 * java.nio.charset.Charset <code>charset</code>}.  The charset that it uses
jaroslav@557
    37
 * may be specified by name or may be given explicitly, or the platform's
jaroslav@557
    38
 * default charset may be accepted.
jaroslav@557
    39
 *
jaroslav@557
    40
 * <p> Each invocation of one of an InputStreamReader's read() methods may
jaroslav@557
    41
 * cause one or more bytes to be read from the underlying byte-input stream.
jaroslav@557
    42
 * To enable the efficient conversion of bytes to characters, more bytes may
jaroslav@557
    43
 * be read ahead from the underlying stream than are necessary to satisfy the
jaroslav@557
    44
 * current read operation.
jaroslav@557
    45
 *
jaroslav@557
    46
 * <p> For top efficiency, consider wrapping an InputStreamReader within a
jaroslav@557
    47
 * BufferedReader.  For example:
jaroslav@557
    48
 *
jaroslav@557
    49
 * <pre>
jaroslav@557
    50
 * BufferedReader in
jaroslav@557
    51
 *   = new BufferedReader(new InputStreamReader(System.in));
jaroslav@557
    52
 * </pre>
jaroslav@557
    53
 *
jaroslav@557
    54
 * @see BufferedReader
jaroslav@557
    55
 * @see InputStream
jaroslav@557
    56
 * @see java.nio.charset.Charset
jaroslav@557
    57
 *
jaroslav@557
    58
 * @author      Mark Reinhold
jaroslav@557
    59
 * @since       JDK1.1
jaroslav@557
    60
 */
jaroslav@557
    61
jaroslav@557
    62
public class InputStreamReader extends Reader {
jaroslav@557
    63
jaroslav@557
    64
    private final StreamDecoder sd;
jaroslav@557
    65
jaroslav@557
    66
    /**
jaroslav@557
    67
     * Creates an InputStreamReader that uses the default charset.
jaroslav@557
    68
     *
jaroslav@557
    69
     * @param  in   An InputStream
jaroslav@557
    70
     */
jaroslav@557
    71
    public InputStreamReader(InputStream in) {
jaroslav@557
    72
        super(in);
jaroslav@557
    73
        try {
jaroslav@557
    74
            sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
jaroslav@557
    75
        } catch (UnsupportedEncodingException e) {
jaroslav@557
    76
            // The default encoding should always be available
jaroslav@557
    77
            throw new Error(e);
jaroslav@557
    78
        }
jaroslav@557
    79
    }
jaroslav@557
    80
jaroslav@557
    81
    /**
jaroslav@557
    82
     * Creates an InputStreamReader that uses the named charset.
jaroslav@557
    83
     *
jaroslav@557
    84
     * @param  in
jaroslav@557
    85
     *         An InputStream
jaroslav@557
    86
     *
jaroslav@557
    87
     * @param  charsetName
jaroslav@557
    88
     *         The name of a supported
jaroslav@557
    89
     *         {@link java.nio.charset.Charset </code>charset<code>}
jaroslav@557
    90
     *
jaroslav@557
    91
     * @exception  UnsupportedEncodingException
jaroslav@557
    92
     *             If the named charset is not supported
jaroslav@557
    93
     */
jaroslav@557
    94
    public InputStreamReader(InputStream in, String charsetName)
jaroslav@557
    95
        throws UnsupportedEncodingException
jaroslav@557
    96
    {
jaroslav@557
    97
        super(in);
jaroslav@557
    98
        if (charsetName == null)
jaroslav@557
    99
            throw new NullPointerException("charsetName");
jaroslav@557
   100
        sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
jaroslav@557
   101
    }
jaroslav@557
   102
jaroslav@557
   103
    /**
jaroslav@557
   104
     * Creates an InputStreamReader that uses the given charset. </p>
jaroslav@557
   105
     *
jaroslav@557
   106
     * @param  in       An InputStream
jaroslav@557
   107
     * @param  cs       A charset
jaroslav@557
   108
     *
jaroslav@557
   109
     * @since 1.4
jaroslav@557
   110
     * @spec JSR-51
jaroslav@557
   111
     */
jaroslav@560
   112
/* XXX:
jaroslav@557
   113
    public InputStreamReader(InputStream in, Charset cs) {
jaroslav@557
   114
        super(in);
jaroslav@557
   115
        if (cs == null)
jaroslav@557
   116
            throw new NullPointerException("charset");
jaroslav@557
   117
        sd = StreamDecoder.forInputStreamReader(in, this, cs);
jaroslav@557
   118
    }
jaroslav@560
   119
*/
jaroslav@557
   120
    /**
jaroslav@557
   121
     * Creates an InputStreamReader that uses the given charset decoder.  </p>
jaroslav@557
   122
     *
jaroslav@557
   123
     * @param  in       An InputStream
jaroslav@557
   124
     * @param  dec      A charset decoder
jaroslav@557
   125
     *
jaroslav@557
   126
     * @since 1.4
jaroslav@557
   127
     * @spec JSR-51
jaroslav@557
   128
     */
jaroslav@560
   129
/* XXX:
jaroslav@557
   130
    public InputStreamReader(InputStream in, CharsetDecoder dec) {
jaroslav@557
   131
        super(in);
jaroslav@557
   132
        if (dec == null)
jaroslav@557
   133
            throw new NullPointerException("charset decoder");
jaroslav@557
   134
        sd = StreamDecoder.forInputStreamReader(in, this, dec);
jaroslav@557
   135
    }
jaroslav@560
   136
*/
jaroslav@560
   137
    
jaroslav@557
   138
    /**
jaroslav@557
   139
     * Returns the name of the character encoding being used by this stream.
jaroslav@557
   140
     *
jaroslav@557
   141
     * <p> If the encoding has an historical name then that name is returned;
jaroslav@557
   142
     * otherwise the encoding's canonical name is returned.
jaroslav@557
   143
     *
jaroslav@557
   144
     * <p> If this instance was created with the {@link
jaroslav@557
   145
     * #InputStreamReader(InputStream, String)} constructor then the returned
jaroslav@557
   146
     * name, being unique for the encoding, may differ from the name passed to
jaroslav@557
   147
     * the constructor. This method will return <code>null</code> if the
jaroslav@557
   148
     * stream has been closed.
jaroslav@557
   149
     * </p>
jaroslav@557
   150
     * @return The historical name of this encoding, or
jaroslav@557
   151
     *         <code>null</code> if the stream has been closed
jaroslav@557
   152
     *
jaroslav@557
   153
     * @see java.nio.charset.Charset
jaroslav@557
   154
     *
jaroslav@557
   155
     * @revised 1.4
jaroslav@557
   156
     * @spec JSR-51
jaroslav@557
   157
     */
jaroslav@557
   158
    public String getEncoding() {
jaroslav@557
   159
        return sd.getEncoding();
jaroslav@557
   160
    }
jaroslav@557
   161
jaroslav@557
   162
    /**
jaroslav@557
   163
     * Reads a single character.
jaroslav@557
   164
     *
jaroslav@557
   165
     * @return The character read, or -1 if the end of the stream has been
jaroslav@557
   166
     *         reached
jaroslav@557
   167
     *
jaroslav@557
   168
     * @exception  IOException  If an I/O error occurs
jaroslav@557
   169
     */
jaroslav@557
   170
    public int read() throws IOException {
jaroslav@557
   171
        return sd.read();
jaroslav@557
   172
    }
jaroslav@557
   173
jaroslav@557
   174
    /**
jaroslav@557
   175
     * Reads characters into a portion of an array.
jaroslav@557
   176
     *
jaroslav@557
   177
     * @param      cbuf     Destination buffer
jaroslav@557
   178
     * @param      offset   Offset at which to start storing characters
jaroslav@557
   179
     * @param      length   Maximum number of characters to read
jaroslav@557
   180
     *
jaroslav@557
   181
     * @return     The number of characters read, or -1 if the end of the
jaroslav@557
   182
     *             stream has been reached
jaroslav@557
   183
     *
jaroslav@557
   184
     * @exception  IOException  If an I/O error occurs
jaroslav@557
   185
     */
jaroslav@557
   186
    public int read(char cbuf[], int offset, int length) throws IOException {
jaroslav@557
   187
        return sd.read(cbuf, offset, length);
jaroslav@557
   188
    }
jaroslav@557
   189
jaroslav@557
   190
    /**
jaroslav@557
   191
     * Tells whether this stream is ready to be read.  An InputStreamReader is
jaroslav@557
   192
     * ready if its input buffer is not empty, or if bytes are available to be
jaroslav@557
   193
     * read from the underlying byte stream.
jaroslav@557
   194
     *
jaroslav@557
   195
     * @exception  IOException  If an I/O error occurs
jaroslav@557
   196
     */
jaroslav@557
   197
    public boolean ready() throws IOException {
jaroslav@557
   198
        return sd.ready();
jaroslav@557
   199
    }
jaroslav@557
   200
jaroslav@557
   201
    public void close() throws IOException {
jaroslav@557
   202
        sd.close();
jaroslav@557
   203
    }
jaroslav@557
   204
}