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

StreamCharacterIterator

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

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

param
is an InputStream, which is parsed

        this.is = is;
        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;
        }

        int c;
        int i = n;
        while (--i >= 0)
        {
            c = is.read();
            if (c < 0) // EOF
            {
                closed = true;
                break;
            }
            buff.append((char) c);
        }
        return n - i;
    
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());
        }