# FileLock

public abstract class **FileLock** extends [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)  
implements [AutoCloseable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html) 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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html#lock(long,%20long,%20boolean)` or `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html#tryLock(long,%20long,%20boolean)` methods of the
`https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html` class, or the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#lock(long,%20long,%20boolean,%20A,%20java.nio.channels.CompletionHandler<java.nio.channels.FileLock,%20?%20super%20A>)`
or `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html#tryLock(long,%20long,%20boolean)`
methods of the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html` class.

A file-lock object is initially valid. It remains valid until the lock
is released by invoking the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#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
`https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#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 `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#overlaps(long,%20long)` 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.

File-lock objects are safe for use by multiple concurrent threads.


## Platform dependencies

This file-locking API is intended to map directly to the native locking
facility of the underlying operating system. Thus the locks held on a file
should be visible to all programs that have access to the file, regardless
of the language in which those programs are written.

Whether or not a lock actually prevents another program from accessing
the content of the locked region is system-dependent and therefore
unspecified. The native file-locking facilities of some systems are merely
*advisory*, meaning that programs must cooperatively observe a known
locking protocol in order to guarantee data integrity. On other systems
native file locks are *mandatory*, meaning that if one program locks a
region of a file then other programs are actually prevented from accessing
that region in a way that would violate the lock. On yet other systems,
whether native file locks are advisory or mandatory is configurable on a
per-file basis. To ensure consistent and correct behavior across platforms,
it is strongly recommended that the locks provided by this API be used as if
they were advisory locks.

On some systems, acquiring a mandatory lock on a region of a file
prevents that region from being `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html#map(java.nio.channels.FileChannel.MapMode,%20long,%20long)`, and vice versa. Programs that combine
locking and mapping should be prepared for this combination to fail.

On some systems, closing a channel releases all locks held by the Java
virtual machine on the underlying file regardless of whether the locks were
acquired via that channel or via another channel open on the same file. It
is strongly recommended that, within a program, a unique channel be used to
acquire all locks on any given file.

Some network filesystems permit file locking to be used with
memory-mapped files only when the locked regions are page-aligned and a
whole multiple of the underlying hardware's page size. Some network
filesystems do not implement file locks on regions that extend past a
certain position, often 2^30^ or 2^31^. In general, great
care should be taken when locking files that reside on network filesystems.

### Protected Constructor Summary

