FileDocCategorySizeDatePackage
CharSequenceReader.javaAPI DocAndroid 1.5 API4739Wed May 06 22:42:46 BST 2009org.apache.commons.io.input

CharSequenceReader

public class CharSequenceReader extends Reader implements Serializable
{@link Reader} implementation that can read from String, StringBuffer, StringBuilder or CharBuffer.

Note: Supports {@link #mark(int)} and {@link #reset()}.

version
$Revision: 610516 $ $Date: 2008-01-09 19:05:05 +0000 (Wed, 09 Jan 2008) $
since
Commons IO 1.4

Fields Summary
private final CharSequence
charSequence
private int
idx
private int
mark
Constructors Summary
public CharSequenceReader(CharSequence charSequence)
Construct a new instance with the specified character sequence.

param
charSequence The character sequence, may be null

        this.charSequence = (charSequence != null ? charSequence : "");
    
Methods Summary
public voidclose()
Close resets the file back to the start and removes any marked position.

        idx = 0;
        mark = 0;
    
public voidmark(int readAheadLimit)
Mark the current position.

param
readAheadLimit ignored

        mark = idx;
    
public booleanmarkSupported()
Mark is supported (returns true).

return
true

        return true;
    
public intread()
Read a single character.

return
the next character from the character sequence or -1 if the end has been reached.

        if (idx >= charSequence.length()) {
            return -1;
        } else {
            return charSequence.charAt(idx++);
        }
    
public intread(char[] array, int offset, int length)
Read the sepcified number of characters into the array.

param
array The array to store the characters in
param
offset The starting position in the array to store
param
length The maximum number of characters to read
return
The number of characters read or -1 if there are no more

        if (idx >= charSequence.length()) {
            return -1;
        }
        if (array == null) {
            throw new NullPointerException("Character array is missing");
        }
        if (length < 0 || (offset + length) > array.length) {
            throw new IndexOutOfBoundsException("Array Size=" + array.length +
                    ", offset=" + offset + ", length=" + length);
        }
        int count = 0;
        for (int i = 0; i < length; i++) {
            int c = read();
            if (c == -1) {
                return count;
            }
            array[offset + i] = (char)c;
            count++;
        }
        return count;
    
public voidreset()
Reset the reader to the last marked position (or the beginning if mark has not been called).

        idx = mark;
    
public longskip(long n)
Skip the specified number of characters.

param
n The number of characters to skip
return
The actual number of characters skipped

        if (n < 0) {
            throw new IllegalArgumentException(
                    "Number of characters to skip is less than zero: " + n);
        }
        if (idx >= charSequence.length()) {
            return -1;
        }
        int dest = (int)Math.min(charSequence.length(), (idx + n));
        int count = dest - idx;
        idx = dest;
        return count;
    
public java.lang.StringtoString()
Return a String representation of the underlying character sequence.

return
The contents of the character sequence

        return charSequence.toString();