jdk/src/share/classes/java/nio/channels/FileLock.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2057 3acf8e5e2ca0
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.nio.channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A token representing a lock on a region of a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p> A file-lock object is created each time a lock is acquired on a file via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * one of the {@link FileChannel#lock(long,long,boolean) lock} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * FileChannel#tryLock(long,long,boolean) tryLock} methods of the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * FileChannel} class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <p> A file-lock object is initially valid.  It remains valid until the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * is released by invoking the {@link #release release} method, by closing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * channel that was used to acquire it, or by the termination of the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * virtual machine, whichever comes first.  The validity of a lock may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * tested by invoking its {@link #isValid isValid} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * <p> A file lock is either <i>exclusive</i> or <i>shared</i>.  A shared lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * prevents other concurrently-running programs from acquiring an overlapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * exclusive lock, but does allow them to acquire overlapping shared locks.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * exclusive lock prevents other programs from acquiring an overlapping lock of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * either type.  Once it is released, a lock has no further effect on the locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * that may be acquired by other programs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p> Whether a lock is exclusive or shared may be determined by invoking its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * {@link #isShared isShared} method.  Some platforms do not support shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * locks, in which case a request for a shared lock is automatically converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * into a request for an exclusive lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p> The locks held on a particular file by a single Java virtual machine do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * not overlap.  The {@link #overlaps overlaps} method may be used to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * whether a candidate lock range overlaps an existing lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * <p> A file-lock object records the file channel upon whose file the lock is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * held, the type and validity of the lock, and the position and size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * locked region.  Only the validity of a lock is subject to change over time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * all other aspects of a lock's state are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p> File locks are held on behalf of the entire Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * They are not suitable for controlling access to a file by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * threads within the same virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * <p> File-lock objects are safe for use by multiple concurrent threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <a name="pdep">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <h4> Platform dependencies </h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p> This file-locking API is intended to map directly to the native locking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * facility of the underlying operating system.  Thus the locks held on a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * should be visible to all programs that have access to the file, regardless
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * of the language in which those programs are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <p> Whether or not a lock actually prevents another program from accessing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * the content of the locked region is system-dependent and therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * unspecified.  The native file-locking facilities of some systems are merely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <i>advisory</i>, meaning that programs must cooperatively observe a known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * locking protocol in order to guarantee data integrity.  On other systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * native file locks are <i>mandatory</i>, meaning that if one program locks a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * region of a file then other programs are actually prevented from accessing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * that region in a way that would violate the lock.  On yet other systems,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * whether native file locks are advisory or mandatory is configurable on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * per-file basis.  To ensure consistent and correct behavior across platforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * it is strongly recommended that the locks provided by this API be used as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * they were advisory locks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p> On some systems, acquiring a mandatory lock on a region of a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * prevents that region from being {@link java.nio.channels.FileChannel#map
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * </code>mapped into memory<code>}, and vice versa.  Programs that combine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * locking and mapping should be prepared for this combination to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <p> On some systems, closing a channel releases all locks held by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * virtual machine on the underlying file regardless of whether the locks were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * acquired via that channel or via another channel open on the same file.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * is strongly recommended that, within a program, a unique channel be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * acquire all locks on any given file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <p> Some network filesystems permit file locking to be used with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * memory-mapped files only when the locked regions are page-aligned and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * whole multiple of the underlying hardware's page size.  Some network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * filesystems do not implement file locks on regions that extend past a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * certain position, often 2<sup>30</sup> or 2<sup>31</sup>.  In general, great
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * care should be taken when locking files that reside on network filesystems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
public abstract class FileLock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private final FileChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private final long position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private final long size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private final boolean shared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Initializes a new instance of this class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @param  channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *         The file channel upon whose file this lock is held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @param  position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *         The position within the file at which the locked region starts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *         must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param  size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *         The size of the locked region; must be non-negative, and the sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param  shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *         <tt>true</tt> if this lock is shared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *         <tt>false</tt> if it is exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *         If the preconditions on the parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    protected FileLock(FileChannel channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                       long position, long size, boolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (position < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new IllegalArgumentException("Negative position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new IllegalArgumentException("Negative size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (position + size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalArgumentException("Negative position + size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        this.position = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.shared = shared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Returns the file channel upon whose file this lock is held.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return  The file channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public final FileChannel channel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Returns the position within the file of the first byte of the locked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <p> A locked region need not be contained within, or even overlap, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * actual underlying file, so the value returned by this method may exceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * the file's current size.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @return  The position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public final long position() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Returns the size of the locked region in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p> A locked region need not be contained within, or even overlap, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * actual underlying file, so the value returned by this method may exceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * the file's current size.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return  The size of the locked region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public final long size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Tells whether this lock is shared.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return <tt>true</tt> if lock is shared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *         <tt>false</tt> if it is exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public final boolean isShared() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return shared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Tells whether or not this lock overlaps the given lock range.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * @return  <tt>true</tt> if, and only if, this lock and the given lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     *          range overlap by at least one byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    public final boolean overlaps(long position, long size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (position + size <= this.position)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            return false;               // That is below this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (this.position + this.size <= position)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            return false;               // This is below that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * Tells whether or not this lock is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <p> A lock object remains valid until it is released or the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * file channel is closed, whichever comes first.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @return  <tt>true</tt> if, and only if, this lock is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public abstract boolean isValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Releases this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <p> If this lock object is valid then invoking this method releases the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * lock and renders the object invalid.  If this lock object is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * then invoking this method has no effect.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *          If the channel that was used to acquire this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *          is no longer open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public abstract void release() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Returns a string describing the range, type, and validity of this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @return  A descriptive string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return (this.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                + "[" + position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                + ":" + size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                + " " + (shared ? "shared" : "exclusive")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                + " " + (isValid() ? "valid" : "invalid")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
}