Package java.io
Class CharArrayReader
java.lang.Object
java.io.Reader
java.io.CharArrayReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
public class CharArrayReader extends Reader
A specialized
Reader for reading the contents of a char array.- See Also:
CharArrayWriter
-
Field Summary
-
Constructor Summary
Constructors Constructor Description CharArrayReader(char[] buf)Constructs a CharArrayReader on the char arraybuf.CharArrayReader(char[] buf, int offset, int length)Constructs a CharArrayReader on the char arraybuf. -
Method Summary
Modifier and Type Method Description voidclose()This method closes this CharArrayReader.voidmark(int readLimit)Sets a mark position in this reader.booleanmarkSupported()Indicates whether this reader supports themark()andreset()methods.intread()Reads a single character from this reader and returns it as an integer with the two higher-order bytes set to 0.intread(char[] buffer, int offset, int count)Reads up tocountcharacters from this CharArrayReader and stores them atoffsetin the character arraybuffer.booleanready()Indicates whether this reader is ready to be read without blocking.voidreset()Resets this reader's position to the lastmark()location.longskip(long charCount)SkipscharCountcharacters in this reader.
-
Field Details
-
buf
protected char[] bufThe buffer for characters. -
pos
protected int posThe current buffer position. -
markedPos
protected int markedPosThe current mark position. -
count
protected int countThe ending index of the buffer.
-
-
Constructor Details
-
CharArrayReader
public CharArrayReader(char[] buf)Constructs a CharArrayReader on the char arraybuf. The size of the reader is set to the length of the buffer and the object to to read from is set tobuf.- Parameters:
buf- the char array from which to read.
-
CharArrayReader
public CharArrayReader(char[] buf, int offset, int length)Constructs a CharArrayReader on the char arraybuf. The size of the reader is set tolengthand the start position from which to read the buffer is set tooffset.- Parameters:
buf- the char array from which to read.offset- the index of the first character inbufto read.length- the number of characters that can be read frombuf.- Throws:
IllegalArgumentException- ifoffset < 0orlength < 0, or ifoffsetis greater than the size ofbuf.
-
-
Method Details
-
close
public void close()This method closes this CharArrayReader. Once it is closed, you can no longer read from it. Only the first invocation of this method has any effect. -
mark
Sets a mark position in this reader. The parameterreadLimitis ignored for CharArrayReaders. Callingreset()will reposition the reader back to the marked position provided the mark has not been invalidated.- Overrides:
markin classReader- Parameters:
readLimit- ignored for CharArrayReaders.- Throws:
IOException- if this reader is closed.- See Also:
Reader.markSupported(),Reader.reset()
-
markSupported
public boolean markSupported()Indicates whether this reader supports themark()andreset()methods.- Overrides:
markSupportedin classReader- Returns:
truefor CharArrayReader.- See Also:
mark(int),reset()
-
read
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 no more characters are available from this reader.- Overrides:
readin classReader- Returns:
- the character read as an int or -1 if the end of the reader has been reached.
- Throws:
IOException- if this reader is closed.
-
read
Reads up tocountcharacters from this CharArrayReader and stores them atoffsetin the character arraybuffer. Returns the number of characters actually read or -1 if the end of reader was encountered.- Specified by:
readin classReader- Throws:
IndexOutOfBoundsException- ifoffset < 0 || count < 0 || offset + count > buffer.length.IOException- if this reader is closed.
-
ready
Indicates whether this reader is ready to be read without blocking. Returnstrueif the nextreadwill not block. Returnsfalseif this reader may or may not block whenreadis called. The implementation in CharArrayReader always returnstrueeven when it has been closed.- Overrides:
readyin classReader- Returns:
trueif this reader will not block whenreadis called,falseif unknown or blocking will occur.- Throws:
IOException- if this reader is closed.- See Also:
Reader.read(),Reader.read(char[]),Reader.read(char[], int, int)
-
reset
Resets this reader's position to the lastmark()location. Invocations ofread()andskip()will occur from this new location. If this reader has not been marked, it is reset to the beginning of the string.- Overrides:
resetin classReader- Throws:
IOException- if this reader is closed.- See Also:
Reader.mark(int),Reader.markSupported()
-
skip
SkipscharCountcharacters in this reader. Subsequent calls toreadwill not return these characters unlessresetis used. This method does nothing and returns 0 ifcharCount <= 0.- Overrides:
skipin classReader- Returns:
- the number of characters actually skipped.
- Throws:
IOException- if this reader is closed.- See Also:
Reader.mark(int),Reader.markSupported(),Reader.reset()
-