FileDocCategorySizeDatePackage
TailFilter.javaAPI DocApache Ant 1.707667Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.filters

TailFilter

public final class TailFilter extends BaseParamFilterReader implements ChainableReader
Reads the last n lines of a stream. (Default is last10 lines.) Example:
<tailfilter lines="3"/>
Or:
<filterreader classname="org.apache.tools.ant.filters.TailFilter">
<param name="lines" value="3"/>
</filterreader>

Fields Summary
private static final String
LINES_KEY
Parameter name for the number of lines to be returned.
private static final String
SKIP_KEY
Parameter name for the number of lines to be skipped.
private static final int
DEFAULT_NUM_LINES
Default number of lines to show
private long
lines
Number of lines to be returned in the filtered stream.
private long
skip
Number of lines to be skipped.
private boolean
completedReadAhead
Whether or not read-ahead been completed.
private org.apache.tools.ant.util.LineTokenizer
lineTokenizer
A line tokenizer
private String
line
the current line from the input stream
private int
linePos
the position in the current line
private LinkedList
lineList
Constructors Summary
public TailFilter()
Constructor for "dummy" instances.

see
BaseFilterReader#BaseFilterReader()


               
      
        super();
    
public TailFilter(Reader in)
Creates a new filtered reader.

param
in A Reader object providing the underlying stream. Must not be null.

        super(in);
        lineTokenizer = new LineTokenizer();
        lineTokenizer.setIncludeDelims(true);
    
Methods Summary
public java.io.Readerchain(java.io.Reader rdr)
Creates a new TailFilter using the passed in Reader for instantiation.

param
rdr A Reader object providing the underlying stream. Must not be null.
return
a new filter based on this configuration, but filtering the specified reader

        TailFilter newFilter = new TailFilter(rdr);
        newFilter.setLines(getLines());
        newFilter.setSkip(getSkip());
        newFilter.setInitialized(true);
        return newFilter;
    
private longgetLines()
Returns the number of lines to be returned in the filtered stream.

return
the number of lines to be returned in the filtered stream

        return lines;
    
private longgetSkip()
Returns the number of lines to be skipped in the filtered stream.

return
the number of lines to be skipped in the filtered stream

        return skip;
    
private voidinitialize()
Scans the parameters list for the "lines" parameter and uses it to set the number of lines to be returned in the filtered stream. also scan for "skip" parameter.

        Parameter[] params = getParameters();
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                if (LINES_KEY.equals(params[i].getName())) {
                    setLines(new Long(params[i].getValue()).longValue());
                    continue;
                }
                if (SKIP_KEY.equals(params[i].getName())) {
                    skip = new Long(params[i].getValue()).longValue();
                    continue;
                }
            }
        }
    
public intread()
Returns the next character in the filtered stream. If the read-ahead has been completed, the next character in the buffer is returned. Otherwise, the stream is read to the end and buffered (with the buffer growing as necessary), then the appropriate position in the buffer is set to read from.

return
the next character in the resulting stream, or -1 if the end of the resulting stream has been reached
exception
IOException if the underlying stream throws an IOException during reading

        if (!getInitialized()) {
            initialize();
            setInitialized(true);
        }

        while (line == null || line.length() == 0) {
            line = lineTokenizer.getToken(in);
            line = tailFilter(line);
            if (line == null) {
                return -1;
            }
            linePos = 0;
        }

        int ch = line.charAt(linePos);
        linePos++;
        if (linePos == line.length()) {
            line = null;
        }
        return ch;
    
public voidsetLines(long lines)
Sets the number of lines to be returned in the filtered stream.

param
lines the number of lines to be returned in the filtered stream

        this.lines = lines;
    
public voidsetSkip(long skip)
Sets the number of lines to be skipped in the filtered stream.

param
skip the number of lines to be skipped in the filtered stream

        this.skip = skip;
    
private java.lang.StringtailFilter(java.lang.String line)
implement a tail filter on a stream of lines. line = null is the end of the stream.

return
"" while reading in the lines, line while outputting the lines null at the end of outputting the lines

        if (!completedReadAhead) {
            if (line != null) {
                lineList.add(line);
                if (lines == -1) {
                    if (lineList.size() > skip) {
                        return (String) lineList.removeFirst();
                    }
                } else {
                    long linesToKeep = lines + (skip > 0 ? skip : 0);
                    if (linesToKeep < lineList.size()) {
                        lineList.removeFirst();
                    }
                }
                return "";
            }
            completedReadAhead = true;
            if (skip > 0) {
                for (int i = 0; i < skip; ++i) {
                    lineList.removeLast();
                }
            }
            if (lines > -1) {
                while (lineList.size() > lines) {
                    lineList.removeFirst();
                }
            }
        }
        if (lineList.size() > 0) {
            return (String) lineList.removeFirst();
        }
        return null;