IndentContLineReaderpublic class IndentContLineReader extends ContLineReader Subclass of ContLineReader for lines continued by indentation of
following line (like RFC822 mail, Usenet News, etc.).
Normally you would read header & body of the message(s) with code like:
while ((headerLine = clr.readLine()) != null && headerLine.length() > 0) {
processHeaderLine(headerLine);
}
clr.setContinuationMode(false);
while ((bodyLine = clr.readLine()) != null) {
processBodyLine(bodyLine);
}
|
Fields Summary |
---|
protected String | prevLine | protected static String | sampleTxt |
Constructors Summary |
---|
public IndentContLineReader(Reader in)Construct an IndentContLineReader with the default buffer size.
super(in);
| public IndentContLineReader(Reader in, int sz)Construct an IndentContLineReader using the given buffer size.
super(in, sz);
|
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)
IndentContLineReader is = new IndentContLineReader(
new StringReader(sampleTxt));
String aLine;
// Print Mail/News Header
System.out.println("----- Message Header -----");
while ((aLine = is.readLine()) != null && aLine.length() > 0) {
System.out.println(is.getLineNumber() + ": " + aLine);
}
// Make "is" behave like normal BufferedReader
is.setContinuationMode(false);
System.out.println();
// Print Message Body
System.out.println("----- Message Body -----");
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 '\'s that
mark the end of all but the last.
String s;
// If we saved a previous line, start with it. Else,
// read the first line of possible continuation.
// If non-null, put it into the StringBuffer and its line
// number in firstLineNumber.
if (prevLine != null) {
s = prevLine;
prevLine = null;
}
else {
s = readPhysicalLine();
}
// save the line number of the first line.
firstLineNumber = super.getLineNumber();
// Now we have one line. If we are not in continuation
// mode, or if a previous readPhysicalLine() returned null,
// we are finished, so return it.
if (!doContinue || s == null)
return s;
// Otherwise, start building a stringbuffer
StringBuffer sb = new StringBuffer(s);
// Read as many continued lines as there are, if any.
while (true) {
String nextPart = readPhysicalLine();
if (nextPart == null) {
// Egad! EOF within continued line.
// Return what we have so far.
return sb.toString();
}
// If the next line begins with space, it's continuation
if (nextPart.length() > 0 &&
Character.isWhitespace(nextPart.charAt(0))) {
sb.append(nextPart); // and add line.
} else {
// else we just read too far, so put in "pushback" holder
prevLine = nextPart;
break;
}
}
return sb.toString(); // return what's left
|
|