java.io

Class StringReader

Implemented Interfaces:
AutoCloseable, 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)
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

close, mark, markSupported, read, read, read, read, ready, reset, skip

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

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 Details

close

public void close()
Closes the stream. Any futher attempts to read from the stream may generate an IOException.
Specified by:
close in interface Closeable
close in interface AutoCloseable
Overrides:
close in interface Reader

mark

public void mark(int readAheadLimit)
            throws IOException
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 interface Reader
Parameters:
Throws:
IOException - If an error occurs such as mark not being supported for this class

markSupported

public boolean markSupported()
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 interface Reader
Returns:
true if mark/reset functionality is supported, false otherwise

read

public int read()
            throws IOException
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 interface 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

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 interface 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 interface Reader

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 interface Reader
Parameters:
n - The requested number of chars to skip
Returns:
The actual number of chars skipped.

StringReader.java -- permits a String to be read as a character input stream Copyright (C) 1998, 1999, 2000, 2003 Free Software Foundation This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.