org.archive.crawler.util
Class LogReader

java.lang.Object
  extended by org.archive.crawler.util.LogReader

public class LogReader
extends java.lang.Object

This class contains a variety of methods for reading log files (or other text files containing repeated lines with similar information).

All methods are static.

Author:
Kristinn Sigurdsson

Constructor Summary
LogReader()
           
 
Method Summary
static java.lang.String buildDisplayingHeader(int len, long logsize)
           
static int findFirstLineBeginning(java.io.InputStreamReader reader, java.lang.String prefix)
          Return the line number of the first line in the log/file that that begins with the given string.
static int findFirstLineBeginningFromSeries(java.lang.String aFileName, java.lang.String prefix)
          Return the line number of the first line in the log/file that begins with the given string.
static int findFirstLineContaining(java.io.InputStreamReader reader, java.lang.String regExpr)
          Return the line number of the first line in the log/file that matches a given regular expression.
static int findFirstLineContaining(java.lang.String aFileName, java.lang.String regExpr)
          Return the line number of the first line in the log/file that matches a given regular expression.
static int findFirstLineContainingFromSeries(java.lang.String aFileName, java.lang.String regExpr)
          Return the line number of the first line in the log/file that matches a given regular expression.
static java.lang.String get(java.io.InputStreamReader reader)
          Reads entire contents of reader, returns as string.
static java.lang.String[] get(java.io.InputStreamReader reader, int lineNumber, int n, long logsize)
          Gets a portion of a log file.
static java.lang.String get(java.lang.String aFileName)
          Returns the entire file.
static java.lang.String[] get(java.lang.String aFileName, int lineNumber, int n)
          Gets a portion of a log file.
static java.lang.String[] getByRegExpr(java.io.InputStreamReader reader, java.lang.String regExpr, int addLines, boolean prependLineNumbers, int skipFirstMatches, int numberOfMatches, long logsize)
          Returns all lines in a log/file matching a given regular expression.
static java.lang.String[] getByRegExpr(java.io.InputStreamReader reader, java.lang.String regExpr, java.lang.String addLines, boolean prependLineNumbers, int skipFirstMatches, int numberOfMatches, long logsize)
          Returns all lines in a log/file matching a given regular expression.
static java.lang.String[] getByRegExpr(java.lang.String aFileName, java.lang.String regExpr, int addLines, boolean prependLineNumbers, int skipFirstMatches, int numberOfMatches)
          Returns all lines in a log/file matching a given regular expression.
static java.lang.String[] getByRegExpr(java.lang.String aFileName, java.lang.String regExpr, java.lang.String addLines, boolean prependLineNumbers, int skipFirstMatches, int numberOfMatches)
          Returns all lines in a log/file matching a given regular expression.
static java.lang.String[] getByRegExprFromSeries(java.lang.String aFileName, java.lang.String regExpr, int addLines, boolean prependLineNumbers, int skipFirstMatches, int numberOfMatches)
          Returns all lines in a log/file matching a given regular expression.
static java.lang.String[] getByRegExprFromSeries(java.lang.String aFileName, java.lang.String regExpr, java.lang.String addLines, boolean prependLineNumbers, int skipFirstMatches, int numberOfMatches)
          Returns all lines in a log/file matching a given regular expression.
static java.lang.String[] getFromSeries(java.lang.String aFileName, int lineNumber, int n)
          Gets a portion of a log spread across a numbered series of files.
static java.lang.String[] tail(java.io.RandomAccessFile raf, int n)
          Implementation of a unix-like 'tail -n' command
static java.lang.String[] tail(java.lang.String aFileName)
          Implementation of a unix-like 'tail' command
static java.lang.String[] tail(java.lang.String aFileName, int n)
          Implementation of a unix-like 'tail -n' command
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LogReader

public LogReader()
Method Detail

get

public static java.lang.String get(java.lang.String aFileName)
Returns the entire file. Useful for smaller files.

Parameters:
aFileName - a file name
Returns:
The String representation of the entire file. Null is returned if errors occur (file not found or io exception)

get

public static java.lang.String get(java.io.InputStreamReader reader)
Reads entire contents of reader, returns as string.

