151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/*
246805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it
651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as
751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation.  Oracle designates this
851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided
951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code.
1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT
1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that
1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code).
1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version
1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation,
1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any
2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions.
2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.nio.channels;
2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.io.IOException;
2951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/**
3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * A token representing a lock on a region of a file.
3251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
3351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> A file-lock object is created each time a lock is acquired on a file via
3451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * one of the {@link FileChannel#lock(long,long,boolean) lock} or {@link
3551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FileChannel#tryLock(long,long,boolean) tryLock} methods of the
36e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * {@link FileChannel} class, or the {@link
37e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * AsynchronousFileChannel#lock(long,long,boolean,Object,CompletionHandler) lock}
38e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * or {@link AsynchronousFileChannel#tryLock(long,long,boolean) tryLock}
39e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera * methods of the {@link AsynchronousFileChannel} class.
4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> A file-lock object is initially valid.  It remains valid until the lock
4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is released by invoking the {@link #release release} method, by closing the
4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * channel that was used to acquire it, or by the termination of the Java
4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * virtual machine, whichever comes first.  The validity of a lock may be
4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * tested by invoking its {@link #isValid isValid} method.
4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> A file lock is either <i>exclusive</i> or <i>shared</i>.  A shared lock
4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prevents other concurrently-running programs from acquiring an overlapping
4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exclusive lock, but does allow them to acquire overlapping shared locks.  An
5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exclusive lock prevents other programs from acquiring an overlapping lock of
5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * either type.  Once it is released, a lock has no further effect on the locks
5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that may be acquired by other programs.
5351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Whether a lock is exclusive or shared may be determined by invoking its
5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link #isShared isShared} method.  Some platforms do not support shared
5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * locks, in which case a request for a shared lock is automatically converted
5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * into a request for an exclusive lock.
5851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
5951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The locks held on a particular file by a single Java virtual machine do
6051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not overlap.  The {@link #overlaps overlaps} method may be used to test
6151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * whether a candidate lock range overlaps an existing lock.
6251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> A file-lock object records the file channel upon whose file the lock is
6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * held, the type and validity of the lock, and the position and size of the
6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * locked region.  Only the validity of a lock is subject to change over time;
6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * all other aspects of a lock's state are immutable.
6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> File locks are held on behalf of the entire Java virtual machine.
6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * They are not suitable for controlling access to a file by multiple
7051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * threads within the same virtual machine.
7151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> File-lock objects are safe for use by multiple concurrent threads.
7351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7546805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera * <a name="pdep"></a><h2> Platform dependencies </h2>
7651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
7751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> This file-locking API is intended to map directly to the native locking
7851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * facility of the underlying operating system.  Thus the locks held on a file
7951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * should be visible to all programs that have access to the file, regardless
8051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the language in which those programs are written.
8151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
8251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Whether or not a lock actually prevents another program from accessing
8351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the content of the locked region is system-dependent and therefore
8451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * unspecified.  The native file-locking facilities of some systems are merely
8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>advisory</i>, meaning that programs must cooperatively observe a known
8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * locking protocol in order to guarantee data integrity.  On other systems
8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * native file locks are <i>mandatory</i>, meaning that if one program locks a
8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * region of a file then other programs are actually prevented from accessing
8951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that region in a way that would violate the lock.  On yet other systems,
9051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * whether native file locks are advisory or mandatory is configurable on a
9151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * per-file basis.  To ensure consistent and correct behavior across platforms,
9251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * it is strongly recommended that the locks provided by this API be used as if
9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * they were advisory locks.
9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> On some systems, acquiring a mandatory lock on a region of a file
9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * prevents that region from being {@link java.nio.channels.FileChannel#map
9751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <i>mapped into memory</i>}, and vice versa.  Programs that combine
9851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * locking and mapping should be prepared for this combination to fail.
9951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> On some systems, closing a channel releases all locks held by the Java
10151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * virtual machine on the underlying file regardless of whether the locks were
10251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * acquired via that channel or via another channel open on the same file.  It
10351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is strongly recommended that, within a program, a unique channel be used to
10451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * acquire all locks on any given file.
10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> Some network filesystems permit file locking to be used with
10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * memory-mapped files only when the locked regions are page-aligned and a
10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * whole multiple of the underlying hardware's page size.  Some network
10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * filesystems do not implement file locks on regions that extend past a
11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * certain position, often 2<sup>30</sup> or 2<sup>31</sup>.  In general, great
11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * care should be taken when locking files that reside on network filesystems.
11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski *
11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author Mark Reinhold
11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author JSR-51 Expert Group
11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4
11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */
11851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
11951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic abstract class FileLock implements AutoCloseable {
12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private final Channel channel;
12251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private final long position;
12351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private final long size;
12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    private final boolean shared;
12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
12746805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera     * Initializes a new instance of this class.
12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param  channel
13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         The file channel upon whose file this lock is held
13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param  position
13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         The position within the file at which the locked region starts;
13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         must be non-negative
13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param  size
13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         The size of the locked region; must be non-negative, and the sum
13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
13951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @param  shared
14151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>true</tt> if this lock is shared,
14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>false</tt> if it is exclusive
14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws IllegalArgumentException
14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         If the preconditions on the parameters do not hold
14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    protected FileLock(FileChannel channel,
14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                       long position, long size, boolean shared)
14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    {
15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (position < 0)
15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException("Negative position");
15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (size < 0)
15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException("Negative size");
154e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (position + size < 0)
155e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw new IllegalArgumentException("Negative position + size");
156e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        this.channel = channel;
157e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        this.position = position;
158e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        this.size = size;
159e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        this.shared = shared;
160e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    }
161e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera
162e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    /**
163e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * Initializes a new instance of this class.
164e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
165e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param  channel
166e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         The channel upon whose file this lock is held
167e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
168e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param  position
169e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         The position within the file at which the locked region starts;
170e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         must be non-negative
171e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
172e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param  size
173e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         The size of the locked region; must be non-negative, and the sum
174e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
175e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
176e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @param  shared
177e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         <tt>true</tt> if this lock is shared,
178e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         <tt>false</tt> if it is exclusive
179e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
180e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @throws IllegalArgumentException
181e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *         If the preconditions on the parameters do not hold
182e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     *
183e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     * @since 1.7
184e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera     */
185e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    protected FileLock(AsynchronousFileChannel channel,
186e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera                       long position, long size, boolean shared)
187e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera    {
188e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (position < 0)
189e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw new IllegalArgumentException("Negative position");
190e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera        if (size < 0)
191e6bac4bf9c85c2454ce22c91da6c654552c268e0Shubham Ajmera            throw new IllegalArgumentException("Negative size");
19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (position + size < 0)
19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            throw new IllegalArgumentException("Negative position + size");
19451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        this.channel = channel;
19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        this.position = position;
19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        this.size = size;
19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        this.shared = shared;
19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
20051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
20151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the file channel upon whose file this lock was acquired.
20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
20351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> This method has been superseded by the {@link #acquiredBy acquiredBy}
20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * method.
20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  The file channel, or {@code null} if the file lock was not
20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          acquired by a file channel.
20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public final FileChannel channel() {
21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (channel instanceof FileChannel) ? (FileChannel)channel : null;
21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the channel upon whose file this lock was acquired.
21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  The channel upon whose file this lock was acquired.
21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.7
21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public Channel acquiredBy() {
22151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return channel;
22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
22351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the position within the file of the first byte of the locked
22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * region.
22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> A locked region need not be contained within, or even overlap, the
22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * actual underlying file, so the value returned by this method may exceed
23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the file's current size.  </p>
23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  The position
23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
23451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public final long position() {
23551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return position;
23651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
23751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
23851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
23951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns the size of the locked region in bytes.
24051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> A locked region need not be contained within, or even overlap, the
24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * actual underlying file, so the value returned by this method may exceed
24351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * the file's current size.  </p>
24451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
24551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  The size of the locked region
24651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
24751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public final long size() {
24851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return size;
24951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
25051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
25151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
25246805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera     * Tells whether this lock is shared.
25351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
25451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return <tt>true</tt> if lock is shared,
25551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *         <tt>false</tt> if it is exclusive
25651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
25751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public final boolean isShared() {
25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return shared;
25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
26051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
26246805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera     * Tells whether or not this lock overlaps the given lock range.
26346805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera     *
26446805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera     * @param   position
26546805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera     *          The starting position of the lock range
26646805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera     * @param   size
26746805d66f32e857c885434b4fa8cb6b32dcc4bf7Shubham Ajmera     *          The size of the lock range
26851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
26951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  <tt>true</tt> if, and only if, this lock and the given lock
27051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          range overlap by at least one byte
27151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
27251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public final boolean overlaps(long position, long size) {
27351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (position + size <= this.position)
27451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;               // That is below this
27551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        if (this.position + this.size <= position)
27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski            return false;               // This is below that
27751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return true;
27851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
27951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Tells whether or not this lock is valid.
28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> A lock object remains valid until it is released or the associated
28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * file channel is closed, whichever comes first.  </p>
28551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
28651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  <tt>true</tt> if, and only if, this lock is valid
28751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
28851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public abstract boolean isValid();
28951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
29051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
29151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Releases this lock.
29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * <p> If this lock object is valid then invoking this method releases the
29451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * lock and renders the object invalid.  If this lock object is invalid
29551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * then invoking this method has no effect.  </p>
29651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
29751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws  ClosedChannelException
29851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          If the channel that was used to acquire this lock
29951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          is no longer open
30051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
30151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @throws  IOException
30251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *          If an I/O error occurs
30351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
30451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public abstract void release() throws IOException;
30551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
30651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
30751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * This method invokes the {@link #release} method. It was added
30851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * to the class so that it could be used in conjunction with the
30951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * automatic resource management block construct.
31051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
31151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @since 1.7
31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public final void close() throws IOException {
31451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        release();
31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    /**
31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * Returns a string describing the range, type, and validity of this lock.
31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     *
32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     * @return  A descriptive string
32151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski     */
32251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    public final String toString() {
32351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski        return (this.getClass().getName()
32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                + "[" + position
32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                + ":" + size
32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                + " " + (shared ? "shared" : "exclusive")
32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                + " " + (isValid() ? "valid" : "invalid")
32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski                + "]");
32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski    }
33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski
33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski}
332