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

StreamCharacterIterator

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

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 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());
        }