Parameters:
reader -
Returns:
String of entire contents; null for any error.

get

public static java.lang.String[] get(java.lang.String aFileName,
                                     int lineNumber,
                                     int n)
Gets a portion of a log file. Starting at a given line number and the n-1 lines following that one or until the end of the log if that is reached first.

Parameters:
aFileName - The filename of the log/file
lineNumber - The number of the first line to get (if larger then the file an empty string will be returned)
n - How many lines to return (total, including the one indicated by lineNumber). If smaller then 1 then an empty string will be returned.
Returns:
An array of two strings is returned. At index 0 a portion of the file starting at lineNumber and reaching lineNumber+n is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception)

getFromSeries

public static java.lang.String[] getFromSeries(java.lang.String aFileName,
                                               int lineNumber,
                                               int n)
Gets a portion of a log spread across a numbered series of files. Starting at a given line number and the n-1 lines following that one or until the end of the log if that is reached first.

Parameters:
aFileName - The filename of the log/file
lineNumber - The number of the first line to get (if larger then the file an empty string will be returned)
n - How many lines to return (total, including the one indicated by lineNumber). If smaller then 1 then an empty string will be returned.
Returns:
An array of two strings is returned. At index 0 a portion of the file starting at lineNumber and reaching lineNumber+n is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception)

buildDisplayingHeader

public static java.lang.String buildDisplayingHeader(int len,
                                                     long logsize)

get

public static java.lang.String[] get(java.io.InputStreamReader reader,
                                     int lineNumber,
                                     int n,
                                     long logsize)
Gets a portion of a log file. Starting at a given line number and the n-1 lines following that one or until the end of the log if that is reached first.

Parameters:
reader - source to scan for lines
lineNumber - The number of the first line to get (if larger then the file an empty string will be returned)
n - How many lines to return (total, including the one indicated by lineNumber). If smaller then 1 then an empty string will be returned.
logsize - total size of source
Returns:
An array of two strings is returned. At index 0 a portion of the file starting at lineNumber and reaching lineNumber+n is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception)

findFirstLineContaining

public static int findFirstLineContaining(java.lang.String aFileName,
                                          java.lang.String regExpr)
Return the line number of the first line in the log/file that matches a given regular expression.

Parameters:
aFileName - The filename of the log/file
regExpr - The regular expression that is to be used
Returns:
The line number (counting from 1, not zero) of the first line that matches the given regular expression. -1 is returned if no line matches the regular expression. -1 also is returned if errors occur (file not found, io exception etc.)

findFirstLineBeginningFromSeries

public static int findFirstLineBeginningFromSeries(java.lang.String aFileName,
                                                   java.lang.String prefix)
Return the line number of the first line in the log/file that begins with the given string.

Parameters:
aFileName - The filename of the log/file
prefix - The prefix string to match
Returns:
The line number (counting from 1, not zero) of the first line that matches the given regular expression. -1 is returned if no line matches the regular expression. -1 also is returned if errors occur (file not found, io exception etc.)

findFirstLineBeginning

public static int findFirstLineBeginning(java.io.InputStreamReader reader,
                                         java.lang.String prefix)
Return the line number of the first line in the log/file that that begins with the given string.

Parameters:
reader - The reader of the log/file
prefix - The prefix string to match
Returns:
The line number (counting from 1, not zero) of the first line that matches the given regular expression. -1 is returned if no line matches the regular expression. -1 also is returned if errors occur (file not found, io exception etc.)

findFirstLineContainingFromSeries

public static int findFirstLineContainingFromSeries(java.lang.String aFileName,
                                                    java.lang.String regExpr)
Return the line number of the first line in the log/file that matches a given regular expression.

Parameters:
aFileName - The filename of the log/file
regExpr - The regular expression that is to be used
Returns:
The line number (counting from 1, not zero) of the first line that matches the given regular expression. -1 is returned if no line matches the regular expression. -1 also is returned if errors occur (file not found, io exception etc.)

findFirstLineContaining

public static int findFirstLineContaining(java.io.InputStreamReader reader,
                                          java.lang.String regExpr)
