java.io
Class InputStreamReader

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.io.Reader sample code for java.io.Reader definition code for java.io.Reader 
      extended by java.io.InputStreamReader
All Implemented Interfaces:
Closeable sample code for java.io.Closeable definition code for java.io.Closeable , Readable sample code for java.lang.Readable definition code for java.lang.Readable
Direct Known Subclasses:
FileReader sample code for java.io.FileReader definition code for java.io.FileReader

public class InputStreamReader
extends Reader sample code for java.io.Reader definition code for java.io.Reader

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset . The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.

Each invocation of one of an InputStreamReader's read() methods may cause one or more bytes to be read from the underlying byte-input stream. To enable the efficient conversion of bytes to characters, more bytes may be read ahead from the underlying stream than are necessary to satisfy the current read operation.

For top efficiency, consider wrapping an InputStreamReader within a BufferedReader. For example:

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

Since:
JDK1.1
See Also:
BufferedReader sample code for java.io.BufferedReader definition code for java.io.BufferedReader , InputStream sample code for java.io.InputStream definition code for java.io.InputStream , Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset

Field Summary
 
Fields inherited from class java.io.Reader sample code for java.io.Reader definition code for java.io.Reader
lock sample code for java.io.Reader.lock definition code for java.io.Reader.lock
 
Constructor Summary
InputStreamReader sample code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream) definition code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream) (InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in)
          Create an InputStreamReader that uses the default charset.
InputStreamReader sample code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream, java.nio.charset.Charset) definition code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream, java.nio.charset.Charset) (InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in, Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset  cs)
          Create an InputStreamReader that uses the given charset.
InputStreamReader sample code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream, java.nio.charset.CharsetDecoder) definition code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream, java.nio.charset.CharsetDecoder) (InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in, CharsetDecoder sample code for java.nio.charset.CharsetDecoder definition code for java.nio.charset.CharsetDecoder  dec)
          Create an InputStreamReader that uses the given charset decoder.
InputStreamReader sample code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream, java.lang.String) definition code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream, java.lang.String) (InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in, String sample code for java.lang.String definition code for java.lang.String  charsetName)
          Create an InputStreamReader that uses the named charset.
 
Method Summary
 void close sample code for java.io.InputStreamReader.close() definition code for java.io.InputStreamReader.close() ()
          Close the stream.
 String sample code for java.lang.String definition code for java.lang.String getEncoding sample code for java.io.InputStreamReader.getEncoding() definition code for java.io.InputStreamReader.getEncoding() ()
          Return the name of the character encoding being used by this stream.
 int read sample code for java.io.InputStreamReader.read() definition code for java.io.InputStreamReader.read() ()
          Read a single character.
 int read sample code for java.io.InputStreamReader.read(char[], int, int) definition code for java.io.InputStreamReader.read(char[], int, int) (char[] cbuf, int offset, int length)
          Read characters into a portion of an array.
 boolean ready sample code for java.io.InputStreamReader.ready() definition code for java.io.InputStreamReader.ready() ()
          Tell whether this stream is ready to be read.
 
Methods inherited from class java.io.Reader sample code for java.io.Reader definition code for java.io.Reader
mark sample code for java.io.Reader.mark(int) definition code for java.io.Reader.mark(int) , markSupported sample code for java.io.Reader.markSupported() definition code for java.io.Reader.markSupported() , read sample code for java.io.Reader.read(char[]) definition code for java.io.Reader.read(char[]) , read sample code for java.io.Reader.read(java.nio.CharBuffer) definition code for java.io.Reader.read(java.nio.CharBuffer) , reset sample code for java.io.Reader.reset() definition code for java.io.Reader.reset() , skip sample code for java.io.Reader.skip(long) definition code for java.io.Reader.skip(long)
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , toString sample code for java.lang.Object.toString() definition code for java.lang.Object.toString() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Constructor Detail

InputStreamReader sample code for java.io.InputStreamReader(java.io.InputStream) definition code for java.io.InputStreamReader(java.io.InputStream)

public InputStreamReader(InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in)
Create an InputStreamReader that uses the default charset.

Parameters:
in - An InputStream

