Package org.jline.utils
Class InputStreamReader
- java.lang.Object
-
- java.io.Reader
-
- org.jline.utils.InputStreamReader
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,java.lang.Readable
public class InputStreamReader extends java.io.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
-
-
Constructor Summary
Constructors Constructor Description InputStreamReader(java.io.InputStream in)
Constructs a newInputStreamReader
on theInputStream
in
.InputStreamReader(java.io.InputStream in, java.lang.String enc)
Constructs a new InputStreamReader on the InputStreamin
.InputStreamReader(java.io.InputStream in, java.nio.charset.Charset charset)
Constructs a new InputStreamReader on the InputStreamin
and Charsetcharset
.InputStreamReader(java.io.InputStream in, java.nio.charset.CharsetDecoder dec)
Constructs a new InputStreamReader on the InputStreamin
and CharsetDecoderdec
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes this reader.java.lang.String
getEncoding()
Returns the name of the encoding used to convert bytes into characters.int
read()
Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0.int
read(char[] buf, int offset, int length)
Reads at mostlength
characters from this reader and stores them at positionoffset
in the character arraybuf
.boolean
ready()
Indicates whether this reader is ready to be read without blocking.
-
-
-
Constructor Detail
-
InputStreamReader
public InputStreamReader(java.io.InputStream in)
Constructs a newInputStreamReader
on theInputStream
in
. 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
public InputStreamReader(java.io.InputStream in, java.lang.String enc) throws java.io.UnsupportedEncodingException
Constructs a new InputStreamReader on the InputStreamin
. The character converter that is used to decode bytes into characters is identified by name byenc
. If the encoding cannot be found, an UnsupportedEncodingException error is thrown.- Parameters:
in
- the InputStream from which to read characters.enc
- identifies the character converter to use.- Throws:
java.lang.NullPointerException
- ifenc
isnull
.java.io.UnsupportedEncodingException
- if the encoding specified byenc
cannot be found.
-
InputStreamReader
public InputStreamReader(java.io.InputStream in, java.nio.charset.CharsetDecoder dec)
Constructs a new InputStreamReader on the InputStreamin
and CharsetDecoderdec
.- Parameters:
in
- the source InputStream from which to read characters.dec
- the CharsetDecoder used by the character conversion.
-
InputStreamReader
public InputStreamReader(java.io.InputStream in, java.nio.charset.Charset charset)
Constructs a new InputStreamReader on the InputStreamin
and Charsetcharset
.- Parameters:
in
- the source InputStream from which to read characters.charset
- the Charset that defines the character converter
-
-
Method Detail
-
close
public void close() throws java.io.IOException
Closes this reader. This implementation closes the source InputStream and releases all local storage.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classjava.io.Reader
- Throws:
java.io.IOException
- if an error occurs attempting to close this reader.
-
getEncoding
public java.lang.String getEncoding()
Returns the name of the encoding used to convert bytes into characters. The valuenull
is returned if this reader has been closed.- Returns:
- the name of the character converter or
null
if this reader is closed.
-
read
public int read() throws java.io.IOException
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:
read
in classjava.io.Reader
- Returns:
- the character read or -1 if the end of the reader has been reached.
- Throws:
java.io.IOException
- if this reader is closed or some other I/O error occurs.
-
read
public int read(char[] buf, int offset, int length) throws java.io.IOException
Reads at mostlength
characters from this reader and stores them at positionoffset
in the character arraybuf
. 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:
read
in classjava.io.Reader
- Parameters:
buf
- the array to store the characters read.offset
- the initial position inbuf
to store the characters read from this reader.length
- the maximum number of characters to read.- Returns:
- the number of characters read or -1 if the end of the reader has been reached.
- Throws:
java.lang.IndexOutOfBoundsException
- ifoffset < 0
orlength < 0
, or ifoffset + length
is greater than the length ofbuf
.java.io.IOException
- if this reader is closed or some other I/O error occurs.
-
ready
public boolean ready() throws java.io.IOException
Indicates whether this reader is ready to be read without blocking. If the result istrue
, the nextread()
will not block. If the result isfalse
then this reader may or may not block whenread()
is called. This implementation returnstrue
if there are bytes available in the buffer or the source stream has bytes available.- Overrides:
ready
in classjava.io.Reader
- Returns:
true
if the receiver will not block whenread()
is called,false
if unknown or blocking will occur.- Throws:
java.io.IOException
- if this reader is closed or some other I/O error occurs.
-
-