FileDocCategorySizeDatePackage
InputHelper.javaAPI DocExample4588Sat Mar 09 14:51:28 GMT 2002ora.ch6

InputHelper

public class InputHelper extends Object

Fields Summary
private static final int
BUFFER_SIZE
private final char[]
buffer
private int
charsLeft
private int
nextChar
private InputStreamReader
reader
Constructors Summary
public InputHelper(InputStream is)


    // Constructs a helper to read a given stream
       
        reader = new InputStreamReader(is);
    
Methods Summary
public voiddispose()

        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ex) {
            }
            reader = null;
        }
    
private booleanfindNext(char c)

        for (;;) {
            if (charsLeft == 0) {
                charsLeft = reader.read(buffer, 0, BUFFER_SIZE);
                if (charsLeft < 0) {
                    return false;
                }
                nextChar = 0;
            }
            charsLeft--;
            if (c == buffer[nextChar++]) {
                return true;
            }
        }
    
public java.lang.StringgatherNumber()

        StringBuffer sb = new StringBuffer();
        boolean gotNumeric = false;
        for (;;) {
            char c = getNext();
            
            // Skip until we find a digit.
            boolean isDigit = Character.isDigit(c);
            if (!gotNumeric && !isDigit) {
                continue;
            }
            gotNumeric = true;
            if (!isDigit) {
                if (c == '." || c == ',") {
                    continue;
                }
                break;
            }
            sb.append(c);
        }
        return sb.toString();
    
private chargetNext()

        if (charsLeft == 0) {
            charsLeft = reader.read(buffer, 0, BUFFER_SIZE);
            if (charsLeft < 0) {
                return (char)0;
            }
            nextChar = 0;
        }
        charsLeft--;
        return buffer[nextChar++];
    
public java.lang.StringBuffergetRestOfLine()

    
        StringBuffer sb = new StringBuffer();
        char c;
        for (;;) {
            c = getNext();
            if (c == '\n" || c == (char)0) {
                break;
            }
            sb.append(c);
        }
        return sb;
    
public booleanmoveAfterString(java.lang.String str)

        char[] chars = str.toCharArray();
        int count = chars.length;
        char firstChar = chars[0];

        char c = (char)0;
        for (;;) {
            if (c != firstChar && !findNext(firstChar)) {
                // Reached the end of the input stream
                return false;
            }

            boolean mismatch = false;
            for (int i = 1; i < count; i++) {
                c = getNext();
                if (c != chars[i]) {
                    mismatch = true;
                    break;
                }
            }

            if (!mismatch) {
                return true;
            }

            // Mismatch. 'c' has the first mismatched
            // character - start the loop again with
            // that character. This is necessary because we
            // could have found "wweb" while looking for "web"
        }