public class StreamParser
extends java.lang.Object
The principle here is that the various read methods change the state of this parser object.
For example, the readWord() method does not return a value, but sets
the value of the workBuffer attribute instead.
| Modifier and Type | Field and Description |
|---|---|
java.lang.String |
commentStart
a line comment indicator: rest of the line after this is to be ignored.
|
char |
curChar
the current character that was read from the stream.
|
char |
firstCommentChar
the first character of the
commentStart string. |
protected java.lang.Exception |
readException
the last exception while reading
|
int |
readState
the current read state.
|
java.io.BufferedInputStream |
stream
the input stream from which to read
|
java.lang.StringBuffer |
workBuffer
a working buffer, used by the readXYZ functions to return the thing that was read
|
| Constructor and Description |
|---|
StreamParser(java.io.InputStream stream)
Constructs the parser with the given stream.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
expect(java.lang.String str)
expect some string.
|
java.lang.Exception |
getReadException()
return the last read exception and clear it.
|
void |
nextChar()
read the next character from the stream.
|
double |
readFloatNumber()
Parse a floating-point number.
|
int |
readIntNumber()
Parse an integer number.
|
void |
readLine()
Read until the end of the line.
|
void |
readSpace()
Everything
<= 32 is considered whitespace. |
void |
readSpaceOnly()
Everything
<= 32 is considered whitespace; this method reads over it. |
void |
readUntil(char ch)
read until the given character occurs.
|
boolean |
readUntilCharOrNewline(char ch)
Read until the given character occurs, or until a Newline (LF, '\n') occurs.
|
void |
readWord()
Read a word, until the next whitespace.
|
void |
trimEnd(java.lang.StringBuffer buf)
remove whitespace from the end of the given StringBuffer
|
public java.io.BufferedInputStream stream
protected java.lang.Exception readException
public int readState
>= 0 is ok, -1 is EOF and -2 is error.public char curChar
public java.lang.StringBuffer workBuffer
public java.lang.String commentStart
firstCommentChar if you change this.
By default, line comments start with "#".public char firstCommentChar
commentStart string.public StreamParser(java.io.InputStream stream)
stream - an input streampublic void nextChar()
readException variable,
or a code >= 0 for OK.public java.lang.Exception getReadException()
public void trimEnd(java.lang.StringBuffer buf)
public void readSpaceOnly()
<= 32 is considered whitespace; this method reads over it.
Doesn't read over comments (contrary to former readSpace behaviour).
Uses nextChar().
Note that this function does not change the work buffer.public void readSpace()
<= 32 is considered whitespace.
Also reads over comments that start with commentStart and go until the end
of the line.
Uses nextChar().
Note that this function does not change the work buffer.public boolean expect(java.lang.String str)
public void readWord()
public void readLine()
public void readUntil(char ch)
public boolean readUntilCharOrNewline(char ch)
NOTE: This doesn't do exactly the same thing as readLine() - The curChar pointer remains at the final character (the given char, or NL). readLine, on the other hand, also eats the NL and possibly a following CR.
ch - The Char to read until.public int readIntNumber()
workBuffer, and also returns it.java.lang.NumberFormatException - if the parsed number is not a valid integer.public double readFloatNumber()
workBuffer.java.lang.NumberFormatException - if the parsed number is not a valid double.