All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class java.io.InputStreamReader

java.lang.Object
   |
   +----java.io.Reader
           |
           +----java.io.InputStreamReader

public class InputStreamReader
extends Reader
Read from a byte stream, translating bytes into characters according to a specified character encoding. Each InputStreamReader incorporates its own ByteToCharConverter, and is thus a bridge from byte streams to character streams.

The encoding used by an InputStreamReader may be specified by name, by providing a ByteToCharConverter, or by accepting the default encoding, which is defined by the system property file.encoding.

An InputStreamReader reads bytes from the underlying byte-input stream into an internal buffer. The size of this buffer may be specified, but by default it is large enough for most purposes. Each invocation of a read() method causes the encoding converter to be invoked on the bytes remaining in the buffer. For top efficiency, consider wrapping an InputStreamReader within a BufferedReader so as to avoid frequent converter invocations. For example,

 BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));
 

See Also:
BufferedReader, InputStream

Constructor Index

 o InputStreamReader(InputStream)
Create an InputStreamReader that uses the default character encoding and the default input-buffer size.
 o InputStreamReader(InputStream, ByteToCharConverter)
Create an InputStreamReader that uses the specified byte-to-character converter and the default input-buffer size.
 o InputStreamReader(InputStream, ByteToCharConverter, int)
Create an InputStreamReader that uses the specified byte-to-character converter and the specified input-buffer size.
 o InputStreamReader(InputStream, int)
Create an InputStreamReader that uses the default character encoding and the specified input-buffer size.
 o InputStreamReader(InputStream, String)
Create an InputStreamReader that uses the named character encoding and the default input-buffer size.
 o InputStreamReader(InputStream, String, int)
Create an InputStreamReader that uses the named character encoding and the specified input-buffer size.

Method Index

 o close()
Close the stream.
 o getCharacterEncoding()
Return the name of the encoding being used by this stream.
 o read()
Read a single character.
 o read(char[], int, int)
Read characters into a portion of an array.
 o ready()
Tell whether this stream is ready to be read.

Constructors

 o InputStreamReader
  public InputStreamReader(InputStream in)
Create an InputStreamReader that uses the default character encoding and the default input-buffer size.

Parameters:
in - An InputStream
 o InputStreamReader
  public InputStreamReader(InputStream in,
                           String enc) throws UnsupportedEncodingException
Create an InputStreamReader that uses the named character encoding and the default input-buffer size.

Parameters:
in - An InputStream
enc - Name of encoding to be used
Throws: UnsupportedEncodingException
If no converter can be found for the specified encoding
 o InputStreamReader
  public InputStreamReader(InputStream in,
                           int sz)
Create an InputStreamReader that uses the default character encoding and the specified input-buffer size.

Parameters:
in - An InputStream
sz - Input-buffer size
Throws: IllegalArgumentException
If sz is <= 0
 o InputStreamReader
  public InputStreamReader(InputStream in,
                           String enc,
                           int sz) throws UnsupportedEncodingException
Create an InputStreamReader that uses the named character encoding and the specified input-buffer size.

Parameters:
in - An InputStream
enc - Name of encoding to be used
sz - Input-buffer size
Throws: UnsupportedEncodingException
If no converter can be found for the specified encoding
Throws: IllegalArgumentException
If sz is <= 0
 o InputStreamReader
  public InputStreamReader(InputStream in,
                           ByteToCharConverter btc)
Create an InputStreamReader that uses the specified byte-to-character converter and the default input-buffer size. The converter will be reset before its first use. Note: Because the converter has state, it should not be used after the constructor returns.

Parameters:
in - An InputStream
btc - A ByteToCharConverter
 o InputStreamReader
  public InputStreamReader(InputStream in,
                           ByteToCharConverter btc,
                           int sz)
Create an InputStreamReader that uses the specified byte-to-character converter and the specified input-buffer size. The converter will be reset before its first use. Note: Because the converter has state, it should not be used after the constructor returns.

Parameters:
in - An InputStream
btc - A ByteToCharConverter
sz - Input-buffer size
Throws: IllegalArgumentException
If sz is <= 0

Methods

 o getCharacterEncoding
  public String getCharacterEncoding() throws IOException
Return the name of the encoding being used by this stream.

Throws: IOException
If an I/O error occurs
 o read
  public int read() throws IOException
Read a single character. This method is not very efficient, as it invokes the encoding converter once for each character read.

Returns:
The character read, or -1 if the end of the stream has been reached
Throws: IOException
If an I/O error occurs
Overrides:
read in class Reader
See Also:
read
 o read
  public int read(char cbuf[],
                  int off,
                  int len) throws IOException
Read characters into a portion of an array.

Parameters:
cbuf - Destination buffer
off - Offset at which to start storing characters
len - Maximum number of characters to read
Returns:
The number of characters read, or -1 if the end of the stream has been reached
Throws: IOException
If an I/O error occurs
Overrides:
read in class Reader
 o ready
  public boolean ready() throws IOException
Tell whether this stream is ready to be read. An InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the underlying byte stream.

Throws: IOException
If an I/O error occurs
Overrides:
ready in class Reader
 o close
  public void close() throws IOException
Close the stream.

Throws: IOException
If an I/O error occurs
Overrides:
close in class Reader

All Packages  Class Hierarchy  This Package  Previous  Next  Index