1 package uk.ac.vamsas.client.simpleclient;
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.OutputStream;
10 import java.io.RandomAccessFile;
11 import java.nio.channels.ByteChannel;
12 import java.nio.channels.FileChannel;
13 import java.nio.channels.ReadableByteChannel;
15 import org.apache.commons.logging.LogFactory;
18 * transient object representing a file lock
19 * This lock should hold for all processes interacting in a session.
23 public abstract class Lock {
24 protected org.apache.commons.logging.Log log = LogFactory.getLog(Lock.class);
25 File target = null; // The file that is being locked
26 protected RandomAccessFile rafile=null;
29 * creates a valid Lock (test with <method>isLocked</method>)
30 * if a lock could be obtained for <param>lockfile</param>
33 protected Lock(java.io.File lockfile) {
37 * test whether the given file is a target or related to the lock
40 * @return true if target is locked and afile is related to target
42 public abstract boolean isTargetLockFile(File afile);
45 * @return true if lock is held on the target
47 public abstract boolean isLocked();
49 * release lock and close all managed channels to file
52 public abstract void release();
54 * optionally close the open random access channel on the file when releasing lock
57 public abstract void release(boolean closeChannel);
60 * gets Locked Stream for reading from
61 * @param atStart true to start reading at beginning of file.
62 * @return null if file not locked
65 public abstract FileInputStream getFileInputStream(boolean atStart) throws IOException;
68 * gets Locked stream to write to
69 * FileInput always starts at the *end* of the file (after any truncation)
70 * @param clear true means file will be cleared to zero length
71 * @return null if file is not locked
74 public abstract FileOutputStream getFileOutputStream(boolean clear) throws IOException;
76 * return buffered output stream to locked file.
77 * @param clear - true means file is truncated to 0 length before writing
80 public abstract BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException;
82 protected void finalize() throws Throwable {
86 * return buffered input stream for locked file.
87 * @param atStart - true means read from begining of file
88 * @return null if file is not locked.
90 public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
91 FileInputStream fis = getFileInputStream(atStart);
93 return new BufferedInputStream(fis);
97 * safe lock target length() function.
98 * @return -1 for non-lockable target, otherwise target's file length
100 public abstract long length();
101 public abstract RandomAccessFile getRaFile() throws IOException;
102 public abstract FileChannel getRaChannel() throws IOException;