Return the line number of the first line in the log/file that matches a given regular expression.

Parameters:
reader - The reader of the log/file
regExpr - The regular expression that is to be used
Returns:
The line number (counting from 1, not zero) of the first line that matches the given regular expression. -1 is returned if no line matches the regular expression. -1 also is returned if errors occur (file not found, io exception etc.)

getByRegExpr

public static java.lang.String[] getByRegExpr(java.lang.String aFileName,
                                              java.lang.String regExpr,
                                              int addLines,
                                              boolean prependLineNumbers,
                                              int skipFirstMatches,
                                              int numberOfMatches)
Returns all lines in a log/file matching a given regular expression. Possible to get lines immediately following the matched line. Also possible to have each line prepended by it's line number.

Parameters:
aFileName - The filename of the log/file
regExpr - The regular expression that is to be used
addLines - How many lines (in addition to the matched line) to add. A value less then 1 will mean that only the matched line will be included. If another matched line is hit before we reach this limit it will be included and this counter effectively reset for it.
prependLineNumbers - If true, then each line will be prepended by it's line number in the file.
skipFirstMatches - The first number of matches up to this value will be skipped over.
numberOfMatches - Once past matches that are to be skipped this many matches will be added to the return value. A value of 0 will cause all matching lines to be included.
Returns:
An array of two strings is returned. At index 0 tall lines in a log/file matching a given regular expression is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception) If a PatternSyntaxException occurs, it's error message will be returned and the informational string will be empty (not null).

getByRegExprFromSeries

public static java.lang.String[] getByRegExprFromSeries(java.lang.String aFileName,
                                                        java.lang.String regExpr,
                                                        int addLines,
                                                        boolean prependLineNumbers,
                                                        int skipFirstMatches,
                                                        int numberOfMatches)
Returns all lines in a log/file matching a given regular expression. Possible to get lines immediately following the matched line. Also possible to have each line prepended by it's line number.

Parameters:
aFileName - The filename of the log/file
regExpr - The regular expression that is to be used
addLines - How many lines (in addition to the matched line) to add. A value less then 1 will mean that only the matched line will be included. If another matched line is hit before we reach this limit it will be included and this counter effectively reset for it.
prependLineNumbers - If true, then each line will be prepended by it's line number in the file.
skipFirstMatches - The first number of matches up to this value will be skipped over.
numberOfMatches - Once past matches that are to be skipped this many matches will be added to the return value. A value of 0 will cause all matching lines to be included.
Returns:
An array of two strings is returned. At index 0 tall lines in a log/file matching a given regular expression is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception) If a PatternSyntaxException occurs, it's error message will be returned and the informational string will be empty (not null).

getByRegExpr

public static java.lang.String[] getByRegExpr(java.io.InputStreamReader reader,
                                              java.lang.String regExpr,
                                              int addLines,
                                              boolean prependLineNumbers,
                                              int skipFirstMatches,
                                              int numberOfMatches,
                                              long logsize)
Returns all lines in a log/file matching a given regular expression. Possible to get lines immediately following the matched line. Also possible to have each line prepended by it's line number.

Parameters:
reader - The reader of the log/file
regExpr - The regular expression that is to be used
addLines - How many lines (in addition to the matched line) to add. A value less then 1 will mean that only the matched line will be included. If another matched line is hit before we reach this limit it will be included and this counter effectively reset for it.
prependLineNumbers - If true, then each line will be prepended by it's line number in the file.
skipFirstMatches - The first number of matches up to this value will be skipped over.
numberOfMatches - Once past matches that are to be skipped this many matches will be added to the return value. A value of 0 will cause all matching lines to be included.
logsize - Size of the log in bytes
Returns:
An array of two strings is returned. At index 0 all lines in a log/file matching a given regular expression is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception) If a PatternSyntaxException occurs, it's error message will be returned and the informational string will be empty (not null).

getByRegExpr

public static java.lang.String[] getByRegExpr(java.lang.String aFileName,
                                              java.lang.String regExpr,
                                              java.lang.String addLines,
                                              boolean prependLineNumbers,
                                              int skipFirstMatches,
                                              int numberOfMatches)
