/* * @(#)FileLock.java 1.8 03/12/19 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.nio.channels; import java.io.IOException; /** * A token representing a lock on a region of a file. * *
A file-lock object is created each time a lock is acquired on a file via * one of the {@link FileChannel#lock(long,long,boolean) lock} or {@link * FileChannel#tryLock(long,long,boolean) tryLock} methods of the {@link * FileChannel} class. * *
A file-lock object is initially valid. It remains valid until the lock * is released by invoking the {@link #release release} method, by closing the * channel that was used to acquire it, or by the termination of the Java * virtual machine, whichever comes first. The validity of a lock may be * tested by invoking its {@link #isValid isValid} method. * *
A file lock is either exclusive or shared. A shared lock * prevents other concurrently-running programs from acquiring an overlapping * exclusive lock, but does allow them to acquire overlapping shared locks. An * exclusive lock prevents other programs from acquiring an overlapping lock of * either type. Once it is released, a lock has no further effect on the locks * that may be acquired by other programs. * *
Whether a lock is exclusive or shared may be determined by invoking its * {@link #isShared isShared} method. Some platforms do not support shared * locks, in which case a request for a shared lock is automatically converted * into a request for an exclusive lock. * *
The locks held on a particular file by a single Java virtual machine do * not overlap. The {@link #overlaps overlaps} method may be used to test * whether a candidate lock range overlaps an existing lock. * *
A file-lock object records the file channel upon whose file the lock is * held, the type and validity of the lock, and the position and size of the * locked region. Only the validity of a lock is subject to change over time; * all other aspects of a lock's state are immutable. * *
File locks are held on behalf of the entire Java virtual machine. * They are not suitable for controlling access to a file by multiple * threads within the same virtual machine. * *
* * @return true if lock is shared, * false if it is exclusive */ public final boolean isShared() { return shared; } /** * Tells whether or not this lock overlaps the given lock range. * * @return true if, and only if, this lock and the given lock * range overlap by at least one byte */ public final boolean overlaps(long position, long size) { if (position + size <= this.position) return false; // That is below this if (this.position + this.size <= position) return false; // This is below that return true; } /** * Tells whether or not this lock is valid. * *A lock object remains valid until it is released or the associated * file channel is closed, whichever comes first.
* * @return true if, and only if, this lock is valid */ public abstract boolean isValid(); /** * Releases this lock. * *If this lock object is valid then invoking this method releases the * lock and renders the object invalid. If this lock object is invalid * then invoking this method has no effect.
* * @throws ClosedChannelException * If the channel that was used to acquire this lock * is no longer open * * @throws IOException * If an I/O error occurs */ public abstract void release() throws IOException; /** * Returns a string describing the range, type, and validity of this lock. * * @return A descriptive string */ public final String toString() { return (this.getClass().getName() + "[" + position + ":" + size + " " + (shared ? "shared" : "exclusive") + " " + (isValid() ? "valid" : "invalid") + "]"); } }