| java.lang.Object com.bostechcorp.cbesb.console.help.FileHelper
FileHelper | public class FileHelper (Code) | | |
getLastEnd | public long getLastEnd()(Code) | | |
getLastFrom | public long getLastFrom()(Code) | | |
setLastEnd | public void setLastEnd(long lastEnd)(Code) | | |
setLastFrom | public void setLastFrom(long lastFrom)(Code) | | |
setLastString | public void setLastString(String lastString)(Code) | | |
tail | public String tail(String fileName, int lineCount, int chunkSize) throws ServerSideException(Code) | | Reads last N lines from the given file. File reading is done in chunks.
Constraints:
1 Minimize the number of file reads -- Avoid reading the complete file
to get last few lines.
2 Minimize the JVM in-memory usage -- Avoid storing the complete file
info in in-memory.
Approach: Read a chunk of characters from end of file. One chunk should
contain multiple lines. Reverse this chunk and extract the lines.
Repeat this until you get required number of last N lines. In this way
we read and store only the required part of the file.
1 Create a RandomAccessFile.
2 Get the position of last character using (i.e length-1). Let this be curPos.
3 Move the cursor to fromPos = (curPos - chunkSize). Use seek().
4 If fromPos is less than or equal to ZERO then go to step-5. Else go to step-6
5 Read characters from beginning of file to curPos. Go to step-9.
6 Read 'chunksize' characters from fromPos.
7 Extract the lines. On reading required N lines go to step-9.
8 Repeat step 3 to 7 until
a. N lines are read.
OR
b. All lines are read when num of lines in file is less than N.
Last line may be a incomplete, so discard it. Modify curPos appropriately.
9 Exit. Got N lines or less than that.
Parameters: fileName - Parameters: lineCount - Parameters: chunkSize - |
|
|