Package java.io
Class InputStreamReader
java.lang.Object
java.io.Reader
java.io.InputStreamReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
- Direct Known Subclasses:
FileReader
public class InputStreamReader extends Reader
A class for turning a byte stream into a character stream. Data read from the
source input stream is converted into characters by either a default or a
provided character converter. The default encoding is taken from the
"file.encoding" system property.
InputStreamReader contains a buffer
of bytes read from the source stream and converts these into characters as
needed. The buffer size is 8K.- See Also:
OutputStreamWriter
-
Field Summary
-
Constructor Summary
Constructors Constructor Description InputStreamReader(InputStream in)InputStreamReader(InputStream in, String charsetName)Constructs a new InputStreamReader on the InputStreamin.InputStreamReader(InputStream in, Charset charset)Constructs a new InputStreamReader on the InputStreaminand Charsetcharset.InputStreamReader(InputStream in, CharsetDecoder dec)Constructs a new InputStreamReader on the InputStreaminand CharsetDecoderdec. -
Method Summary
Modifier and Type Method Description voidclose()Closes this reader.StringgetEncoding()Returns the canonical name of the encoding used by this writer to convert characters to bytes, or null if this writer has been closed.intread()Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0.intread(char[] buffer, int offset, int count)Reads up tocountcharacters from this reader and stores them at positionoffsetin the character arraybuffer.booleanready()Indicates whether this reader is ready to be read without blocking.
-
Constructor Details
-
InputStreamReader
Constructs a newInputStreamReaderon theInputStreamin. This constructor sets the character converter to the encoding specified in the "file.encoding" property and falls back to ISO 8859_1 (ISO-Latin-1) if the property doesn't exist.- Parameters:
in- the input stream from which to read characters.
-
InputStreamReader
Constructs a new InputStreamReader on the InputStreamin. The character converter that is used to decode bytes into characters is identified by name bycharsetName. If the encoding cannot be found, an UnsupportedEncodingException error is thrown.- Parameters:
in- the InputStream from which to read characters.charsetName- identifies the character converter to use.- Throws:
NullPointerException- ifcharsetNameisnull.UnsupportedEncodingException- if the encoding specified bycharsetNamecannot be found.
-
InputStreamReader
Constructs a new InputStreamReader on the InputStreaminand CharsetDecoderdec.- Parameters:
in- the source InputStream from which to read characters.dec- the CharsetDecoder used by the character conversion.
-
InputStreamReader
Constructs a new InputStreamReader on the InputStreaminand Charsetcharset.- Parameters:
in- the source InputStream from which to read characters.charset- the Charset that defines the character converter
-
-
Method Details
-
close
Closes this reader. This implementation closes the source InputStream and releases all local storage.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein classReader- Throws:
IOException- if an error occurs attempting to close this reader.
-
getEncoding
Returns the canonical name of the encoding used by this writer to convert characters to bytes, or null if this writer has been closed. Most callers should probably keep track of the String or Charset they passed in; this method may not return the same name. -
read
Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0. Returns -1 if the end of the reader has been reached. The byte value is either obtained from converting bytes in this reader's buffer or by first filling the buffer from the source InputStream and then reading from the buffer.- Overrides:
readin classReader- Returns:
- the character read or -1 if the end of the reader has been reached.
- Throws:
IOException- if this reader is closed or some other I/O error occurs.
-
read
Reads up tocountcharacters from this reader and stores them at positionoffsetin the character arraybuffer. Returns the number of characters actually read or -1 if the end of the reader has been reached. The bytes are either obtained from converting bytes in this reader's buffer or by first filling the buffer from the source InputStream and then reading from the buffer.- Specified by:
readin classReader- Throws:
IndexOutOfBoundsException- ifoffset < 0 || count < 0 || offset + count > buffer.length.IOException- if this reader is closed or some other I/O error occurs.
-
ready
Indicates whether this reader is ready to be read without blocking. If the result istrue, the nextread()will not block. If the result isfalsethen this reader may or may not block whenread()is called. This implementation returnstrueif there are bytes available in the buffer or the source stream has bytes available.- Overrides:
readyin classReader- Returns:
trueif the receiver will not block whenread()is called,falseif unknown or blocking will occur.- Throws:
IOException- if this reader is closed or some other I/O error occurs.- See Also:
Reader.read(),Reader.read(char[]),Reader.read(char[], int, int)
-