InputStreamReader sample code for java.io.InputStreamReader(java.io.InputStream, java.lang.String) definition code for java.io.InputStreamReader(java.io.InputStream, java.lang.String)

public InputStreamReader(InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in,
                         String sample code for java.lang.String definition code for java.lang.String  charsetName)
                  throws UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException 
Create an InputStreamReader that uses the named charset.

Parameters:
in - An InputStream
charsetName - The name of a supported charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset
Throws:
UnsupportedEncodingException sample code for java.io.UnsupportedEncodingException definition code for java.io.UnsupportedEncodingException - If the named charset is not supported

InputStreamReader sample code for java.io.InputStreamReader(java.io.InputStream, java.nio.charset.Charset) definition code for java.io.InputStreamReader(java.io.InputStream, java.nio.charset.Charset)

public InputStreamReader(InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in,
                         Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset  cs)
Create an InputStreamReader that uses the given charset.

Parameters:
in - An InputStream
cs - A charset
Since:
1.4

InputStreamReader sample code for java.io.InputStreamReader(java.io.InputStream, java.nio.charset.CharsetDecoder) definition code for java.io.InputStreamReader(java.io.InputStream, java.nio.charset.CharsetDecoder)

public InputStreamReader(InputStream sample code for java.io.InputStream definition code for java.io.InputStream  in,
                         CharsetDecoder sample code for java.nio.charset.CharsetDecoder definition code for java.nio.charset.CharsetDecoder  dec)
Create an InputStreamReader that uses the given charset decoder.

Parameters:
in - An InputStream
dec - A charset decoder
Since:
1.4
Method Detail

getEncoding sample code for java.io.InputStreamReader.getEncoding() definition code for java.io.InputStreamReader.getEncoding()

public String sample code for java.lang.String definition code for java.lang.String  getEncoding()
Return the name of the character encoding being used by this stream.

If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the InputStreamReader(InputStream, String) sample code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream, java.lang.String) definition code for java.io.InputStreamReader.InputStreamReader(java.io.InputStream, java.lang.String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.

Returns:
The historical name of this encoding, or possibly null if the stream has been closed
See Also:
Charset sample code for java.nio.charset.Charset definition code for java.nio.charset.Charset

read sample code for java.io.InputStreamReader.read() definition code for java.io.InputStreamReader.read()

public int read()
         throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Read a single character.

Overrides:
read sample code for java.io.Reader.read() definition code for java.io.Reader.read() in class Reader sample code for java.io.Reader definition code for java.io.Reader
Returns:
The character read, or -1 if the end of the stream has been reached
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

read sample code for java.io.InputStreamReader.read(char[], int, int) definition code for java.io.InputStreamReader.read(char[], int, int)

public int read(char[] cbuf,
                int offset,
                int length)
         throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Read characters into a portion of an array.

Specified by:
read sample code for java.io.Reader.read(char[], int, int) definition code for java.io.Reader.read(char[], int, int) in class Reader sample code for java.io.Reader definition code for java.io.Reader
Parameters:
cbuf - Destination buffer
offset - Offset at which to start storing characters
length - 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 sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

ready sample code for java.io.InputStreamReader.ready() definition code for java.io.InputStreamReader.ready()

public boolean ready()
              throws IOException sample code for java.io.IOException definition code for java.io.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.

Overrides:
ready sample code for java.io.Reader.ready() definition code for java.io.Reader.ready() in class Reader sample code for java.io.Reader definition code for java.io.Reader
Returns:
True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block.
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs

close sample code for java.io.InputStreamReader.close() definition code for java.io.InputStreamReader.close()

public void close()
           throws IOException sample code for java.io.IOException definition code for java.io.IOException 
Close the stream.

Specified by:
close sample code for java.io.Closeable.close() definition code for java.io.Closeable.close() in interface Closeable sample code for java.io.Closeable definition code for java.io.Closeable
Specified by:
close sample code for java.io.Reader.close() definition code for java.io.Reader.close() in class Reader sample code for java.io.Reader definition code for java.io.Reader
Throws:
IOException sample code for java.io.IOException definition code for java.io.IOException - If an I/O error occurs