|---|---|
|   | [FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#FileLock(java.nio.channels.FileChannel,%20long,%20long,%20boolean))([FileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html) channel, long position, long size, boolean shared) Initializes a new instance of this class. |
|   | [FileLock](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#FileLock(java.nio.channels.AsynchronousFileChannel,%20long,%20long,%20boolean))([AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html) channel, long position, long size, boolean shared) Initializes a new instance of this class. |

### Public Method Summary

|---|---|
| [Channel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html) | [acquiredBy](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#acquiredBy())() Returns the channel upon whose file this lock was acquired. |
| final [FileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html) | [channel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#channel())() Returns the file channel upon whose file this lock was acquired. |
| final void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#close())() This method invokes the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#release()` method. |
| final boolean | [isShared](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#isShared())() Tells whether this lock is shared. |
| abstract boolean | [isValid](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#isValid())() Tells whether or not this lock is valid. |
| final boolean | [overlaps](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#overlaps(long,%20long))(long position, long size) Tells whether or not this lock overlaps the given lock range. |
| final long | [position](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#position())() Returns the position within the file of the first byte of the locked region. |
| abstract void | [release](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#release())() Releases this lock. |
| final long | [size](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#size())() Returns the size of the locked region in bytes. |
| final [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#toString())() Returns a string describing the range, type, and validity of this lock. |

### Inherited Method Summary

From class [java.lang.Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html)

|---|---|
| [Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) | [clone](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#clone())() Creates and returns a copy of this `Object`. |
| boolean | [equals](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#equals(java.lang.Object))([Object](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html) obj) Compares this instance with the specified object and indicates if they are equal. |
| void | [finalize](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#finalize())() Invoked when the garbage collector has detected that this instance is no longer reachable. |
| final [Class](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html)\<?\> | [getClass](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#getClass())() Returns the unique instance of `https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Class.html` that represents this object's class. |
| int | [hashCode](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#hashCode())() Returns an integer hash code for this object. |
| final void | [notify](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notify())() Causes a thread which is waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| final void | [notifyAll](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#notifyAll())() Causes all threads which are waiting on this object's monitor (by means of calling one of the `wait()` methods) to be woken up. |
| [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html) | [toString](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#toString())() Returns a string containing a concise, human-readable description of this object. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long,%20int))(long timeout, int nanos) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait(long))(long timeout) Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object or until the specified timeout expires. |
| final void | [wait](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/Object.html#wait())() Causes the calling thread to wait until another thread calls the `notify()` or `notifyAll()` method of this object. |

From interface [java.lang.AutoCloseable](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html)

|---|---|
| abstract void | [close](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/AutoCloseable.html#close())() Closes this resource, relinquishing any underlying resources. |

## Protected Constructors

#### protected
**FileLock**
([FileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html) channel, long position, long size, boolean shared)

Initializes a new instance of this class.

##### Parameters

| channel | The file channel upon whose file this lock is held |
| position | The position within the file at which the locked region starts; must be non-negative |
| size | The size of the locked region; must be non-negative, and the sum position + size must be non-negative |
| shared | true if this lock is shared, false if it is exclusive |
|---|---|

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the preconditions on the parameters do not hold |
|---|---|

#### protected
**FileLock**
([AsynchronousFileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/AsynchronousFileChannel.html) channel, long position, long size, boolean shared)

Initializes a new instance of this class.

##### Parameters

| channel | The channel upon whose file this lock is held |
| position | The position within the file at which the locked region starts; must be non-negative |
| size | The size of the locked region; must be non-negative, and the sum position + size must be non-negative |
| shared | true if this lock is shared, false if it is exclusive |
|---|---|

##### Throws

| [IllegalArgumentException](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/IllegalArgumentException.html) | If the preconditions on the parameters do not hold |
|---|---|

## Public Methods

#### public [Channel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/Channel.html)
**acquiredBy**
()

Returns the channel upon whose file this lock was acquired.

##### Returns

- The channel upon whose file this lock was acquired.

#### public final [FileChannel](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileChannel.html)
**channel**
()

Returns the file channel upon whose file this lock was acquired.

This method has been superseded by the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#acquiredBy()`
method.

##### Returns

- The file channel, or `null` if the file lock was not acquired by a file channel.

#### public final void
**close**
()

This method invokes the `https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/FileLock.html#release()` method. It was added
to the class so that it could be used in conjunction with the
automatic resource management block construct.

##### Throws

| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) |   |
|---|---|

#### public final boolean
**isShared**
()

Tells whether this lock is shared.

##### Returns

- true if lock is shared, false if it is exclusive

#### public abstract boolean
**isValid**
()

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.

<br />

##### Returns

- true if, and only if, this lock is valid

#### public final boolean
**overlaps**
(long position, long size)

Tells whether or not this lock overlaps the given lock range.

##### Parameters

| position | The starting position of the lock range |
| size | The size of the lock range |
|---|---|

##### Returns

- true if, and only if, this lock and the given lock range overlap by at least one byte

#### public final long
**position**
()

Returns the position within the file of the first byte of the locked
region.

A locked region need not be contained within, or even overlap, the
actual underlying file, so the value returned by this method may exceed
the file's current size.

<br />

##### Returns

- The position

#### public abstract void
**release**
()

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.

<br />

##### Throws

| [ClosedChannelException](https://developers.google.com/j2objc/javadoc/jre/reference/java/nio/channels/ClosedChannelException.html) | If the channel that was used to acquire this lock is no longer open |
| [IOException](https://developers.google.com/j2objc/javadoc/jre/reference/java/io/IOException.html) | If an I/O error occurs |
|---|---|

#### public final long
**size**
()

Returns the size of the locked region in bytes.

A locked region need not be contained within, or even overlap, the
actual underlying file, so the value returned by this method may exceed
the file's current size.

<br />

##### Returns

- The size of the locked region

#### public final [String](https://developers.google.com/j2objc/javadoc/jre/reference/java/lang/String.html)
**toString**
()

Returns a string describing the range, type, and validity of this lock.

##### Returns

- A descriptive string