FileDocCategorySizeDatePackage
FileLock.javaAPI DocAndroid 1.5 API8177Wed May 06 22:41:04 BST 2009java.nio.channels

FileLock

public abstract class FileLock extends Object
A {@code FileLock} represents a locked region of a file.

Locks have certain properties that enable collaborating processes to avoid the lost update problem or reading inconsistent data. Logically, a file lock can be exclusive or shared. Multiple processes can hold shared locks on the same region of a file, but only a single process can hold an exclusive lock on a given region of a file and no other process can simultaneously hold a shared lock overlapping the exclusive lock. An application can determine whether a {@code FileLock} is shared or exclusive via the {@code isShared()} method.

Locks held by a particular process cannot overlap one another. Applications can determine whether a proposed lock will overlap by using the {@code overlaps(long, long)}) method. Locks held in other processes may overlap locks held in this process. Locks are shared amongst all threads in the acquiring process, and are therefore unsuitable for intra-process synchronization.

Once a lock is acquired, it is immutable in all its state except {@code isValid()}. The lock will initially be valid, but may be rendered invalid by explicit removal of the lock, using {@code release()}, or implicitly by closing the channel or exiting the process (terminating the virtual machine).

Platform dependencies

Locks are intended to be true platform operating system file locks, and therefore locks held by the virtual machine process will be visible to other operating system processes.

The characteristics of the underlying operating system locks will show through in the Java implementation. For example, some platforms' locks are 'mandatory' -- meaning the operating system enforces the locks on processes that attempt to access locked regions of files; whereas other platforms' locks are only 'advisory' -- meaning that processes are required to collaborate to ensure locks are acquired and there is a potential for processes to not play well. To be on the safe side, it is best to assume that the platform is adopting advisory locks and always acquire shared locks when reading a region of a file.

On some platforms, the presence of a lock will prevent the file from being memory-mapped. On some platforms, closing a channel on a given file handle will release all the locks held on that file -- even if there are other channels open on the same file; their locks will also be released. The safe option here is to ensure that you only acquire locks on a single channel for a particular file and that becomes the synchronization point.

Further care should be exercised when locking files maintained on network file systems, since they often have further limitations.

since
Android 1.0

Fields Summary
private final FileChannel
channel
private final long
position
private final long
size
private final boolean
shared
Constructors Summary
protected FileLock(FileChannel channel, long position, long size, boolean shared)
Constructs a new file lock instance for a given channel. The constructor enforces the starting position, length and sharing mode of the lock.

param
channel the underlying file channel that holds the lock.
param
position the starting point for the lock.
param
size the length of the lock in number of bytes.
param
shared the lock's sharing mode of lock; {@code true} is shared, {@code false} is exclusive.
since
Android 1.0

        super();
        if (position < 0 || size < 0 || position + size < 0) {
            throw new IllegalArgumentException();
        }
        this.channel = channel;
        this.position = position;
        this.size = size;
        this.shared = shared;
    
Methods Summary
public final java.nio.channels.FileChannelchannel()
Returns the lock's {@link FileChannel}.

return
the channel.
since
Android 1.0

        return channel;
    
public final booleanisShared()
Indicates if the file lock is shared with other processes or if it is exclusive.

return
{@code true} if the lock is a shared lock, {@code false} if it is exclusive.
since
Android 1.0

        return shared;
    
public abstract booleanisValid()
Indicates whether this lock is a valid file lock. The lock is valid unless the underlying channel has been closed or it has been explicitly released.

return
{@code true} if the lock is valid, {@code false} otherwise.
since
Android 1.0

public final booleanoverlaps(long start, long length)
Indicates if the receiver's lock region overlaps the region described in the parameter list.

param
start the starting position for the comparative lock.
param
length the length of the comparative lock.
return
{@code true} if there is an overlap, {@code false} otherwise.
since
Android 1.0

        final long end = position + size - 1;
        final long newEnd = start + length - 1;
        if (end < start || position > newEnd) {
            return false;
        }
        return true;
    
public final longposition()
Returns the lock's starting position in the file.

return
the lock position.
since
Android 1.0

        return position;
    
public abstract voidrelease()
Releases this particular lock on the file. If the lock is invalid then this method has no effect. Once released, the lock becomes invalid.

throws
ClosedChannelException if the channel is already closed when an attempt to release the lock is made.
throws
IOException if another I/O error occurs.
since
Android 1.0

public final longsize()
Returns the length of the file lock in bytes.

return
the size of the file lock in bytes.
since
Android 1.0

        return size;
    
public final java.lang.StringtoString()
Returns a string that shows the details of the lock suitable for display to an end user.

return
the display string.
since
Android 1.0

        StringBuffer buffer = new StringBuffer(64); // Guess length of string
        buffer.append("FileLock: [position="); //$NON-NLS-1$
        buffer.append(position);
        buffer.append(", size="); //$NON-NLS-1$
        buffer.append(size);
        buffer.append(", shared="); //$NON-NLS-1$
        buffer.append(Boolean.toString(shared));
        buffer.append("]"); //$NON-NLS-1$
        return buffer.toString();