< prev index next >

src/java.base/share/classes/java/io/InputStreamReader.java

Print this page

 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 import java.nio.CharBuffer;
 29 import java.nio.charset.Charset;
 30 import java.nio.charset.CharsetDecoder;

 31 import sun.nio.cs.StreamDecoder;
 32 
 33 
 34 /**
 35  * An InputStreamReader is a bridge from byte streams to character streams: It
 36  * reads bytes and decodes them into characters using a specified {@link
 37  * Charset charset}.  The charset that it uses
 38  * may be specified by name or may be given explicitly, or the
 39  * {@link Charset#defaultCharset() default charset} may be used.
 40  *
 41  * <p> Each invocation of one of an InputStreamReader's read() methods may
 42  * cause one or more bytes to be read from the underlying byte-input stream.
 43  * To enable the efficient conversion of bytes to characters, more bytes may
 44  * be read ahead from the underlying stream than are necessary to satisfy the
 45  * current read operation.
 46  *
 47  * <p> For top efficiency, consider wrapping an InputStreamReader within a
 48  * BufferedReader.  For example:
 49  *
 50  * <pre>
 51  * BufferedReader in
 52  *   = new BufferedReader(new InputStreamReader(anInputStream));
 53  * </pre>
 54  *
 55  * @see BufferedReader
 56  * @see InputStream
 57  * @see Charset
 58  *
 59  * @author      Mark Reinhold
 60  * @since       1.1
 61  */
 62 
 63 public class InputStreamReader extends Reader {
 64 
 65     private final StreamDecoder sd;
 66 
 67     /**
 68      * Creates an InputStreamReader that uses the
 69      * {@link Charset#defaultCharset() default charset}.
 70      *
 71      * @param  in   An InputStream
 72      *
 73      * @see Charset#defaultCharset()
 74      */
 75     public InputStreamReader(InputStream in) {
 76         super(in);
 77         sd = StreamDecoder.forInputStreamReader(in, this,
 78                 Charset.defaultCharset()); // ## check lock object
 79     }
 80 
 81     /**
 82      * Creates an InputStreamReader that uses the named charset.
 83      *
 84      * @param  in
 85      *         An InputStream
 86      *
 87      * @param  charsetName
 88      *         The name of a supported {@link Charset charset}
 89      *
 90      * @throws     UnsupportedEncodingException
 91      *             If the named charset is not supported
 92      */
 93     public InputStreamReader(InputStream in, String charsetName)
 94         throws UnsupportedEncodingException
 95     {
 96         super(in);
 97         if (charsetName == null)
 98             throw new NullPointerException("charsetName");
 99         sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
100     }
101 
102     /**
103      * Creates an InputStreamReader that uses the given charset.
104      *
105      * @param  in       An InputStream
106      * @param  cs       A charset
107      *
108      * @since 1.4
109      */
110     public InputStreamReader(InputStream in, Charset cs) {
111         super(in);
112         if (cs == null)
113             throw new NullPointerException("charset");
114         sd = StreamDecoder.forInputStreamReader(in, this, cs);
115     }
116 
117     /**
118      * Creates an InputStreamReader that uses the given charset decoder.
119      *
120      * @param  in       An InputStream
121      * @param  dec      A charset decoder
122      *
123      * @since 1.4
124      */
125     public InputStreamReader(InputStream in, CharsetDecoder dec) {
126         super(in);
127         if (dec == null)
128             throw new NullPointerException("charset decoder");
129         sd = StreamDecoder.forInputStreamReader(in, this, dec);
130     }
131 
132     /**
133      * Returns the name of the character encoding being used by this stream.
134      *
135      * <p> If the encoding has an historical name then that name is returned;
136      * otherwise the encoding's canonical name is returned.
137      *
138      * <p> If this instance was created with the {@link
139      * #InputStreamReader(InputStream, String)} constructor then the returned
140      * name, being unique for the encoding, may differ from the name passed to
141      * the constructor. This method will return {@code null} if the
142      * stream has been closed.
143      * </p>
144      * @return The historical name of this encoding, or
145      *         {@code null} if the stream has been closed
146      *
147      * @see Charset
148      *
149      * @revised 1.4

 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 import java.nio.CharBuffer;
 29 import java.nio.charset.Charset;
 30 import java.nio.charset.CharsetDecoder;
 31 import java.util.Objects;
 32 import sun.nio.cs.StreamDecoder;
 33 

 34 /**
 35  * An InputStreamReader is a bridge from byte streams to character streams: It
 36  * reads bytes and decodes them into characters using a specified {@link
 37  * Charset charset}.  The charset that it uses
 38  * may be specified by name or may be given explicitly, or the
 39  * {@link Charset#defaultCharset() default charset} may be used.
 40  *
 41  * <p> Each invocation of one of an InputStreamReader's read() methods may
 42  * cause one or more bytes to be read from the underlying byte-input stream.
 43  * To enable the efficient conversion of bytes to characters, more bytes may
 44  * be read ahead from the underlying stream than are necessary to satisfy the
 45  * current read operation.
 46  *
 47  * <p> For top efficiency, consider wrapping an InputStreamReader within a
 48  * BufferedReader.  For example:
 49  *
 50  * <pre>
 51  * BufferedReader in
 52  *   = new BufferedReader(new InputStreamReader(anInputStream));
 53  * </pre>
 54  *
 55  * @see BufferedReader
 56  * @see InputStream
 57  * @see Charset
 58  *
 59  * @author      Mark Reinhold
 60  * @since       1.1
 61  */
 62 
 63 public class InputStreamReader extends Reader {

 64     private final StreamDecoder sd;
 65 
 66     /**
 67      * Creates an InputStreamReader that uses the
 68      * {@link Charset#defaultCharset() default charset}.
 69      *
 70      * @param  in   An InputStream
 71      *
 72      * @see Charset#defaultCharset()
 73      */
 74     public InputStreamReader(InputStream in) {
 75         Objects.requireNonNull(in);
 76         sd = StreamDecoder.forInputStreamReader(in, lock, Charset.defaultCharset());

 77     }
 78 
 79     /**
 80      * Creates an InputStreamReader that uses the named charset.
 81      *
 82      * @param  in
 83      *         An InputStream
 84      *
 85      * @param  charsetName
 86      *         The name of a supported {@link Charset charset}
 87      *
 88      * @throws     UnsupportedEncodingException
 89      *             If the named charset is not supported
 90      */
 91     public InputStreamReader(InputStream in, String charsetName)
 92         throws UnsupportedEncodingException
 93     {
 94         Objects.requireNonNull(in);
 95         Objects.requireNonNull(charsetName, "charsetName");
 96         sd = StreamDecoder.forInputStreamReader(in, lock, charsetName);

 97     }
 98 
 99     /**
100      * Creates an InputStreamReader that uses the given charset.
101      *
102      * @param  in       An InputStream
103      * @param  cs       A charset
104      *
105      * @since 1.4
106      */
107     public InputStreamReader(InputStream in, Charset cs) {
108         Objects.requireNonNull(in);
109         Objects.requireNonNull(cs, "charset");
110         sd = StreamDecoder.forInputStreamReader(in, lock, cs);

111     }
112 
113     /**
114      * Creates an InputStreamReader that uses the given charset decoder.
115      *
116      * @param  in       An InputStream
117      * @param  dec      A charset decoder
118      *
119      * @since 1.4
120      */
121     public InputStreamReader(InputStream in, CharsetDecoder dec) {
122         Objects.requireNonNull(dec, "charset decoder");
123         sd = StreamDecoder.forInputStreamReader(in, lock, dec);


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} if the
136      * stream has been closed.
137      * </p>
138      * @return The historical name of this encoding, or
139      *         {@code null} if the stream has been closed
140      *
141      * @see Charset
142      *
143      * @revised 1.4
< prev index next >