public class CharArrayReader extends Reader
Reader
for reading the contents of a char array.CharArrayWriter
Modifier and Type | Field and Description |
---|---|
protected char[] |
buf
The buffer for characters.
|
protected int |
count
The ending index of the buffer.
|
protected int |
markedPos
The current mark position.
|
protected int |
pos
The current buffer position.
|
Constructor and Description |
---|
CharArrayReader(char[] buf)
Constructs a CharArrayReader on the char array
buf . |
CharArrayReader(char[] buf,
int offset,
int length)
Constructs a CharArrayReader on the char array
buf . |
Modifier and Type | Method and Description |
---|---|
void |
close()
This method closes this CharArrayReader.
|
void |
mark(int readLimit)
Sets a mark position in this reader.
|
boolean |
markSupported()
Indicates whether this reader supports the
mark() and
reset() methods. |
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[] buffer,
int offset,
int len)
Reads at most
count characters from this CharArrayReader and
stores them at offset in the character array buf . |
boolean |
ready()
Indicates whether this reader is ready to be read without blocking.
|
void |
reset()
Resets this reader's position to the last
mark() location. |
long |
skip(long n)
Skips
count number of characters in this reader. |
protected char[] buf
protected int pos
protected int markedPos
protected int count
public CharArrayReader(char[] buf)
buf
. The size of
the reader is set to the length of the buffer and the object to read
from is set to buf
.buf
- the char array from which to read.public CharArrayReader(char[] buf, int offset, int length)
buf
. The size of
the reader is set to length
and the start position from which to
read the buffer is set to offset
.buf
- the char array from which to read.offset
- the index of the first character in buf
to read.length
- the number of characters that can be read from buf
.IllegalArgumentException
- if offset < 0
or length < 0
, or if
offset
is greater than the size of buf
.public void close()
close
in interface AutoCloseable
close
in class Reader
public void mark(int readLimit) throws IOException
readLimit
is
ignored for CharArrayReaders. Calling reset()
will reposition the
reader back to the marked position provided the mark has not been
invalidated.mark
in class Reader
readLimit
- ignored for CharArrayReaders.IOException
- if this reader is closed.public boolean markSupported()
mark()
and
reset()
methods.markSupported
in class Reader
true
for CharArrayReader.mark(int)
,
reset()
public int read() throws IOException
read
in class Reader
IOException
- if this reader is closed.public int read(char[] buffer, int offset, int len) throws IOException
count
characters from this CharArrayReader and
stores them at offset
in the character array buf
.
Returns the number of characters actually read or -1 if the end of reader
was encountered.read
in class Reader
buffer
- the character array to store the characters read.offset
- the initial position in buffer
to store the characters
read from this reader.len
- the maximum number of characters to read.IndexOutOfBoundsException
- if offset < 0
or len < 0
, or if
offset + len
is bigger than the size of
buffer
.IOException
- if this reader is closed.public boolean ready() throws IOException
true
if the next read
will not block. Returns
false
if this reader may or may not block when read
is
called. The implementation in CharArrayReader always returns true
even when it has been closed.ready
in class Reader
true
if this reader will not block when read
is
called, false
if unknown or blocking will occur.IOException
- if this reader is closed.public void reset() throws IOException
mark()
location.
Invocations of read()
and skip()
will occur from this new
location. If this reader has not been marked, it is reset to the
beginning of the string.reset
in class Reader
IOException
- if this reader is closed.public long skip(long n) throws IOException
count
number of characters in this reader. Subsequent
read()
s will not return these characters unless reset()
is used. This method does nothing and returns 0 if n
is negative.skip
in class Reader
n
- the number of characters to skip.IOException
- if this reader is closed.