EscapeContLineReaderpublic class EscapeContLineReader extends ContLineReader Subclass of LineNumberReader to allow reading of lines continued
with an escape character. |
Fields Summary |
---|
public static final char | ESCAPEThe default escape character. | protected char | escapeThe actual escape character. | protected boolean | hitEOFEOF flag, needed since we use super.readLine() several places | protected static String | sampleTxt |
Constructors Summary |
---|
public EscapeContLineReader(Reader in)Construct a EscapeContLineReader with the default input-buffer size.
super(in);
| public EscapeContLineReader(Reader in, int sz)Construct a EscapeContLineReader using the given input-buffer size.
super(in, sz);
| public EscapeContLineReader(Reader in, char esc)Construct an EscapeContLineReader given a Reader and a
non-default escape character.
super(in);
escape = esc;
|
Methods Summary |
---|
public int | getLineNumber()Line number of first line in current (possibly continued) line
return firstLineNumber;
| public static void | main(java.lang.String[] argv)
EscapeContLineReader is = new EscapeContLineReader(
new StringReader(sampleTxt));
String aLine;
while ((aLine = is.readLine()) != null) {
System.out.println(is.getLineNumber() + ": " + aLine);
}
is.close();
| public java.lang.String | readLine()Read one (possibly continued) line, stripping out the \ that
marks the end of each line but the last in a sequence.
// Read the first line, save its contents in the StringBuffer
// and its line number in firstLineNumber.
String s = readPhysicalLine();
if (s == null)
hitEOF = true;
if (hitEOF)
return null;
StringBuffer sb = new StringBuffer(s);
firstLineNumber = super.getLineNumber();
// Now read as many continued lines as there are.
while (sb.length() > 0 &&
sb.charAt(sb.length() - 1) == escape) {
sb.setLength(sb.length() - 1); // Kill the escape
// sb.deleteCharAt(sb.length() - 1);// Java 2 - kill it
String nextPart = readPhysicalLine();
if (nextPart == null) {
hitEOF = true;
throw new IOException(
"EOF within continued line at line " +
firstLineNumber);
}
sb.append(' ").append(nextPart); // and add line.
}
return sb.toString();
|
|