java.io
Class StringReader

java.lang.Object
  extended by java.io.Reader
      extended by java.io.StringReader
All Implemented Interfaces:
Closeable, Readable

public class StringReader
extends Reader

This class permits a 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.


Field Summary
 
Fields inherited from class java.io.Reader
lock
 
Constructor Summary
StringReader(String buffer)
          Create a new StringReader that will read chars from the passed in String.
 
Method Summary
 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.
 
Methods inherited from class java.io.Reader
read, read
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StringReader

public StringReader(String buffer)
Create a new StringReader that will read chars from the passed in String. This stream will read from the beginning to the end of the String.

Parameters:
buffer - The String this stream will read from.
Method Detail

close

public void close()
Description copied from class: Reader
Closes the stream. Any futher attempts to read from the stream may generate an IOException.

Specified by:
close in interface Closeable
Specified by:
close in class Reader

mark

public void mark(int readAheadLimit)
          throws IOException
Description copied from class: Reader
Marks a position in the input to which the stream can be "reset" by calling the 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.

Overrides:
mark in class Reader
Parameters:
readAheadLimit - The number of chars that can be read before the mark becomes invalid
Throws:
IOException - If an error occurs such as mark not being supported for this class

markSupported

public boolean markSupported()
Description copied from class: Reader
Returns a boolean that indicates whether the mark/reset methods are supported in this class. Those methods can be used to remember a specific point in the stream and reset the stream to that point.

This method always returns false in this class, but subclasses can override this method to return true if they support mark/reset functionality.

Overrides:
markSupported in class Reader
Returns:
true if mark/reset functionality is supported, false otherwise

read

public int read()
         throws IOException
Description copied from class: Reader
Reads an char from the input stream and returns it as an int in the range of 0-65535. This method also will return -1 if the end of the stream has been reached.

This method will block until the char can be read.

Overrides:
read in class Reader
Returns:
The char read or -1 if end of stream
Throws:
IOException - If an error occurs

read

public int read(char[] b,
                int off,
                int len)
         throws IOException
Description copied from class: Reader
Read chars from a stream and stores them into a caller supplied buffer. It starts storing the data at index 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.

Specified by:
read in class Reader
Parameters:
b - The array into which the chars read should be stored
off - The offset into the array to start storing chars
len - The requested number of chars to read
Returns:
The actual number of chars read, or -1 if end of stream.
Throws:
IOException - If an error occurs.

ready

public boolean ready()
              throws IOException
This method determines if the stream is ready to be read. This class is always ready to read and so always returns true, unless close() has previously been called in which case an IOException is thrown.

Overrides:
ready in class Reader
Returns:
true to indicate that this object is ready to be read.
Throws:
IOException - If the stream is closed.

reset

public void reset()
           throws IOException
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.

Overrides:
reset in class Reader
Throws:
IOException - Always thrown for this class

skip

public long skip(long n)
          throws IOException
This method attempts to skip the requested number of chars in the input stream. It does this by advancing the 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.

Overrides:
skip in class Reader
Parameters:
n - The requested number of chars to skip
Returns:
The actual number of chars skipped.
Throws:
IOException - If an error occurs