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) {
38 * @return true if lock is held on the target
40 public abstract boolean isLocked();
42 * release lock and close all managed channels to file
45 public abstract void release();
47 * optionally close the open random access channel on the file when releasing lock
50 public abstract void release(boolean closeChannel);
53 * gets Locked Stream for reading from
54 * @param atStart true to start reading at beginning of file.
55 * @return null if file not locked
58 public abstract FileInputStream getFileInputStream(boolean atStart) throws IOException;
61 * gets Locked stream to write to
62 * FileInput always starts at the *end* of the file (after any truncation)
63 * @param clear true means file will be cleared to zero length
64 * @return null if file is not locked
67 public abstract FileOutputStream getFileOutputStream(boolean clear) throws IOException;
69 * return buffered output stream to locked file.
70 * @param clear - true means file is truncated to 0 length before writing
73 public abstract BufferedOutputStream getBufferedOutputStream(boolean clear) throws IOException;
75 protected void finalize() throws Throwable {
79 * return buffered input stream for locked file.
80 * @param atStart - true means read from begining of file
81 * @return null if file is not locked.
83 public BufferedInputStream getBufferedInputStream(boolean atStart) throws IOException {
84 FileInputStream fis = getFileInputStream(atStart);
86 return new BufferedInputStream(fis);
90 * safe lock target length() function.
91 * @return -1 for non-lockable target, otherwise target's file length
93 public abstract long length();
94 public abstract RandomAccessFile getRaFile() throws IOException;
95 public abstract FileChannel getRaChannel() throws IOException;