jdk/src/share/classes/java/nio/channels/FileLock.java
author alanb
Sun, 15 Feb 2009 12:25:54 +0000
changeset 2057 3acf8e5e2ca0
parent 2 90ce3da70b43
child 3631 4dc04372d56b
permissions -rw-r--r--
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99 4313887: New I/O: Improved filesystem interface 4607272: New I/O: Support asynchronous I/O Reviewed-by: sherman, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
     2
 * Copyright 2001-2009 Sun Microsystems, Inc.  All Rights Reserved.
2
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
 * A token representing a lock on a region of a file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p> A file-lock object is created each time a lock is acquired on a file via
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * one of the {@link FileChannel#lock(long,long,boolean) lock} or {@link
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    35
 * FileChannel#tryLock(long,long,boolean) tryLock} methods of the
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    36
 * {@link FileChannel} class, or the {@link
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    37
 * AsynchronousFileChannel#lock(long,long,boolean,Object,CompletionHandler) lock}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    38
 * or {@link AsynchronousFileChannel#tryLock(long,long,boolean) tryLock}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    39
 * methods of the {@link AsynchronousFileChannel} class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p> A file-lock object is initially valid.  It remains valid until the lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * is released by invoking the {@link #release release} method, by closing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * channel that was used to acquire it, or by the termination of the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * virtual machine, whichever comes first.  The validity of a lock may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * tested by invoking its {@link #isValid isValid} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> A file lock is either <i>exclusive</i> or <i>shared</i>.  A shared lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * prevents other concurrently-running programs from acquiring an overlapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * exclusive lock, but does allow them to acquire overlapping shared locks.  An
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * exclusive lock prevents other programs from acquiring an overlapping lock of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * either type.  Once it is released, a lock has no further effect on the locks
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * that may be acquired by other programs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p> Whether a lock is exclusive or shared may be determined by invoking its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@link #isShared isShared} method.  Some platforms do not support shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * locks, in which case a request for a shared lock is automatically converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * into a request for an exclusive lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p> The locks held on a particular file by a single Java virtual machine do
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * not overlap.  The {@link #overlaps overlaps} method may be used to test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * whether a candidate lock range overlaps an existing lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p> A file-lock object records the file channel upon whose file the lock is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * held, the type and validity of the lock, and the position and size of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * locked region.  Only the validity of a lock is subject to change over time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * all other aspects of a lock's state are immutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p> File locks are held on behalf of the entire Java virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * They are not suitable for controlling access to a file by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * threads within the same virtual machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * <p> File-lock objects are safe for use by multiple concurrent threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    75
 * <a name="pdep"><h4> Platform dependencies </h4></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <p> This file-locking API is intended to map directly to the native locking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * facility of the underlying operating system.  Thus the locks held on a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * should be visible to all programs that have access to the file, regardless
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * of the language in which those programs are written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p> Whether or not a lock actually prevents another program from accessing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the content of the locked region is system-dependent and therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * unspecified.  The native file-locking facilities of some systems are merely
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <i>advisory</i>, meaning that programs must cooperatively observe a known
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * locking protocol in order to guarantee data integrity.  On other systems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * native file locks are <i>mandatory</i>, meaning that if one program locks a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * region of a file then other programs are actually prevented from accessing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * that region in a way that would violate the lock.  On yet other systems,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * whether native file locks are advisory or mandatory is configurable on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * per-file basis.  To ensure consistent and correct behavior across platforms,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * it is strongly recommended that the locks provided by this API be used as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * they were advisory locks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p> On some systems, acquiring a mandatory lock on a region of a file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * prevents that region from being {@link java.nio.channels.FileChannel#map
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
    97
 * <i>mapped into memory</i>}, and vice versa.  Programs that combine
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * locking and mapping should be prepared for this combination to fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p> On some systems, closing a channel releases all locks held by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * virtual machine on the underlying file regardless of whether the locks were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * acquired via that channel or via another channel open on the same file.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * is strongly recommended that, within a program, a unique channel be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * acquire all locks on any given file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * <p> Some network filesystems permit file locking to be used with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * memory-mapped files only when the locked regions are page-aligned and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * whole multiple of the underlying hardware's page size.  Some network
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * filesystems do not implement file locks on regions that extend past a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * certain position, often 2<sup>30</sup> or 2<sup>31</sup>.  In general, great
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * care should be taken when locking files that reside on network filesystems.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * @author Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @author JSR-51 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * @since 1.4
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   117
 * @updated 1.7
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
public abstract class FileLock {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   122
    private final Channel channel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private final long position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private final long size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private final boolean shared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Initializes a new instance of this class.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param  channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *         The file channel upon whose file this lock is held
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param  position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *         The position within the file at which the locked region starts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *         must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param  size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *         The size of the locked region; must be non-negative, and the sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param  shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *         <tt>true</tt> if this lock is shared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *         <tt>false</tt> if it is exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @throws IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *         If the preconditions on the parameters do not hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    protected FileLock(FileChannel channel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                       long position, long size, boolean shared)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (position < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            throw new IllegalArgumentException("Negative position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalArgumentException("Negative size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (position + size < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new IllegalArgumentException("Negative position + size");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.position = position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        this.shared = shared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   164
     * {@note new} Initializes a new instance of this class.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   165
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   166
     * @param  channel
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   167
     *         The channel upon whose file this lock is held
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   168
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   169
     * @param  position
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   170
     *         The position within the file at which the locked region starts;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   171
     *         must be non-negative
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   172
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   173
     * @param  size
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   174
     *         The size of the locked region; must be non-negative, and the sum
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   175
     *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   176
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   177
     * @param  shared
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   178
     *         <tt>true</tt> if this lock is shared,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   179
     *         <tt>false</tt> if it is exclusive
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   180
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   181
     * @throws IllegalArgumentException
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   182
     *         If the preconditions on the parameters do not hold
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     *
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   184
     * @since 1.7
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   185
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   186
    protected FileLock(AsynchronousFileChannel channel,
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   187
                       long position, long size, boolean shared)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   188
    {
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   189
        if (position < 0)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   190
            throw new IllegalArgumentException("Negative position");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   191
        if (size < 0)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   192
            throw new IllegalArgumentException("Negative size");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   193
        if (position + size < 0)
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   194
            throw new IllegalArgumentException("Negative position + size");
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   195
        this.channel = channel;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   196
        this.position = position;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   197
        this.size = size;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   198
        this.shared = shared;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   199
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   200
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   201
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   202
     * {@note revised}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   203
     * Returns the file channel upon whose file this lock was acquired.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   204
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   205
     * <p> This method has been superseded by the {@link #acquiredBy acquiredBy}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   206
     * method.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   207
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   208
     * @return  The file channel, or {@code null} if the file lock was not
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   209
     *          acquired by a file channel.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public final FileChannel channel() {
2057
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   212
        return (channel instanceof FileChannel) ? (FileChannel)channel : null;
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   213
    }
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   214
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   215
    /**
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   216
     * {@note new}
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   217
     * Returns the channel upon whose file this lock was acquired.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   218
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   219
     * @return  The channel upon whose file this lock was acquired.
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   220
     *
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   221
     * @since 1.7
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   222
     */
3acf8e5e2ca0 6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents: 2
diff changeset
   223
    public Channel acquiredBy() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * Returns the position within the file of the first byte of the locked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <p> A locked region need not be contained within, or even overlap, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * actual underlying file, so the value returned by this method may exceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * the file's current size.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return  The position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    public final long position() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Returns the size of the locked region in bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <p> A locked region need not be contained within, or even overlap, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * actual underlying file, so the value returned by this method may exceed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * the file's current size.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return  The size of the locked region
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public final long size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Tells whether this lock is shared.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @return <tt>true</tt> if lock is shared,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *         <tt>false</tt> if it is exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public final boolean isShared() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return shared;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Tells whether or not this lock overlaps the given lock range.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return  <tt>true</tt> if, and only if, this lock and the given lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *          range overlap by at least one byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public final boolean overlaps(long position, long size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if (position + size <= this.position)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return false;               // That is below this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        if (this.position + this.size <= position)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            return false;               // This is below that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Tells whether or not this lock is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <p> A lock object remains valid until it is released or the associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * file channel is closed, whichever comes first.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @return  <tt>true</tt> if, and only if, this lock is valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public abstract boolean isValid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * Releases this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p> If this lock object is valid then invoking this method releases the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * lock and renders the object invalid.  If this lock object is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * then invoking this method has no effect.  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @throws  ClosedChannelException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *          If the channel that was used to acquire this lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *          is no longer open
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    public abstract void release() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * Returns a string describing the range, type, and validity of this lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @return  A descriptive string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public final String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return (this.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                + "[" + position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                + ":" + size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                + " " + (shared ? "shared" : "exclusive")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                + " " + (isValid() ? "valid" : "invalid")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
}