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