Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 236   Methods: 7
NCLOC: 92   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TailFilter.java 65.4% 78.3% 100% 75.9%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 package org.apache.tools.ant.filters;
 55   
 
 56   
 import java.io.IOException;
 57   
 import java.io.Reader;
 58   
 import org.apache.tools.ant.types.Parameter;
 59   
 
 60   
 /**
 61   
  * Reads the last <code>n</code> lines of a stream. (Default is last10 lines.)
 62   
  *
 63   
  * Example:
 64   
  *
 65   
  * <pre>&lt;tailfilter lines=&quot;3&quot;/&gt;</pre>
 66   
  *
 67   
  * Or:
 68   
  *
 69   
  * <pre>&lt;filterreader classname=&quot;org.apache.tools.ant.filters.TailFilter&quot;&gt;
 70   
  *   &lt;param name=&quot;lines&quot; value=&quot;3&quot;/&gt;
 71   
  * &lt;/filterreader&gt;</pre>
 72   
  *
 73   
  * @author Magesh Umasankar
 74   
  */
 75   
 public final class TailFilter
 76   
     extends BaseParamFilterReader
 77   
     implements ChainableReader {
 78   
     /** Parameter name for the number of lines to be returned. */
 79   
     private static final String LINES_KEY = "lines";
 80   
 
 81   
     /** Number of lines currently read in. */
 82   
     private long linesRead = 0;
 83   
 
 84   
     /** Number of lines to be returned in the filtered stream. */
 85   
     private long lines = 10;
 86   
 
 87   
     /** Buffer to hold in characters read ahead. */
 88   
     private char[] buffer = new char[4096];
 89   
 
 90   
     /** The character position that has been returned from the buffer. */
 91   
     private int returnedCharPos = -1;
 92   
 
 93   
     /** Whether or not read-ahead been completed. */
 94   
     private boolean completedReadAhead = false;
 95   
 
 96   
     /** Current index position on the buffer. */
 97   
     private int bufferPos = 0;
 98   
 
 99   
     /**
 100   
      * Constructor for "dummy" instances.
 101   
      * 
 102   
      * @see BaseFilterReader#BaseFilterReader()
 103   
      */
 104  1
     public TailFilter() {
 105  1
         super();
 106   
     }
 107   
 
 108   
     /**
 109   
      * Creates a new filtered reader.
 110   
      *
 111   
      * @param in A Reader object providing the underlying stream.
 112   
      *           Must not be <code>null</code>.
 113   
      */
 114  2
     public TailFilter(final Reader in) {
 115  2
         super(in);
 116   
     }
 117   
 
 118   
     /**
 119   
      * Returns the next character in the filtered stream. If the read-ahead
 120   
      * has been completed, the next character in the buffer is returned.
 121   
      * Otherwise, the stream is read to the end and buffered (with the buffer
 122   
      * growing as necessary), then the appropriate position in the buffer is
 123   
      * set to read from.
 124   
      * 
 125   
      * @return the next character in the resulting stream, or -1
 126   
      * if the end of the resulting stream has been reached
 127   
      * 
 128   
      * @exception IOException if the underlying stream throws an IOException
 129   
      * during reading     
 130   
      */
 131  36
     public final int read() throws IOException {
 132  36
         if (!getInitialized()) {
 133  1
             initialize();
 134  1
             setInitialized(true);
 135   
         }
 136   
 
 137  36
         if (!completedReadAhead) {
 138  2
             int ch = -1;
 139  ?
             while ((ch = in.read()) != -1) {
 140  202
                 if (buffer.length == bufferPos) {
 141  0
                     if (returnedCharPos != -1) {
 142  0
                         final char[] tmpBuffer = new char[buffer.length];
 143  0
                         System.arraycopy(buffer, returnedCharPos + 1, tmpBuffer,
 144   
                                          0, buffer.length - (returnedCharPos + 1));
 145  0
                         buffer = tmpBuffer;
 146  0
                         bufferPos = bufferPos - (returnedCharPos + 1);
 147  0
                         returnedCharPos = -1;
 148   
                     } else {
 149  0
                         final char[] tmpBuffer = new char[buffer.length * 2];
 150  0
                         System.arraycopy(buffer, 0, tmpBuffer, 0, bufferPos);
 151  0
                         buffer = tmpBuffer;
 152   
                     }
 153   
                 }
 154   
 
 155  202
                 if (ch == '\n' || ch == -1) {
 156  12
                     ++linesRead;
 157   
 
 158  12
                     if (linesRead == lines) {
 159  11
                         int i = 0;
 160  11
                         for (i = returnedCharPos + 1;
 161  171
                             buffer[i] != 0 && buffer[i] != '\n'; i++) {
 162   
                         }
 163  11
                         returnedCharPos = i;
 164  11
                         --linesRead;
 165   
                     }
 166   
                 }
 167  202
                 if (ch == -1) {
 168  0
                     break;
 169   
                 }
 170   
 
 171  202
                 buffer[bufferPos] = (char) ch;
 172  202
                 bufferPos++;
 173   
             }
 174  2
             completedReadAhead = true;
 175   
         }
 176   
 
 177  36
         ++returnedCharPos;
 178  36
         if (returnedCharPos >= bufferPos) {
 179  5
             return -1;
 180   
         } else {
 181  31
             return buffer[returnedCharPos];
 182   
         }
 183   
     }
 184   
 
 185   
     /**
 186   
      * Sets the number of lines to be returned in the filtered stream.
 187   
      * 
 188   
      * @param lines the number of lines to be returned in the filtered stream
 189   
      */
 190  3
     public final void setLines(final long lines) {
 191  3
         this.lines = lines;
 192   
     }
 193   
 
 194   
     /**
 195   
      * Returns the number of lines to be returned in the filtered stream.
 196   
      * 
 197   
      * @return the number of lines to be returned in the filtered stream
 198   
      */
 199  1
     private final long getLines() {
 200  1
         return lines;
 201   
     }
 202   
 
 203   
     /**
 204   
      * Creates a new TailFilter using the passed in
 205   
      * Reader for instantiation.
 206   
      * 
 207   
      * @param rdr A Reader object providing the underlying stream.
 208   
      *            Must not be <code>null</code>.
 209   
      * 
 210   
      * @return a new filter based on this configuration, but filtering
 211   
      *         the specified reader
 212   
      */
 213  1
     public final Reader chain(final Reader rdr) {
 214  1
         TailFilter newFilter = new TailFilter(rdr);
 215  1
         newFilter.setLines(getLines());
 216  1
         newFilter.setInitialized(true);
 217  1
         return newFilter;
 218   
     }
 219   
 
 220   
     /**
 221   
      * Scans the parameters list for the "lines" parameter and uses
 222   
      * it to set the number of lines to be returned in the filtered stream.
 223   
      */
 224  1
     private final void initialize() {
 225  1
         Parameter[] params = getParameters();
 226  1
         if (params != null) {
 227  1
             for (int i = 0; i < params.length; i++) {
 228  1
                 if (LINES_KEY.equals(params[i].getName())) {
 229  1
                     setLines(new Long(params[i].getValue()).longValue());
 230  1
                     break;
 231   
                 }
 232   
             }
 233   
         }
 234   
     }
 235   
 }
 236