FileDocCategorySizeDatePackage
ReaderCharacterIterator.javaAPI DocJava SE 6 API3941Tue Jun 10 00:22:24 BST 2008com.sun.org.apache.regexp.internal

ReaderCharacterIterator

public final class ReaderCharacterIterator extends Object implements CharacterIterator
Encapsulates java.io.Reader as CharacterIterator
author
Ales Novak
version
CVS $Id: ReaderCharacterIterator.java,v 1.1.2.1 2005/08/01 00:02:57 jeffsuttor Exp $

Fields Summary
private final Reader
reader
Underlying reader
private final StringBuffer
buff
Buffer of read chars
private boolean
closed
read end?
Constructors Summary
public ReaderCharacterIterator(Reader reader)

param
reader a Reader, which is parsed

        this.reader = reader;
        this.buff = new StringBuffer(512);
        this.closed = false;
    
Methods Summary
public charcharAt(int pos)

return
a character at the specified position.

        try
        {
            ensure(pos);
            return buff.charAt(pos);
        }
        catch (IOException e)
        {
            throw new StringIndexOutOfBoundsException(e.getMessage());
        }
    
private voidensure(int idx)
Reads chars up to the idx

        if (closed)
        {
            return;
        }

        if (idx < buff.length())
        {
            return;
        }
        read(idx + 1 - buff.length());
    
public booleanisEnd(int pos)

return
true iff if the specified index is after the end of the character stream

        if (buff.length() > pos)
        {
            return false;
        }
        else
        {
            try
            {
                ensure(pos);
                return (buff.length() <= pos);
            }
            catch (IOException e)
            {
                throw new StringIndexOutOfBoundsException(e.getMessage());
            }
        }
    
private intread(int n)
Reads n characters from the stream and appends them to the buffer

        if (closed)
        {
            return 0;
        }

        char[] c = new char[n];
        int count = 0;
        int read = 0;

        do
        {
            read = reader.read(c);
            if (read < 0) // EOF
            {
                closed = true;
                break;
            }
            count += read;
            buff.append(c, 0, read);
        }
        while (count < n);

        return count;
    
private voidreadAll()
Reads rest of the stream.

        while(! closed)
        {
            read(1000);
        }
    
public java.lang.Stringsubstring(int beginIndex, int endIndex)

return
a substring

        try
        {
            ensure(endIndex);
            return buff.toString().substring(beginIndex, endIndex);
        }
        catch (IOException e)
        {
            throw new StringIndexOutOfBoundsException(e.getMessage());
        }
    
public java.lang.Stringsubstring(int beginIndex)

return
a substring

        try
        {
            readAll();
            return buff.toString().substring(beginIndex);
        }
        catch (IOException e)
        {
            throw new StringIndexOutOfBoundsException(e.getMessage());
        }