Package java.io
Class StringReader
java.lang.Object
java.io.Reader
java.io.StringReader
- All Implemented Interfaces:
Closeable,AutoCloseable,Readable
public class StringReader extends Reader
A specialized
Reader that reads characters from a String in
a sequential manner.- See Also:
StringWriter
-
Field Summary
-
Constructor Summary
Constructors Constructor Description StringReader(String str)Construct a newStringReaderwithstras source. -
Method Summary
Modifier and Type Method Description voidclose()Closes this reader.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 the source string 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 the source string 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)MovescharCountcharacters in the source string.
-
Constructor Details
-
StringReader
Construct a newStringReaderwithstras 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. -
mark
Sets a mark position in this reader. The parameterreadLimitis ignored for this class. Callingreset()will reposition the reader back to the marked position.- Overrides:
markin classReader- Parameters:
readLimit- ignored forStringReaderinstances.- Throws:
IllegalArgumentException- ifreadLimit < 0.IOException- if this reader is closed.- See Also:
markSupported(),reset()
-
markSupported
public boolean markSupported()Indicates whether this reader supports themark()andreset()methods. This implementation returnstrue.- Overrides:
markSupportedin classReader- Returns:
- always
true.
-
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.- 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.
-
read
Reads up tocountcharacters from the source string and stores them atoffsetin the character arraybuffer. Returns the number of characters actually read or -1 if the end of the source string has been reached.- 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. This implementation always returnstrue.- Overrides:
readyin classReader- Returns:
- always
true. - Throws:
IOException- if this reader is closed.- See Also:
read(),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 source string.- Overrides:
resetin classReader- Throws:
IOException- if this reader is closed.- See Also:
mark(int),markSupported()
-
skip
MovescharCountcharacters 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.- Overrides:
skipin classReader- Parameters:
charCount- the maximum number of characters to skip. Positive values skip forward; negative values skip backward.- 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()
-