|
Chapter 11 The java.io Package |
|
InputStreamReader
Name
InputStreamReader
- Class Name:
-
java.io.InputStreamReader
- Superclass:
-
java.io.Reader
- Immediate Subclasses:
-
java.io.FileReader
- Interfaces Implemented:
-
None
- Availability:
-
New as of JDK 1.1
The InputStreamReader class
is a bridge between the byte-oriented world of the InputStream
class and the character-oriented world of the Reader
class. The InputStreamReader
represents a character stream, but it gets its input from an underlying
byte stream. An encoding scheme is responsible for translating the bytes
to Unicode characters. An InputStreamReader
can be created using an explicit encoding scheme or a default encoding
scheme.
For example, to read an ISO-8859-5 byte stream as a Unicode character stream,
you can construct an InputStreamReader
with the encoding "8859_5" as follows:
InputStreamReader inr = new InputStreamReader(in, "8859_5");
Each time you read from an InputStreamReader
object, bytes may be read from the underlying byte stream. To improve efficiency,
you may want to wrap the InputStreamReader
in a BufferedReader.
public class java.io.InputStreamReader extends java.io.Reader {
// Constructors
public InputStreamReader(InputStream in);
public InputStreamReader(InputStream in, String enc);
// Instance Methods
public void close();
public String getEncoding();
public int read();
public int read(char[] cbuf, int off, int len);
public boolean ready();
}
- Parameters
-
- in
-
The input stream to
use.
- Description
-
This constructor creates an InputStreamReader
that gets its data from in
and translates bytes to characters using the system's default encoding
scheme.
- Parameters
-
- in
-
The input stream to use.
- enc
-
The name of an encoding scheme.
- Throws
-
- UnsupportedEncodingException
-
If enc is not a supported encoding scheme.
- Description
-
This constructor creates an InputStreamReader
that gets its data from in
and translates bytes to characters using the given encoding scheme.
- Throws
-
- IOException
-
If any kind
of I/O error occurs.
- Overrides
-
Reader.close()
- Description
-
This method calls the close()
method of the underlying input stream, which releases any system resources
associated with this object.
- Returns
-
A String that contains the
name of the character encoding scheme of this reader.
- Description
-
This method returns the name of the character encoding scheme this InputStreamReader
is currently using.
- Returns
-
The next character of data or -1 if the end of the stream is encountered.
- Throws
-
- IOException
-
If any kind
of I/O error occurs.
- Overrides
-
Reader.read()
- Description
-
This method reads a character of input. The method returns the next
character that has been read and converted from the underlying
byte-oriented InputStream. The
InputStreamReader class uses buffering internally,
so this method returns immediately unless the buffer is empty. If the
buffer is empty, a new block of bytes is read from the
InputStream and converted to characters. The method
blocks until the character is read, the end of stream is encountered,
or an exception is thrown.
- Parameters
-
- cbuf
-
An array of characters to be filled from the stream.
- off
-
An offset into the array.
- len
-
The number of characters to read.
- Returns
-
The actual number of characters read or -1 if the end of the stream is
encountered immediately.
- Throws
-
- IOException
-
If any kind
of I/O error occurs.
- Overrides
-
Reader.read(char[], int, int)
- Description
-
This method reads up to len
characters of input into the given array starting at index off.
The InputStreamReader class
uses buffering internally, so this method returns immediately if there
is enough data in the buffer. If there is not enough data, a new block
of bytes is read from the InputStream
and converted to characters. The method blocks until some data is available.
- Returns
-
true if the reader is ready
to be read; false otherwise.
- Throws
-
- IOException
-
If the reader
is closed or any other kind of I/O error occurs.
- Overrides
-
Reader.ready()
- Description
-
This method returns a boolean
value that indicates whether or not the reader is ready to be read. If
there is data available in the internal buffer or if there are bytes available
to be read from the underlying byte stream, the method returns true.
Otherwise it returns false.
BufferedReader,
FileReader,
InputStream,
IOException,
, Reader,
UnsupportedEncodingException
|
|