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