jdk/src/share/classes/java/nio/channels/FileLock.java
changeset 2057 3acf8e5e2ca0
parent 2 90ce3da70b43
child 3631 4dc04372d56b
equal deleted inserted replaced
2056:115e09b7a004 2057:3acf8e5e2ca0
     1 /*
     1 /*
     2  * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
     2  * Copyright 2001-2009 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     7  * published by the Free Software Foundation.  Sun designates this
    25 
    25 
    26 package java.nio.channels;
    26 package java.nio.channels;
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
    29 
    29 
    30 
       
    31 /**
    30 /**
    32  * A token representing a lock on a region of a file.
    31  * A token representing a lock on a region of a file.
    33  *
    32  *
    34  * <p> A file-lock object is created each time a lock is acquired on a file via
    33  * <p> A file-lock object is created each time a lock is acquired on a file via
    35  * one of the {@link FileChannel#lock(long,long,boolean) lock} or {@link
    34  * one of the {@link FileChannel#lock(long,long,boolean) lock} or {@link
    36  * FileChannel#tryLock(long,long,boolean) tryLock} methods of the {@link
    35  * FileChannel#tryLock(long,long,boolean) tryLock} methods of the
    37  * FileChannel} class.
    36  * {@link FileChannel} class, or the {@link
       
    37  * AsynchronousFileChannel#lock(long,long,boolean,Object,CompletionHandler) lock}
       
    38  * or {@link AsynchronousFileChannel#tryLock(long,long,boolean) tryLock}
       
    39  * methods of the {@link AsynchronousFileChannel} class.
    38  *
    40  *
    39  * <p> A file-lock object is initially valid.  It remains valid until the lock
    41  * <p> A file-lock object is initially valid.  It remains valid until the lock
    40  * is released by invoking the {@link #release release} method, by closing the
    42  * is released by invoking the {@link #release release} method, by closing the
    41  * channel that was used to acquire it, or by the termination of the Java
    43  * channel that was used to acquire it, or by the termination of the Java
    42  * virtual machine, whichever comes first.  The validity of a lock may be
    44  * virtual machine, whichever comes first.  The validity of a lock may be
    68  * threads within the same virtual machine.
    70  * threads within the same virtual machine.
    69  *
    71  *
    70  * <p> File-lock objects are safe for use by multiple concurrent threads.
    72  * <p> File-lock objects are safe for use by multiple concurrent threads.
    71  *
    73  *
    72  *
    74  *
    73  * <a name="pdep">
    75  * <a name="pdep"><h4> Platform dependencies </h4></a>
    74  * <h4> Platform dependencies </h4>
       
    75  *
    76  *
    76  * <p> This file-locking API is intended to map directly to the native locking
    77  * <p> This file-locking API is intended to map directly to the native locking
    77  * facility of the underlying operating system.  Thus the locks held on a file
    78  * facility of the underlying operating system.  Thus the locks held on a file
    78  * should be visible to all programs that have access to the file, regardless
    79  * should be visible to all programs that have access to the file, regardless
    79  * of the language in which those programs are written.
    80  * of the language in which those programs are written.
    91  * it is strongly recommended that the locks provided by this API be used as if
    92  * it is strongly recommended that the locks provided by this API be used as if
    92  * they were advisory locks.
    93  * they were advisory locks.
    93  *
    94  *
    94  * <p> On some systems, acquiring a mandatory lock on a region of a file
    95  * <p> On some systems, acquiring a mandatory lock on a region of a file
    95  * prevents that region from being {@link java.nio.channels.FileChannel#map
    96  * prevents that region from being {@link java.nio.channels.FileChannel#map
    96  * </code>mapped into memory<code>}, and vice versa.  Programs that combine
    97  * <i>mapped into memory</i>}, and vice versa.  Programs that combine
    97  * locking and mapping should be prepared for this combination to fail.
    98  * locking and mapping should be prepared for this combination to fail.
    98  *
    99  *
    99  * <p> On some systems, closing a channel releases all locks held by the Java
   100  * <p> On some systems, closing a channel releases all locks held by the Java
   100  * virtual machine on the underlying file regardless of whether the locks were
   101  * virtual machine on the underlying file regardless of whether the locks were
   101  * acquired via that channel or via another channel open on the same file.  It
   102  * acquired via that channel or via another channel open on the same file.  It
   111  *
   112  *
   112  *
   113  *
   113  * @author Mark Reinhold
   114  * @author Mark Reinhold
   114  * @author JSR-51 Expert Group
   115  * @author JSR-51 Expert Group
   115  * @since 1.4
   116  * @since 1.4
       
   117  * @updated 1.7
   116  */
   118  */
   117 
   119 
   118 public abstract class FileLock {
   120 public abstract class FileLock {
   119 
   121 
   120     private final FileChannel channel;
   122     private final Channel channel;
   121     private final long position;
   123     private final long position;
   122     private final long size;
   124     private final long size;
   123     private final boolean shared;
   125     private final boolean shared;
   124 
   126 
   125     /**
   127     /**
   157         this.size = size;
   159         this.size = size;
   158         this.shared = shared;
   160         this.shared = shared;
   159     }
   161     }
   160 
   162 
   161     /**
   163     /**
   162      * Returns the file channel upon whose file this lock is held.  </p>
   164      * {@note new} Initializes a new instance of this class.
   163      *
   165      *
   164      * @return  The file channel
   166      * @param  channel
       
   167      *         The channel upon whose file this lock is held
       
   168      *
       
   169      * @param  position
       
   170      *         The position within the file at which the locked region starts;
       
   171      *         must be non-negative
       
   172      *
       
   173      * @param  size
       
   174      *         The size of the locked region; must be non-negative, and the sum
       
   175      *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
       
   176      *
       
   177      * @param  shared
       
   178      *         <tt>true</tt> if this lock is shared,
       
   179      *         <tt>false</tt> if it is exclusive
       
   180      *
       
   181      * @throws IllegalArgumentException
       
   182      *         If the preconditions on the parameters do not hold
       
   183      *
       
   184      * @since 1.7
       
   185      */
       
   186     protected FileLock(AsynchronousFileChannel channel,
       
   187                        long position, long size, boolean shared)
       
   188     {
       
   189         if (position < 0)
       
   190             throw new IllegalArgumentException("Negative position");
       
   191         if (size < 0)
       
   192             throw new IllegalArgumentException("Negative size");
       
   193         if (position + size < 0)
       
   194             throw new IllegalArgumentException("Negative position + size");
       
   195         this.channel = channel;
       
   196         this.position = position;
       
   197         this.size = size;
       
   198         this.shared = shared;
       
   199     }
       
   200 
       
   201     /**
       
   202      * {@note revised}
       
   203      * Returns the file channel upon whose file this lock was acquired.
       
   204      *
       
   205      * <p> This method has been superseded by the {@link #acquiredBy acquiredBy}
       
   206      * method.
       
   207      *
       
   208      * @return  The file channel, or {@code null} if the file lock was not
       
   209      *          acquired by a file channel.
   165      */
   210      */
   166     public final FileChannel channel() {
   211     public final FileChannel channel() {
       
   212         return (channel instanceof FileChannel) ? (FileChannel)channel : null;
       
   213     }
       
   214 
       
   215     /**
       
   216      * {@note new}
       
   217      * Returns the channel upon whose file this lock was acquired.
       
   218      *
       
   219      * @return  The channel upon whose file this lock was acquired.
       
   220      *
       
   221      * @since 1.7
       
   222      */
       
   223     public Channel acquiredBy() {
   167         return channel;
   224         return channel;
   168     }
   225     }
   169 
   226 
   170     /**
   227     /**
   171      * Returns the position within the file of the first byte of the locked
   228      * Returns the position within the file of the first byte of the locked