Class CharArrayReader
- All Implemented Interfaces:
Closeable, AutoCloseable, Readable
A specialized Reader for reading the contents of a char array.
See also
- CharArrayWriter
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected char[]The buffer for characters.protected intThe ending index of the buffer.protected intThe current mark position.protected intThe current buffer position. -
Constructor Summary
ConstructorsConstructorDescriptionCharArrayReader(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 TypeMethodDescriptionvoidclose()This method closes this CharArrayReader.voidmark(int readLimit) Sets a mark position in this reader.booleanIndicates 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 len) Reads at mostcountcharacters from this CharArrayReader and stores them atoffsetin the character arraybuf.booleanready()Indicates whether this reader is ready to be read without blocking.voidreset()Resets this reader's position to the lastmark()location.longskip(long n) Skipscountnumber of characters 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 array
buf. The size of the reader is set to the length of the buffer and the object 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 array
buf. 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: @throws IllegalArgumentException ifoffset < 0orlength < 0, or ifoffsetis greater than the size ofbuf.
-
-
-
Method Details
-
close
-
mark
Sets a mark position in this reader. The parameter
readLimitis ignored for CharArrayReaders. Callingreset()will reposition the reader back to the marked position provided the mark has not been invalidated.Parameters
readLimit: ignored for CharArrayReaders.
Throws
IOException: if this reader is closed.
- Overrides:
markin classReader- Throws:
IOException
-
markSupported
public boolean markSupported()Indicates whether this reader supports the
mark()andreset()methods.Returns
truefor CharArrayReader.See also
-
#mark(int)
-
#reset()
- Overrides:
markSupportedin classReader
-
-
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.
Returns
- 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.
- Throws:
IOException
-
read
Reads at most
countcharacters from this CharArrayReader and stores them atoffsetin the character arraybuf. Returns the number of characters actually read or -1 if the end of reader was encountered.Parameters
-
buffer: the character array to store the characters read. -
offset: @param offset the initial position inbufferto store the characters read from this reader. -
len: the maximum number of characters to read.
Returns
- Specified by:
readin classReader- Returns:
number of characters read or -1 if the end of the reader has been reached.
Throws
-
IndexOutOfBoundsException: @throws IndexOutOfBoundsException ifoffset < 0orlen < 0, or ifoffset + lenis bigger than the size ofbuffer. -
IOException: if this reader is closed.
-
- Throws:
IOException
-
-
ready
Indicates whether this reader is ready to be read without blocking. Returns
trueif 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.Returns
- 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.
- Throws:
IOException
-
reset
Resets this reader's position to the last
mark()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.Throws
IOException: if this reader is closed.
- Overrides:
resetin classReader- Throws:
IOException
-
skip
Skips
countnumber of characters in this reader. Subsequentread()s will not return these characters unlessreset()is used. This method does nothing and returns 0 ifnis negative.Parameters
n: the number of characters to skip.
Returns
the number of characters actually skipped.
Throws
IOException: if this reader is closed.
- Overrides:
skipin classReader- Throws:
IOException
-