Class StringReader
- All Implemented Interfaces:
AutoCloseable
A specialized Reader that reads characters from a String in
a sequential manner.
See also
- StringWriter
-
Field Summary
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this reader.voidmark(int readLimit) Sets a mark position in this reader.booleanIndicates whether this reader supports themark()andreset()methods.intread()Reads a single character from the source string and returns it as an integer with the two higher-order bytes set to 0.intread(char[] buf, int offset, int len) Reads at mostlencharacters from the source string 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 ns) Movesnscharacters in the source string.
-
Constructor Details
-
StringReader
Construct a new
StringReaderwithstras source. The size of the reader is set to thelength()of the string and the Object to synchronize access through is set tostr.Parameters
str: the source string for this reader.
-
-
Method Details
-
close
public void close()Closes this reader. Once it is closed, read operations on this reader will throw anIOException. Only the first invocation of this method has any effect.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein classReader
-
mark
Sets a mark position in this reader. The parameter
readLimitis ignored for this class. Callingreset()will reposition the reader back to the marked position.Parameters
readLimit: ignored forStringReaderinstances.
Throws
-
IllegalArgumentException: ifreadLimit < 0. -
IOException: if this reader is closed.
See also
-
#markSupported()
-
#reset()
- Overrides:
markin classReader- Throws:
IOException
-
markSupported
public boolean markSupported()Indicates whether this reader supports the
mark()andreset()methods. This implementation returnstrue.Returns
always
true.- Overrides:
markSupportedin classReader
-
read
Reads a single character from the source string and returns it as an integer with the two higher-order bytes set to 0. Returns -1 if the end of the source string has been reached.
Returns
- Overrides:
readin classReader- Returns:
the character read or -1 if the end of the source string has been reached.
Throws
IOException: if this reader is closed.
- Throws:
IOException
-
read
Reads at most
lencharacters from the source string and stores them atoffsetin the character arraybuf. Returns the number of characters actually read or -1 if the end of the source string has been reached.Parameters
-
buf: 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:
the 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 greater than the size ofbuf. -
IOException: if this reader is closed.
-
- Throws:
IOException
-
-
ready
Indicates whether this reader is ready to be read without blocking. This implementation always returns
true.Returns
always
true.Throws
IOException: if this reader is closed.
See also
-
#read()
-
#read(char[], int, int)
- Overrides:
readyin classReader- 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 source string.Throws
IOException: if this reader is closed.
See also
-
#mark(int)
-
#markSupported()
- Overrides:
resetin classReader- Throws:
IOException
-
skip
Moves
nscharacters in the source string. Unlike theoverridden method, this method may skip negative skip distances: this rewinds the input so that characters may be read again. When the end of the source string has been reached, the input cannot be rewound.Parameters
ns: @param ns the maximum number of characters to skip. Positive values skip forward; negative values skip backward.
Returns
- Overrides:
skipin classReader- Returns:
the number of characters actually skipped. This is bounded below by the number of characters already read and above by the number of characters remaining:
-(num chars already read) <= distance skipped <= num chars remaining.Throws
IOException: if this reader is closed.
See also
-
#mark(int)
-
#markSupported()
-
#reset()
- Throws:
IOException
-