1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/* ------------------------------------ JavaOnTracks Thibaut Colar tcolar-jot AT colar DOT net Artistic Licence 2.0 http://www.javaontracks.net ------------------------------------ */ package net.jot.logger; import java.io.RandomAccessFile; import java.util.Vector; /** * This is used to "tail" a file (read it from the bottom) * this is done tro be fast and not use much memory. * This is to be used together with the TailFilter interface * The tailfilter interface let you tail only matching lines * so this function let you do: * A Tail, a Grep, or a combianison of both (Tail | Grep) * * *@author tcolar *@created May 6, 2003 */ public class JOTFileTailer { byte[] buffer; int bufferLength; int newLine; int startLength; int bytesToRead; int curPos; RandomAccessFile logFile = null; /** *Constructor for the FileTailer object */ public JOTFileTailer() { this(1000000, Character.LINE_SEPARATOR); } /** *Constructor for the FileTailer object * *@param length Description of Parameter */ public JOTFileTailer(int length) { this(length, Character.LINE_SEPARATOR); } /** *Constructor for the FileTailer object * *@param separator Description of Parameter *@param length Description of Parameter */ public JOTFileTailer(int length, int separator) { bufferLength = length; newLine = separator; } /** * This is to be called to set the file to tail. * *@param file Description of Parameter *@exception Exception Description of Exception */ public void loadFile(String file) throws Exception { if (file != null && file.length() > 0) { logFile = new RandomAccessFile(file, "r"); } } /** * This does the job ! * *@param max Description of Parameter *@param filter Description of Parameter *@return Description of the Returned Value *@exception Exception Description of Exception */ public Vector tail(int max, JOTTailFilter filter) throws Exception { int startLength = (int) logFile.length(); int bytesToRead = bufferLength; int curPos = startLength; Vector entries = new Vector(); buffer = new byte[bufferLength]; String remains = ""; if (logFile == null) { throw new Exception("You must load a file first !!"); } while (curPos > 0 && entries.size() <= max) { curPos -= bufferLength; if (curPos < 0) { bytesToRead += curPos; curPos = 0; } logFile.seek(curPos); int nbBytes = logFile.read(buffer, 0, bytesToRead); // next end of line if (nbBytes > 0) { // index of the end of the previous line int lineEnd = nbBytes - 1; //scanning the buffer backward for (int i = nbBytes - 1; i > -1 && entries.size() <= max; i--) { if (buffer[i] == newLine) { String str = ""; for (int cpt = i + 1; cpt < lineEnd; cpt++) { // reading this line str += (char) buffer[cpt]; } str += remains; if (filter.acceptLine(str)) { /* * if accepted * by the filter, adding the line */ entries.add(str); } // marking the end of the previous line lineEnd = i; } } remains = ""; if (entries.size() <= max) { /* * keeping the remains of this buffer * (line broken on two buffer) for * next round. */ for (int cpt = 0; cpt != lineEnd; cpt++) { // reading this line remains += (char) buffer[cpt]; } } } } /* * For the very first line of the file (doesn't have a * carriage return preceding it) */ if (filter.acceptLine(remains)) { entries.add(remains); } // relese buffer to garbage collector for sure. buffer = null; return entries; } }