FileDocCategorySizeDatePackage
ReaderCharacterIterator.javaAPI DocJava SE 5 API5849Fri Aug 26 14:55:28 BST 2005com.sun.org.apache.regexp.internal

ReaderCharacterIterator

public final class ReaderCharacterIterator extends Object implements CharacterIterator
Encapsulates InputStream, ...
author
Ales Novak

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

param
is an 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 offset, int length)

return
a substring

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

return
a substring

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