2 * This file is part of the Vamsas Client version 0.1.
3 * Copyright 2009 by Jim Procter, Iain Milne, Pierre Marguerite,
4 * Andrew Waterhouse and Dominik Lindner.
6 * Earlier versions have also been incorporated into Jalview version 2.4
7 * since 2008, and TOPALi version 2 since 2007.
9 * The Vamsas Client is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * The Vamsas Client is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the Vamsas Client. If not, see <http://www.gnu.org/licenses/>.
22 package uk.ac.vamsas.client.simpleclient;
24 import java.io.BufferedInputStream;
25 import java.io.BufferedOutputStream;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.io.RandomAccessFile;
32 import java.nio.channels.ByteChannel;
33 import java.nio.channels.FileChannel;
34 import java.nio.channels.ReadableByteChannel;
36 import org.apache.commons.logging.LogFactory;
39 * transient object representing a file lock This lock should hold for all
40 * processes interacting in a session.
45 public abstract class Lock {
46 protected org.apache.commons.logging.Log log = LogFactory.getLog(Lock.class);
48 File target = null; // The file that is being locked
50 protected RandomAccessFile rafile = null;
53 * creates a valid Lock (test with <method>isLocked</method>) if a lock could
54 * be obtained for <param>lockfile</param>
58 protected Lock(java.io.File lockfile) {
63 * test whether the given file is a target or related to the lock on the
68 * @return true if target is locked and afile is related to target
70 public abstract boolean isTargetLockFile(File afile);
74 * @return true if lock is held on the target
76 public abstract boolean isLocked();
79 * release lock and close all managed channels to file
82 public abstract void release();
85 * optionally close the open random access channel on the file when releasing
90 public abstract void release(boolean closeChannel);
93 * gets Locked Stream for reading from
96 * true to start reading at beginning of file.
97 * @return null if file not locked
100 public abstract FileInputStream getFileInputStream(boolean atStart)
104 * gets Locked stream to write to FileInput always starts at the *end* of the
105 * file (after any truncation)
108 * true means file will be cleared to zero length
109 * @return null if file is not locked
110 * @throws IOException
112 public abstract FileOutputStream getFileOutputStream(boolean clear)
116 * return buffered output stream to locked file.
119 * - true means file is truncated to 0 length before writing
122 public abstract BufferedOutputStream getBufferedOutputStream(boolean clear)
125 protected void finalize() throws Throwable {
130 * return buffered input stream for locked file.
133 * - true means read from begining of file
134 * @return null if file is not locked.
136 public BufferedInputStream getBufferedInputStream(boolean atStart)
138 FileInputStream fis = getFileInputStream(atStart);
140 return new BufferedInputStream(fis);
145 * safe lock target length() function.
147 * @return -1 for non-lockable target, otherwise target's file length
149 public abstract long length();
151 public abstract RandomAccessFile getRaFile() throws IOException;
153 public abstract FileChannel getRaChannel() throws IOException;