ghm.follow
Class FileFollower

java.lang.Object
  extended by ghm.follow.FileFollower

public class FileFollower
extends java.lang.Object

Instances of this class 'follow' a particular text file, assmebling that file's characters into Strings and sending them to instances of OutputDestination. The name and behavior of this class are inspired by the '-f' (follow) flag of the UNIX command 'tail'.

Author:
Greg Merrill
See Also:
OutputDestination

Field Summary
protected  int bufferSize
           
protected  boolean continueRunning
           
protected  java.io.File file
           
protected  int latency
           
protected static java.lang.String lineSeparator
          Line separator, retrieved from System properties & stored statically.
protected  boolean needsRestart
           
protected  java.util.List<OutputDestination> outputDestinations
           
protected  boolean paused
           
protected  java.lang.Thread runnerThread
           
 
Constructor Summary
FileFollower(java.io.File file, int bufferSize, int latency, OutputDestination[] initialOutputDestinations)
          Constructs a new FileFollower; invoking this constructor does not cause the new object to begin following the supplied file.
FileFollower(java.io.File file, OutputDestination[] initialOutputDestinations)
          Identical to FileFollower(File, int, int, OutputDestination[]), except that a default buffer size (32,768 characters) and latency (1000 milliseconds) are used.
 
Method Summary
 boolean addOutputDestination(OutputDestination outputDestination)
          Add another OutputDestination to which the followed file's contents should be printed.
 int getBufferSize()
          Returns the size of the character buffer used to read characters from the followed file.
 java.io.File getFollowedFile()
          Returns the file which is being followed by this FileFollower
 int getLatency()
          Returns the time (in milliseconds) which a FileFollower spends sleeping each time it encounters the end of the followed file.
 java.util.List<OutputDestination> getOutputDestinations()
          Returns the List which maintains all OutputDestinations for this FileFollower.
 boolean isBeingFollowed()
          Returns the following state of a file
 boolean isPaused()
          Returns the pause state of the follower.
 void pause()
           
 boolean removeOutputDestination(OutputDestination outputDestination)
          Remove the supplied OutputDestination from the list of OutputDestinations to which the followed file's contents should be printed.
 void restart()
           
 void setBufferSize(int bufferSize)
          Sets the size of the character buffer used to read characters from the followed file.
 void setLatency(int latency)
          Sets the time (in milliseconds) which a FileFollower spends sleeping each time it encounters the end of the followed file.
 void start()
          Cause this FileFollower to spawn a thread which will follow the file supplied in the constructor and send its contents to all of the FileFollower's OutputDestinations.

If this FileFollower is running but paused, this method equates to calling unpause().
 void stop()
          Cause this FileFollower to stop following the file supplied in the constructor after it flushes the characters it's currently reading to all its OutputDestinations.
 void stopAndWait()
          Like stop(), but this method will not exit until the thread which is following the file has finished executing (i.e., stop synchronously).
 void unpause()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bufferSize

protected int bufferSize

latency

protected int latency

file

protected java.io.File file

outputDestinations

protected java.util.List<OutputDestination> outputDestinations

continueRunning

protected boolean continueRunning

needsRestart

protected boolean needsRestart

runnerThread

protected java.lang.Thread runnerThread

paused

protected boolean paused

lineSeparator

protected static final java.lang.String lineSeparator
Line separator, retrieved from System properties & stored statically.

Constructor Detail

FileFollower

public FileFollower(java.io.File file,
                    int bufferSize,
                    int latency,
                    OutputDestination[] initialOutputDestinations)
Constructs a new FileFollower; invoking this constructor does not cause the new object to begin following the supplied file. In order to begin following, one must call start().

Parameters:
file - file to be followed
bufferSize - number of chars to be read each time the file is accessed
latency - each time a FileFollower's running thread encounters the end of the file in its stream, it will rest for this many milliseconds before checking to see if there are any more bytes in the file
initialOutputDestinations - an initial array of OutputDestinations which will be used when printing the contents of the file (this array may be null)

FileFollower

public FileFollower(java.io.File file,
                    OutputDestination[] initialOutputDestinations)
Identical to FileFollower(File, int, int, OutputDestination[]), except that a default buffer size (32,768 characters) and latency (1000 milliseconds) are used.

See Also:
FileFollower(File, int, int, OutputDestination[])
Method Detail

start

public void start()
Cause this FileFollower to spawn a thread which will follow the file supplied in the constructor and send its contents to all of the FileFollower's OutputDestinations.

If this FileFollower is running but paused, this method equates to calling unpause().


pause

public void pause()

unpause

public void unpause()

restart

public void restart()

stop

public void stop()
Cause this FileFollower to stop following the file supplied in the constructor after it flushes the characters it's currently reading to all its OutputDestinations.


stopAndWait

public void stopAndWait()
                 throws java.lang.InterruptedException
Like stop(), but this method will not exit until the thread which is following the file has finished executing (i.e., stop synchronously).

Throws:
java.lang.InterruptedException

addOutputDestination

public boolean addOutputDestination(OutputDestination outputDestination)
Add another OutputDestination to which the followed file's contents should be printed.

Parameters:
outputDestination - OutputDestination to be added

removeOutputDestination

public boolean removeOutputDestination(OutputDestination outputDestination)
Remove the supplied OutputDestination from the list of OutputDestinations to which the followed file's contents should be printed.

Parameters:
outputDestination - OutputDestination to be removed

getOutputDestinations

public java.util.List<OutputDestination> getOutputDestinations()
Returns the List which maintains all OutputDestinations for this FileFollower.

Returns:
contains all OutputDestinations for this FileFollower

getFollowedFile

public java.io.File getFollowedFile()
Returns the file which is being followed by this FileFollower

Returns:
file being followed

isBeingFollowed

public boolean isBeingFollowed()
Returns the following state of a file

Returns:
true if being followed, false if not being followed

isPaused

public boolean isPaused()
Returns the pause state of the follower.

Returns:
true if paused, false otherwise

getBufferSize

public int getBufferSize()
Returns the size of the character buffer used to read characters from the followed file. Each time the file is accessed, this buffer is filled.

Returns:
size of the character buffer

setBufferSize

public void setBufferSize(int bufferSize)
Sets the size of the character buffer used to read characters from the followed file. Increasing buffer size will improve efficiency but increase the amount of memory used by the FileFollower.
NOTE: Setting this value will not cause a running FileFollower to immediately begin reading characters into a buffer of the newly specified size. You must stop & restart the FileFollower in order for changes to take effect.

Parameters:
bufferSize - size of the character buffer

getLatency

public int getLatency()
Returns the time (in milliseconds) which a FileFollower spends sleeping each time it encounters the end of the followed file.

Returns:
latency, in milliseconds

setLatency

public void setLatency(int latency)
Sets the time (in milliseconds) which a FileFollower spends sleeping each time it encounters the end of the followed file. Note that extremely low latency values may cause thrashing between the FileFollower's running thread and other threads in an application. A change in this value will be reflected the next time the FileFollower's running thread sleeps.

Parameters:
latency - latency, in milliseconds


Copyright © 2008. All Rights Reserved.