Java.Nio.Channels.FileChannel.Lock Method
Obtains a lock on a specified region of the file.

Syntax

[Android.Runtime.Register("lock", "(JJZ)Ljava/nio/channels/FileLock;", "GetLock_JJZHandler")]
public abstract FileLock Lock (long position, long size, bool shared)

Parameters

position
the starting position for the locked region.
size
the length of the locked region in bytes.
shared
a flag indicating whether an attempt should be made to acquire a shared lock.

Returns

Documentation for this section has not yet been entered.

Exceptions

TypeReason
Java.Lang.IllegalArgumentExceptionif position or size is negative.
Java.Nio.Channels.ClosedChannelExceptionif this channel is closed.
Java.Nio.Channels.OverlappingFileLockExceptionif the requested region overlaps an existing lock or pending lock request.
Java.Nio.Channels.NonReadableChannelExceptionif the channel is not opened in read-mode but shared is true.
Java.Nio.Channels.NonWritableChannelExceptionif the channel is not opened in write mode but shared is false.
Java.Nio.Channels.AsynchronousCloseExceptionif this channel is closed by another thread while this method is executing.
Java.Nio.Channels.FileLockInterruptionExceptionif the thread is interrupted while in the state of waiting on the desired file lock.
Java.IO.IOExceptionif another I/O error occurs.

Remarks

Obtains a lock on a specified region of the file.

This is the blocking version of lock acquisition, see also the tryLock() methods.

Attempts to acquire an overlapping lock region will fail. The attempt will fail if the overlapping lock has already been obtained, or if another thread is currently waiting to acquire the overlapping lock.

If the request is not for an overlapping lock, the thread calling this method will block until the lock is obtained (likely by no contention or another process releasing a lock), or until this thread is interrupted or the channel is closed.

If the lock is obtained successfully then the Java.Nio.Channels.FileLock object returned represents the lock for subsequent operations on the locked region.

If the thread is interrupted while waiting for the lock, the thread is set to the interrupted state and throws a Java.Nio.Channels.FileLockInterruptionException. If this channel is closed while the thread is waiting to obtain the lock then the thread throws a Java.Nio.Channels.AsynchronousCloseException.

There is no requirement for the position and size to be within the current start and length of the file.

Some platforms do not support shared locks, and if a request is made for a shared lock on such a platform, this method will attempt to acquire an exclusive lock instead. It is undefined whether the lock obtained is advisory or mandatory.

[Android Documentation]

Requirements

Namespace: Java.Nio.Channels
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 1