src/java.base/share/classes/java/nio/channels/FileLock.java

Print this page
rev 11253 : 6880737: (fs) FileLock constructors don't throw NPE if the channel argument is null
Summary: Throw IllegalArgumentException if the channel parameter is null
Reviewed-by: TBD


 110  * certain position, often 2<sup>30</sup> or 2<sup>31</sup>.  In general, great
 111  * care should be taken when locking files that reside on network filesystems.
 112  *
 113  *
 114  * @author Mark Reinhold
 115  * @author JSR-51 Expert Group
 116  * @since 1.4
 117  */
 118 
 119 public abstract class FileLock implements AutoCloseable {
 120 
 121     private final Channel channel;
 122     private final long position;
 123     private final long size;
 124     private final boolean shared;
 125 
 126     /**
 127      * Initializes a new instance of this class.
 128      *
 129      * @param  channel
 130      *         The file channel upon whose file this lock is held

 131      *
 132      * @param  position
 133      *         The position within the file at which the locked region starts;
 134      *         must be non-negative
 135      *
 136      * @param  size
 137      *         The size of the locked region; must be non-negative, and the sum
 138      *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
 139      *
 140      * @param  shared
 141      *         <tt>true</tt> if this lock is shared,
 142      *         <tt>false</tt> if it is exclusive
 143      *
 144      * @throws IllegalArgumentException
 145      *         If the preconditions on the parameters do not hold
 146      */
 147     protected FileLock(FileChannel channel,
 148                        long position, long size, boolean shared)
 149     {


 150         if (position < 0)
 151             throw new IllegalArgumentException("Negative position");
 152         if (size < 0)
 153             throw new IllegalArgumentException("Negative size");
 154         if (position + size < 0)
 155             throw new IllegalArgumentException("Negative position + size");
 156         this.channel = channel;
 157         this.position = position;
 158         this.size = size;
 159         this.shared = shared;
 160     }
 161 
 162     /**
 163      * Initializes a new instance of this class.
 164      *
 165      * @param  channel
 166      *         The channel upon whose file this lock is held

 167      *
 168      * @param  position
 169      *         The position within the file at which the locked region starts;
 170      *         must be non-negative
 171      *
 172      * @param  size
 173      *         The size of the locked region; must be non-negative, and the sum
 174      *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
 175      *
 176      * @param  shared
 177      *         <tt>true</tt> if this lock is shared,
 178      *         <tt>false</tt> if it is exclusive
 179      *
 180      * @throws IllegalArgumentException
 181      *         If the preconditions on the parameters do not hold
 182      *
 183      * @since 1.7
 184      */
 185     protected FileLock(AsynchronousFileChannel channel,
 186                        long position, long size, boolean shared)
 187     {


 188         if (position < 0)
 189             throw new IllegalArgumentException("Negative position");
 190         if (size < 0)
 191             throw new IllegalArgumentException("Negative size");
 192         if (position + size < 0)
 193             throw new IllegalArgumentException("Negative position + size");
 194         this.channel = channel;
 195         this.position = position;
 196         this.size = size;
 197         this.shared = shared;
 198     }
 199 
 200     /**
 201      * Returns the file channel upon whose file this lock was acquired.
 202      *
 203      * <p> This method has been superseded by the {@link #acquiredBy acquiredBy}
 204      * method.
 205      *
 206      * @return  The file channel, or {@code null} if the file lock was not
 207      *          acquired by a file channel.




 110  * certain position, often 2<sup>30</sup> or 2<sup>31</sup>.  In general, great
 111  * care should be taken when locking files that reside on network filesystems.
 112  *
 113  *
 114  * @author Mark Reinhold
 115  * @author JSR-51 Expert Group
 116  * @since 1.4
 117  */
 118 
 119 public abstract class FileLock implements AutoCloseable {
 120 
 121     private final Channel channel;
 122     private final long position;
 123     private final long size;
 124     private final boolean shared;
 125 
 126     /**
 127      * Initializes a new instance of this class.
 128      *
 129      * @param  channel
 130      *         The file channel upon whose file this lock is held; must be
 131      *         non-{@code null}
 132      *
 133      * @param  position
 134      *         The position within the file at which the locked region starts;
 135      *         must be non-negative
 136      *
 137      * @param  size
 138      *         The size of the locked region; must be non-negative, and the sum
 139      *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
 140      *
 141      * @param  shared
 142      *         <tt>true</tt> if this lock is shared,
 143      *         <tt>false</tt> if it is exclusive
 144      *
 145      * @throws IllegalArgumentException
 146      *         If the preconditions on the parameters do not hold
 147      */
 148     protected FileLock(FileChannel channel,
 149                        long position, long size, boolean shared)
 150     {
 151         if (channel == null)
 152             throw new IllegalArgumentException("Null channel");
 153         if (position < 0)
 154             throw new IllegalArgumentException("Negative position");
 155         if (size < 0)
 156             throw new IllegalArgumentException("Negative size");
 157         if (position + size < 0)
 158             throw new IllegalArgumentException("Negative position + size");
 159         this.channel = channel;
 160         this.position = position;
 161         this.size = size;
 162         this.shared = shared;
 163     }
 164 
 165     /**
 166      * Initializes a new instance of this class.
 167      *
 168      * @param  channel
 169      *         The channel upon whose file this lock is held; must be
 170      *         non-{@code null}
 171      *
 172      * @param  position
 173      *         The position within the file at which the locked region starts;
 174      *         must be non-negative
 175      *
 176      * @param  size
 177      *         The size of the locked region; must be non-negative, and the sum
 178      *         <tt>position</tt>&nbsp;+&nbsp;<tt>size</tt> must be non-negative
 179      *
 180      * @param  shared
 181      *         <tt>true</tt> if this lock is shared,
 182      *         <tt>false</tt> if it is exclusive
 183      *
 184      * @throws IllegalArgumentException
 185      *         If the preconditions on the parameters do not hold
 186      *
 187      * @since 1.7
 188      */
 189     protected FileLock(AsynchronousFileChannel channel,
 190                        long position, long size, boolean shared)
 191     {
 192         if (channel == null)
 193             throw new IllegalArgumentException("Null channel");
 194         if (position < 0)
 195             throw new IllegalArgumentException("Negative position");
 196         if (size < 0)
 197             throw new IllegalArgumentException("Negative size");
 198         if (position + size < 0)
 199             throw new IllegalArgumentException("Negative position + size");
 200         this.channel = channel;
 201         this.position = position;
 202         this.size = size;
 203         this.shared = shared;
 204     }
 205 
 206     /**
 207      * Returns the file channel upon whose file this lock was acquired.
 208      *
 209      * <p> This method has been superseded by the {@link #acquiredBy acquiredBy}
 210      * method.
 211      *
 212      * @return  The file channel, or {@code null} if the file lock was not
 213      *          acquired by a file channel.