1 18 package org.apache.tools.ant.filters; 19 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import java.util.LinkedList ; 23 import org.apache.tools.ant.types.Parameter; 24 import org.apache.tools.ant.util.LineTokenizer; 25 26 40 public final class TailFilter extends BaseParamFilterReader 41 implements ChainableReader { 42 43 private static final String LINES_KEY = "lines"; 44 45 46 private static final String SKIP_KEY = "skip"; 47 48 49 private static final int DEFAULT_NUM_LINES = 10; 50 51 52 private long lines = DEFAULT_NUM_LINES; 53 54 55 private long skip = 0; 56 57 58 private boolean completedReadAhead = false; 59 60 61 private LineTokenizer lineTokenizer = null; 62 63 64 private String line = null; 65 66 private int linePos = 0; 67 68 private LinkedList lineList = new LinkedList (); 69 70 75 public TailFilter() { 76 super(); 77 } 78 79 85 public TailFilter(final Reader in) { 86 super(in); 87 lineTokenizer = new LineTokenizer(); 88 lineTokenizer.setIncludeDelims(true); 89 } 90 91 104 public int read() throws IOException { 105 if (!getInitialized()) { 106 initialize(); 107 setInitialized(true); 108 } 109 110 while (line == null || line.length() == 0) { 111 line = lineTokenizer.getToken(in); 112 line = tailFilter(line); 113 if (line == null) { 114 return -1; 115 } 116 linePos = 0; 117 } 118 119 int ch = line.charAt(linePos); 120 linePos++; 121 if (linePos == line.length()) { 122 line = null; 123 } 124 return ch; 125 } 126 127 132 public void setLines(final long lines) { 133 this.lines = lines; 134 } 135 136 141 private long getLines() { 142 return lines; 143 } 144 145 150 public void setSkip(final long skip) { 151 this.skip = skip; 152 } 153 154 159 private long getSkip() { 160 return skip; 161 } 162 163 173 public Reader chain(final Reader rdr) { 174 TailFilter newFilter = new TailFilter(rdr); 175 newFilter.setLines(getLines()); 176 newFilter.setSkip(getSkip()); 177 newFilter.setInitialized(true); 178 return newFilter; 179 } 180 181 186 private void initialize() { 187 Parameter[] params = getParameters(); 188 if (params != null) { 189 for (int i = 0; i < params.length; i++) { 190 if (LINES_KEY.equals(params[i].getName())) { 191 setLines(new Long (params[i].getValue()).longValue()); 192 continue; 193 } 194 if (SKIP_KEY.equals(params[i].getName())) { 195 skip = new Long (params[i].getValue()).longValue(); 196 continue; 197 } 198 } 199 } 200 } 201 202 209 private String tailFilter(String line) { 210 if (!completedReadAhead) { 211 if (line != null) { 212 lineList.add(line); 213 if (lines == -1) { 214 if (lineList.size() > skip) { 215 return (String ) lineList.removeFirst(); 216 } 217 } else { 218 long linesToKeep = lines + (skip > 0 ? skip : 0); 219 if (linesToKeep < lineList.size()) { 220 lineList.removeFirst(); 221 } 222 } 223 return ""; 224 } 225 completedReadAhead = true; 226 if (skip > 0) { 227 for (int i = 0; i < skip; ++i) { 228 lineList.removeLast(); 229 } 230 } 231 if (lines > -1) { 232 while (lineList.size() > lines) { 233 lineList.removeFirst(); 234 } 235 } 236 } 237 if (lineList.size() > 0) { 238 return (String ) lineList.removeFirst(); 239 } 240 return null; 241 } 242 } 243 | Popular Tags |