FileDocCategorySizeDatePackage
HeadFilter.javaAPI DocApache Ant 1.706553Wed Dec 13 06:16:22 GMT 2006org.apache.tools.ant.filters

HeadFilter

public final class HeadFilter extends BaseParamFilterReader implements ChainableReader
Reads the first n lines of a stream. (Default is first 10 lines.)

Example:

<headfilter lines="3"/>
Or:
<filterreader classname="org.apache.tools.ant.filters.HeadFilter">
<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 long
linesRead
Number of lines currently read in.
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 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
Constructors Summary
public HeadFilter()
Constructor for "dummy" instances.

see
BaseFilterReader#BaseFilterReader()


               
      
        super();
    
public HeadFilter(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 HeadFilter 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

        HeadFilter newFilter = new HeadFilter(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 java.lang.StringheadFilter(java.lang.String line)
implements a head filter on the input stream

        linesRead++;
        if (skip > 0) {
            if ((linesRead - 1) < skip) {
                return null;
            }
        }

        if (lines > 0) {
            if (linesRead > (lines + skip)) {
                return null;
            }
        }
        return line;
    
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())) {
                    lines = 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 desired number of lines have already been read, the resulting stream is effectively at an end. Otherwise, the next character from the underlying stream is read and returned.

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);
            if (line == null) {
                return -1;
            }
            line = headFilter(line);
            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;