@@ -160,7 +160,7 @@ protected FileLock(FileChannel channel,
160160 throw new IllegalArgumentException ("Negative position + size" );
161161 this .channel = channel ;
162162 this .position = position ;
163- this .size = size ;
163+ this .size = size == 0 ? Long . MAX_VALUE - position : size ;
164164 this .shared = shared ;
165165 }
166166
@@ -202,7 +202,7 @@ protected FileLock(AsynchronousFileChannel channel,
202202 throw new IllegalArgumentException ("Negative position + size" );
203203 this .channel = channel ;
204204 this .position = position ;
205- this .size = size ;
205+ this .size = size == 0 ? Long . MAX_VALUE - position : size ;
206206 this .shared = shared ;
207207 }
208208
@@ -284,11 +284,17 @@ public final boolean isShared() {
284284 * least one byte
285285 */
286286 public final boolean overlaps (long position , long size ) {
287- System .out .printf ("%d %d %d %d%n" , this .position , this .size ,
288- position , size );
289287 if (size < 0 )
290288 return false ;
291289
290+ try {
291+ if (Math .addExact (this .position , this .size ) <= position )
292+ return false ; // This is below that
293+ } catch (ArithmeticException ignored ) {
294+ // the sum of this.position and this.size overflows the range of
295+ // long hence their mathematical sum is greater than position
296+ }
297+
292298 // if size == 0 then the specified lock range is unbounded and
293299 // cannot be below the range of this lock
294300 if (size > 0 ) {
@@ -301,18 +307,6 @@ public final boolean overlaps(long position, long size) {
301307 }
302308 }
303309
304- // if this.size == 0 then the range of this lock is unbounded and
305- // cannot be below the specified lock range
306- if (this .size > 0 ) {
307- try {
308- if (Math .addExact (this .position , this .size ) <= position )
309- return false ; // This is below that
310- } catch (ArithmeticException ignored ) {
311- // the sum of this.position and this.size overflows the range of
312- // long hence their mathematical sum is greater than position
313- }
314- }
315-
316310 return true ;
317311 }
318312
0 commit comments