uk.ac.vamsas.client.simpleclient
Class NativeLock

java.lang.Object
  extended byuk.ac.vamsas.client.simpleclient.Lock
      extended byuk.ac.vamsas.client.simpleclient.NativeLock

public class NativeLock
extends Lock

Author:
JimP

Field Summary
protected  java.nio.channels.FileLock lock
           
 
Fields inherited from class uk.ac.vamsas.client.simpleclient.Lock
log, rafile
 
Constructor Summary
NativeLock(java.io.File lockfile, boolean block)
           
 
Method Summary
protected  void finalize()
           
 java.io.BufferedOutputStream getBufferedOutputStream(boolean clear)
          return buffered output stream to locked file.
 java.io.FileInputStream getFileInputStream(boolean atStart)
          gets Locked Stream for reading from
 java.io.FileOutputStream getFileOutputStream(boolean clear)
          gets Locked stream to write to FileInput always starts at the *end* of the file (after any truncation)
 java.nio.channels.FileChannel getRaChannel()
           
 java.io.RandomAccessFile getRaFile()
           
 boolean isLocked()
           
 boolean isTargetLockFile(java.io.File afile)
          test whether the given file is a target or related to the lock on the target file.
 long length()
          safe lock target length() function.
 void release()
          release lock and close all managed channels to file
 void release(boolean closeChannel)
          optionally close the open random access channel on the file when releasing lock
 
Methods inherited from class uk.ac.vamsas.client.simpleclient.Lock
getBufferedInputStream
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

lock

protected java.nio.channels.FileLock lock
Constructor Detail

NativeLock

public NativeLock(java.io.File lockfile,
                  boolean block)
Parameters:
lockfile -
block - true means thread will block until a lock is obtained.
Method Detail

isLocked

public boolean isLocked()
Specified by:
isLocked in class Lock
Returns:
true if lock is held on the target

release

public void release()
Description copied from class: Lock
release lock and close all managed channels to file

Specified by:
release in class Lock

release

public void release(boolean closeChannel)
Description copied from class: Lock
optionally close the open random access channel on the file when releasing lock

Specified by:
release in class Lock
Parameters:
closeChannel -

getFileInputStream

public java.io.FileInputStream getFileInputStream(boolean atStart)
                                           throws java.io.IOException
gets Locked Stream for reading from

Specified by:
getFileInputStream in class Lock
Parameters:
atStart - true to start reading at beginning of file.
Returns:
null if file not locked
Throws:
java.io.IOException

getFileOutputStream

public java.io.FileOutputStream getFileOutputStream(boolean clear)
                                             throws java.io.IOException
gets Locked stream to write to FileInput always starts at the *end* of the file (after any truncation)

Specified by:
getFileOutputStream in class Lock
Parameters:
clear - true means file will be cleared to zero length
Returns:
null if file is not locked
Throws:
java.io.IOException

getBufferedOutputStream

public java.io.BufferedOutputStream getBufferedOutputStream(boolean clear)
                                                     throws java.io.IOException
return buffered output stream to locked file.

Specified by:
getBufferedOutputStream in class Lock
Parameters:
clear - - true means file is truncated to 0 length before writing
Returns:
Throws:
java.io.IOException

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class Lock
Throws:
java.lang.Throwable
See Also:
Lock.finalize()

length

public long length()
Description copied from class: Lock
safe lock target length() function.

Specified by:
length in class Lock
Returns:
-1 for non-lockable target, otherwise target's file length

getRaFile

public java.io.RandomAccessFile getRaFile()
                                   throws java.io.IOException
Specified by:
getRaFile in class Lock
Throws:
java.io.IOException

getRaChannel

public java.nio.channels.FileChannel getRaChannel()
                                           throws java.io.IOException
Specified by:
getRaChannel in class Lock
Throws:
java.io.IOException

isTargetLockFile

public boolean isTargetLockFile(java.io.File afile)
Description copied from class: Lock
test whether the given file is a target or related to the lock on the target file.

Specified by:
isTargetLockFile in class Lock
Parameters:
afile - a file
Returns:
true if target is locked and afile is related to target