public final class UTF8StreamReader extends Reader
A UTF-8 stream reader.
This reader supports surrogate char
pairs (representing
characters in the range [U+10000 .. U+10FFFF]). It can also be used
to read characters unicodes (31 bits) directly
(ref. read()
).
Each invocation of one of the 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.
Instances of this class can be reused for different input streams
and can be part of a higher level component (e.g. parser) in order
to avoid dynamic buffer allocation when the input source changes.
Also wrapping using a java.io.BufferedReader
is unnescessary
as instances of this class embed their own data buffers.
Note: This reader is unsynchronized and does not test if the UTF-8 encoding is well-formed (e.g. UTF-8 sequences longer than necessary to encode a character).
UTF8StreamWriter
Constructor and Description |
---|
UTF8StreamReader()
Creates a UTF-8 reader having a byte buffer of moderate capacity (2048).
|
UTF8StreamReader(int capacity)
Creates a UTF-8 reader having a byte buffer of specified capacity.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes and
resets this reader for reuse. |
int |
read()
Reads a single character.
|
void |
read(Appendable dest)
Reads characters into the specified appendable.
|
int |
read(char[] cbuf,
int off,
int len)
Reads characters into a portion of an array.
|
boolean |
ready()
Indicates if this stream is ready to be read.
|
void |
reset() |
UTF8StreamReader |
setInput(InputStream inStream)
Sets the input stream to use for reading until this reader is closed.
|
UTF8StreamReader |
setInputStream(InputStream inStream)
Deprecated.
Replaced by
setInput(InputStream) |
public UTF8StreamReader()
public UTF8StreamReader(int capacity)
capacity
- the capacity of the byte buffer.public UTF8StreamReader setInput(InputStream inStream)
Reader reader = new UTF8StreamReader().setInput(inStream);
Reader reader = new java.io.InputStreamReader(inStream, "UTF-8");
inStream
- the input stream.IllegalStateException
- if this reader is being reused and
it has not been closed
or reset
.public boolean ready() throws IOException
ready
in class Reader
true
if the next read() is guaranteed not to block
for input; false
otherwise.IOException
- if an I/O error occurs.public void close() throws IOException
resets
this reader for reuse.close
in interface Closeable
close
in interface AutoCloseable
close
in class Reader
IOException
- if an I/O error occurs.public int read() throws IOException
read
in class Reader
IOException
- if an I/O error occurs.public int read(char[] cbuf, int off, int len) throws IOException
Note: Characters between U+10000 and U+10FFFF are represented
by surrogate pairs (two char
).
read
in class Reader
cbuf
- the destination buffer.off
- the offset at which to start storing characters.len
- the maximum number of characters to readIOException
- if an I/O error occurs.public void read(Appendable dest) throws IOException
dest
- the destination buffer.IOException
- if an I/O error occurs.public UTF8StreamReader setInputStream(InputStream inStream)
setInput(InputStream)
Copyright © 2005-2013 Javolution. All Rights Reserved.