LineReaderpublic class LineReader extends InputStreamReader Reader that knows how to read non-blank lines. Line may be concatenated
according to section 2.1.3 of the vCard 2.1 specification; CRLF followed by
an LWSP character is treated as only the LWSP character.
The line terminator is taken as CRLF. |
Fields Summary |
---|
private final InputStream | inInput stream. | private final Matcher | matcherMatcher function. |
Constructors Summary |
---|
public LineReader(InputStream in, String encoding, Matcher matcher)Constructs a line reader handler.
this(new MarkableInputStream(in), encoding, matcher);
| private LineReader(MarkableInputStream in, String encoding, Matcher matcher)Constructs a line reader.
super(in, encoding);
this.in = in;
this.matcher = matcher;
|
Methods Summary |
---|
public void | mark(int lookahead)Sets marker in input stream.
in.mark(lookahead);
| public boolean | markSupported()Checks if mark is supported.
return true;
| public java.lang.String | readLine()Reads a non-blank line.
StringBuffer sb = new StringBuffer();
boolean lineIsOnlyWhiteSpace = true;
boolean done = false;
for (int i = read(); i != -1 && !done; ) {
switch (i) {
case '\r": {
// start of a new line. follow through and see if
// it is really a new line
i = read();
if (i != '\n") {
throw new IOException("Bad line terminator");
}
// fall through
}
// be generous and accept '\n' alone as a new line.
// this is against the vCard/vCalendar specifications, but
// appears to be common practice.
case '\n": {
// lines with only whitespace are treated as empty lines
if (lineIsOnlyWhiteSpace) {
sb.setLength(0);
}
mark(1);
i = read();
reset();
switch (i) {
case ' ":
case '\t":
// append this line to the previous one
skip(1);
break;
default:
// end of the line
if (!lineIsOnlyWhiteSpace) {
// return this line
done = true;
} else {
skip(1);
// read another line and hope it contains
// more than white space
}
break;
}
break;
}
case ' ":
case '\t":
sb.append((char) i);
i = read();
break;
default:
sb.append((char) i);
if (matcher.match(sb)) {
return sb.toString().trim();
}
i = read();
lineIsOnlyWhiteSpace = false;
}
}
if (lineIsOnlyWhiteSpace) {
return null;
} else {
return sb.toString().trim();
}
| public void | reset()Reset the line markers.
in.reset();
|
|