FileDocCategorySizeDatePackage
EscapeContLineReader.javaAPI DocExample2781Sat Nov 25 12:55:28 GMT 2000None

EscapeContLineReader

public class EscapeContLineReader extends ContLineReader
Subclass of LineNumberReader to allow reading of lines continued with an escape character.
author
Ian Darwin, ian@darwinsys.com
version
$Id: EscapeContLineReader.java,v 1.6 2000/11/25 17:55:28 ian Exp $

Fields Summary
public static final char
ESCAPE
The default escape character.
protected char
escape
The actual escape character.
protected boolean
hitEOF
EOF 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 intgetLineNumber()
Line number of first line in current (possibly continued) line


	           
	   
		return firstLineNumber;
	
public static voidmain(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.StringreadLine()
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();