Returns all lines in a log/file matching a given regular expression. Possible to get lines immediately following the matched line. Also possible to have each line prepended by it's line number.

Parameters:
aFileName - The filename of the log/file
regExpr - The regular expression that is to be used
addLines - Any lines following a match that begin with this string will also be included. We will stop including new lines once we hit the first that does not match.
prependLineNumbers - If true, then each line will be prepended by it's line number in the file.
skipFirstMatches - The first number of matches up to this value will be skipped over.
numberOfMatches - Once past matches that are to be skipped this many matches will be added to the return value. A value of 0 will cause all matching lines to be included.
Returns:
An array of two strings is returned. At index 0 tall lines in a log/file matching a given regular expression is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception) If a PatternSyntaxException occurs, it's error message will be returned and the informational string will be empty (not null).

getByRegExprFromSeries

public static java.lang.String[] getByRegExprFromSeries(java.lang.String aFileName,
                                                        java.lang.String regExpr,
                                                        java.lang.String addLines,
                                                        boolean prependLineNumbers,
                                                        int skipFirstMatches,
                                                        int numberOfMatches)
Returns all lines in a log/file matching a given regular expression. Possible to get lines immediately following the matched line. Also possible to have each line prepended by it's line number.

Parameters:
aFileName - The filename of the log/file
regExpr - The regular expression that is to be used
addLines - Any lines following a match that begin with this string will also be included. We will stop including new lines once we hit the first that does not match.
prependLineNumbers - If true, then each line will be prepended by it's line number in the file.
skipFirstMatches - The first number of matches up to this value will be skipped over.
numberOfMatches - Once past matches that are to be skipped this many matches will be added to the return value. A value of 0 will cause all matching lines to be included.
Returns:
An array of two strings is returned. At index 0 tall lines in a log/file matching a given regular expression is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception) If a PatternSyntaxException occurs, it's error message will be returned and the informational string will be empty (not null).

getByRegExpr

public static java.lang.String[] getByRegExpr(java.io.InputStreamReader reader,
                                              java.lang.String regExpr,
                                              java.lang.String addLines,
                                              boolean prependLineNumbers,
                                              int skipFirstMatches,
                                              int numberOfMatches,
                                              long logsize)
Returns all lines in a log/file matching a given regular expression. Possible to get lines immediately following the matched line. Also possible to have each line prepended by it's line number.

Parameters:
reader - The reader of the log/file
regExpr - The regular expression that is to be used
addLines - Any lines following a match that begin with this string will also be included. We will stop including new lines once we hit the first that does not match.
prependLineNumbers - If true, then each line will be prepended by it's line number in the file.
skipFirstMatches - The first number of matches up to this value will be skipped over.
numberOfMatches - Once past matches that are to be skipped this many matches will be added to the return value. A value of 0 will cause all matching lines to be included.
logsize - Size of the log in bytes
Returns:
An array of two strings is returned. At index 0 tall lines in a log/file matching a given regular expression is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception) If a PatternSyntaxException occurs, it's error message will be returned and the informational string will be empty (not null).

tail

public static java.lang.String[] tail(java.lang.String aFileName)
Implementation of a unix-like 'tail' command

Parameters:
aFileName - a file name String
Returns:
An array of two strings is returned. At index 0 the String representation of at most 10 last lines is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception)

tail

public static java.lang.String[] tail(java.lang.String aFileName,
                                      int n)
Implementation of a unix-like 'tail -n' command

Parameters:
aFileName - a file name String
n - int number of lines to be returned
Returns:
An array of two strings is returned. At index 0 the String representation of at most n last lines is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception)

tail

public static java.lang.String[] tail(java.io.RandomAccessFile raf,
                                      int n)
Implementation of a unix-like 'tail -n' command

Parameters:
raf - a RandomAccessFile to tail
n - int number of lines to be returned
Returns:
An array of two strings is returned. At index 0 the String representation of at most n last lines is located. At index 1 there is an informational string about how large a segment of the file is being returned. Null is returned if errors occur (file not found or io exception)


Copyright © 2003-2011 Internet Archive. All Rights Reserved.