java.io
public class StringReader extends Reader
String
to be read as a character
input stream.
The mark/reset functionality in this class behaves differently than
normal. If no mark has been set, then calling the reset()
method rewinds the read pointer to the beginning of the String
.
Constructor and Description |
---|
StringReader(String buffer)
Create a new
StringReader that will read chars from the
passed in String . |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the stream.
|
void |
mark(int readAheadLimit)
Marks a position in the input to which the stream can be
"reset" by calling the
reset() method. |
boolean |
markSupported()
Returns a boolean that indicates whether the mark/reset
methods are supported in this class.
|
int |
read()
Reads an char from the input stream and returns it
as an int in the range of 0-65535.
|
int |
read(char[] b,
int off,
int len)
Read chars from a stream and stores them into a caller
supplied buffer.
|
boolean |
ready()
This method determines if the stream is ready to be read.
|
void |
reset()
Sets the read position in the stream to the previously
marked position or to 0 (i.e., the beginning of the stream) if the mark
has not already been set.
|
long |
skip(long n)
This method attempts to skip the requested number of chars in the
input stream.
|
public StringReader(String buffer)
StringReader
that will read chars from the
passed in String
. This stream will read from the beginning
to the end of the String
.buffer
- The String
this stream will read from.public void close()
Reader
IOException
.public void mark(int readAheadLimit) throws IOException
Reader
reset()
method. The parameter
readlimit
is the number of chars that can be read from the
stream after setting the mark before the mark becomes invalid. For
example, if mark()
is called with a read limit of 10, then
when 11 chars of data are read from the stream before the
reset()
method is called, then the mark is invalid and the
stream object instance is not required to remember the mark.mark
in class Reader
readAheadLimit
- The number of chars that can be read before the mark
becomes invalidIOException
- If an error occurs such as mark not being
supported for this classpublic boolean markSupported()
Reader
This method always returns false
in this class, but
subclasses can override this method to return true
if they
support mark/reset functionality.
markSupported
in class Reader
true
if mark/reset functionality is supported,
false
otherwisepublic int read() throws IOException
Reader
This method will block until the char can be read.
read
in class Reader
IOException
- If an error occurspublic int read(char[] b, int off, int len) throws IOException
Reader
offset
into the buffer and attempts to read len
chars. This method
can return before reading the number of chars requested. The actual
number of chars read is returned as an int. A -1 is returned to indicate
the end of the stream.
This method will block until some data can be read.
This method operates by calling the single char read()
method
in a loop until the desired number of chars are read. The read loop
stops short if the end of the stream is encountered or if an IOException
is encountered on any read operation except the first. If the first
attempt to read a chars fails, the IOException is allowed to propagate
upward. And subsequent IOException is caught and treated identically
to an end of stream condition. Subclasses can (and should if possible)
override this method to provide a more efficient implementation.
read
in class Reader
b
- The array into which the chars read should be storedoff
- The offset into the array to start storing charslen
- The requested number of chars to readIOException
- If an error occurs.public boolean ready() throws IOException
true
, unless
close() has previously been called in which case an IOException is
thrown.ready
in class Reader
true
to indicate that this object is ready to be read.IOException
- If the stream is closed.public void reset() throws IOException
reset
in class Reader
IOException
- Always thrown for this classpublic long skip(long n) throws IOException
pos
value by
the specified number of chars. It this would exceed the length of the
buffer, then only enough chars are skipped to position the stream at
the end of the buffer. The actual number of chars skipped is returned.skip
in class Reader
n
- The requested number of chars to skipIOException
- If an error occurs