Top Description Fields Constructors Methods
java.io

public Class InputStreamReader

extends Reader
Class Inheritance
Known Direct Subclasses
java.io.FileReader
Imports
java.nio.CharBuffer, java.nio.charset.Charset, .CharsetDecoder, jdk.internal.misc.InternalLock, sun.nio.cs.StreamDecoder

An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the default charset may be used.

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(anInputStream));
BufferedReader in = new BufferedReader(new InputStreamReader(anInputStream));
Author
Mark Reinhold
Since
1.1
See Also
BufferedReader, InputStream, Charset

Field Summary

Modifier and TypeField and Description
private final StreamDecoder
Inherited from java.io.Reader:
lock

Constructor Summary

AccessConstructor and Description
public
InputStreamReader(InputStream
An InputStream
in
)

Creates an InputStreamReader that uses the default charset.

public
InputStreamReader(InputStream
An InputStream
in
,
String
The name of a supported charset
charsetName
)

Creates an InputStreamReader that uses the named charset.

public
InputStreamReader(InputStream
An InputStream
in
,
Charset
A charset
cs
)

Creates an InputStreamReader that uses the given charset.

public
InputStreamReader(InputStream
An InputStream
in
,
CharsetDecoder
A charset decoder
dec
)

Creates an InputStreamReader that uses the given charset decoder.

Method Summary

Modifier and TypeMethod and Description
public void
close()

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Closes the stream and releases any system resources associated with it.

public String

Returns:

The historical name of this encoding, or null if the stream has been closed
getEncoding
()

Returns the name of the character encoding being used by this stream.

private static Object
lockFor(InputStreamReader reader)

Return the lock object for the given reader's stream decoder.

public int
read(CharBuffer
the buffer to read characters into
target
)

Overrides java.io.Reader.read.

Implements java.lang.Readable.read.

Attempts to read characters into the specified character buffer.

public int

Returns:

The character read, or -1 if the end of the stream has been reached
read
()

Overrides java.io.Reader.read.

Reads a single character.

public int
read(char[]
Destination buffer
cbuf
,
int
Offset at which to start storing characters
off
,
int
Maximum number of characters to read
len
)

Implements abstract java.io.Reader.read.

Reads characters into a portion of an array.

public boolean
ready()

Overrides java.io.Reader.ready.

Tells whether this stream is ready to be read.

Inherited from java.io.Reader:
markmarkSupportednullReaderreadresetskiptransferTo

Field Detail

sdback to summary
private final StreamDecoder sd

Constructor Detail

InputStreamReaderback to summary
public InputStreamReader(InputStream in)

Creates an InputStreamReader that uses the default charset.

Parameters
in:InputStream

An InputStream

See Also
Charset#defaultCharset()
InputStreamReaderback to summary
public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException

Creates an InputStreamReader that uses the named charset.

Parameters
in:InputStream

An InputStream

charsetName:String

The name of a supported charset

Exceptions
UnsupportedEncodingException:
If the named charset is not supported
InputStreamReaderback to summary
public InputStreamReader(InputStream in, Charset cs)

Creates an InputStreamReader that uses the given charset.

Parameters
in:InputStream

An InputStream

cs:Charset

A charset

Since
1.4
InputStreamReaderback to summary
public InputStreamReader(InputStream in, CharsetDecoder dec)

Creates an InputStreamReader that uses the given charset decoder.

Parameters
in:InputStream

An InputStream

dec:CharsetDecoder

A charset decoder

Since
1.4

Method Detail

closeback to summary
public void close() throws IOException

Implements abstract java.io.Reader.close.

Implements java.io.Closeable.close.

Doc from java.io.Reader.close.

Closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), ready(), mark(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

Exceptions
IOException:
If an I/O error occurs
getEncodingback to summary
public String getEncoding()

Returns 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) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method will return null if the stream has been closed.

Returns:String

The historical name of this encoding, or null if the stream has been closed

See Also
Charset
lockForback to summary
private static Object lockFor(InputStreamReader reader)

Return the lock object for the given reader's stream decoder. If the reader type is trusted then an internal lock can be used. If the reader type is not trusted then the reader object is the lock.

readback to summary
public int read(CharBuffer target) throws IOException

Overrides java.io.Reader.read.

Implements java.lang.Readable.read.

Doc from java.io.Reader.read.

Attempts to read characters into the specified character buffer. The buffer is used as a repository of characters as-is: the only changes made are the results of a put operation. No flipping or rewinding of the buffer is performed. If the length of the specified character buffer is zero, then no characters will be read and zero will be returned.

Parameters
target:CharBuffer

the buffer to read characters into

Returns:int

The number of characters added to the buffer, possibly zero, or -1 if this source of characters is at its end

Exceptions
IOException:
if an I/O error occurs
readback to summary
public int read() throws IOException

Overrides java.io.Reader.read.

Reads a single character.

Returns:int

The character read, or -1 if the end of the stream has been reached

Exceptions
IOException:
If an I/O error occurs
readback to summary
public int read(char[] cbuf, int off, int len) throws IOException

Implements abstract java.io.Reader.read.

Doc from java.io.Reader.read.

Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.

If len is zero, then no characters are read and 0 is returned; otherwise, there is an attempt to read at least one character. If no character is available because the stream is at its end, the value -1 is returned; otherwise, at least one character is read and stored into cbuf.

Parameters
cbuf:char[]

Destination buffer

off:int

Offset at which to start storing characters

len:int

Maximum number of characters to read

Returns:int

The number of characters read, or -1 if the end of the stream has been reached

Exceptions
IOException:
If an I/O error occurs
IndexOutOfBoundsException:
If off is negative, or len is negative, or len is greater than cbuf.length - off
readyback to summary
public boolean ready() throws IOException

Overrides java.io.Reader.ready.

Tells 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.

Returns:boolean

Doc from java.io.Reader.ready.

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.

Exceptions
IOException:
If an I/O error occurs