Skip to content

Commit d1001cc

Browse files
committed
5041655: Move size zero handling to file channel impl classes
1 parent 30ce90c commit d1001cc

File tree

6 files changed

+35
-26
lines changed

6 files changed

+35
-26
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2007, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -454,7 +454,8 @@ public static AsynchronousFileChannel open(Path file, OpenOption... options)
454454
* non-negative
455455
* @param size
456456
* The size of the locked region; must be non-negative, and the sum
457-
* {@code position} + {@code size} must be non-negative
457+
* {@code position} + {@code size} must be non-negative.
458+
* A value of zero indicates the remainder of the file.
458459
* @param shared
459460
* {@code true} to request a shared lock, in which case this
460461
* channel must be open for reading (and possibly writing);
@@ -532,7 +533,8 @@ public final <A> void lock(A attachment,
532533
* non-negative
533534
* @param size
534535
* The size of the locked region; must be non-negative, and the sum
535-
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative
536+
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative.
537+
* A value of zero indicates the remainder of the file.
536538
* @param shared
537539
* {@code true} to request a shared lock, in which case this
538540
* channel must be open for reading (and possibly writing);
@@ -594,7 +596,8 @@ public final Future<FileLock> lock() {
594596
*
595597
* @param size
596598
* The size of the locked region; must be non-negative, and the sum
597-
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative
599+
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative.
600+
* A value of zero indicates the remainder of the file.
598601
*
599602
* @param shared
600603
* {@code true} to request a shared lock,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,6 @@ public abstract class FileLock implements AutoCloseable {
127127
/**
128128
* Initializes a new instance of this class.
129129
*
130-
* <p> If {@code size} is zero, the entire file from {@code position}
131-
* onward is locked.
132-
*
133130
* @param channel
134131
* The file channel upon whose file this lock is held
135132
*
@@ -139,7 +136,8 @@ public abstract class FileLock implements AutoCloseable {
139136
*
140137
* @param size
141138
* The size of the locked region; must be non-negative, and the sum
142-
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative
139+
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative.
140+
* A value of zero indicates the remainder of the file.
143141
*
144142
* @param shared
145143
* {@code true} if this lock is shared,
@@ -160,16 +158,13 @@ protected FileLock(FileChannel channel,
160158
throw new IllegalArgumentException("Negative position + size");
161159
this.channel = channel;
162160
this.position = position;
163-
this.size = size == 0 ? Long.MAX_VALUE - position : size;
161+
this.size = size;
164162
this.shared = shared;
165163
}
166164

167165
/**
168166
* Initializes a new instance of this class.
169167
*
170-
* <p> If {@code size} is zero, the entire file from {@code position}
171-
* onward is locked.
172-
*
173168
* @param channel
174169
* The channel upon whose file this lock is held
175170
*
@@ -179,7 +174,8 @@ protected FileLock(FileChannel channel,
179174
*
180175
* @param size
181176
* The size of the locked region; must be non-negative, and the sum
182-
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative
177+
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative.
178+
* A value of zero indicates the remainder of the file.
183179
*
184180
* @param shared
185181
* {@code true} if this lock is shared,
@@ -202,7 +198,7 @@ protected FileLock(AsynchronousFileChannel channel,
202198
throw new IllegalArgumentException("Negative position + size");
203199
this.channel = channel;
204200
this.position = position;
205-
this.size = size == 0 ? Long.MAX_VALUE - position : size;
201+
this.size = size;
206202
this.shared = shared;
207203
}
208204

@@ -271,17 +267,16 @@ public final boolean isShared() {
271267
* Tells whether or not this lock overlaps the given lock range.
272268
*
273269
* <p> If {@code size} is negative, {@code false} is returned regardless
274-
* of the value of {@code position}. If {@code size} is zero, it means the
275-
* lock range is unbounded.
270+
* of the value of {@code position}. A {@code size} of zero indicates the
271+
* remainder of the file.
276272
*
277273
* @param position
278274
* The starting position of the lock range
279275
* @param size
280276
* The size of the lock range
281277
*
282-
* @return {@code false} if, and only if, {@code size} is negative or this
283-
* lock and the given lock range do <em>not</em> overlap by at
284-
* least one byte
278+
* @return {@code true} if, and only if, this lock and the given lock
279+
* range overlap by at least one byte
285280
*/
286281
public final boolean overlaps(long position, long size) {
287282
if (size < 0)

src/java.base/share/classes/sun/nio/ch/FileChannelImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1271,6 +1271,8 @@ public FileLock lock(long position, long size, boolean shared)
12711271
throw new NonReadableChannelException();
12721272
if (!shared && !writable)
12731273
throw new NonWritableChannelException();
1274+
if (size == 0)
1275+
size = Long.MAX_VALUE - Math.max(0, position);
12741276
FileLockImpl fli = new FileLockImpl(this, position, size, shared);
12751277
FileLockTable flt = fileLockTable();
12761278
flt.add(fli);
@@ -1316,6 +1318,8 @@ public FileLock tryLock(long position, long size, boolean shared)
13161318
throw new NonReadableChannelException();
13171319
if (!shared && !writable)
13181320
throw new NonWritableChannelException();
1321+
if (size == 0)
1322+
size = Long.MAX_VALUE - Math.max(0, position);
13191323
FileLockImpl fli = new FileLockImpl(this, position, size, shared);
13201324
FileLockTable flt = fileLockTable();
13211325
flt.add(fli);

src/java.base/share/classes/sun/nio/ch/SimpleAsynchronousFileChannelImpl.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -181,8 +181,10 @@ <A> Future<FileLock> implLock(final long position,
181181
if (!shared && !writing)
182182
throw new NonWritableChannelException();
183183

184+
long len = size != 0 ? size : Long.MAX_VALUE - Math.max(0, position);
185+
184186
// add to lock table
185-
final FileLockImpl fli = addToFileLockTable(position, size, shared);
187+
final FileLockImpl fli = addToFileLockTable(position, len, shared);
186188
if (fli == null) {
187189
Throwable exc = new ClosedChannelException();
188190
if (handler == null)
@@ -203,7 +205,7 @@ public void run() {
203205
try {
204206
begin();
205207
do {
206-
n = nd.lock(fdObj, true, position, size, shared);
208+
n = nd.lock(fdObj, true, position, len, shared);
207209
} while ((n == FileDispatcher.INTERRUPTED) && isOpen());
208210
if (n != FileDispatcher.LOCKED || !isOpen()) {
209211
throw new AsynchronousCloseException();
@@ -248,6 +250,9 @@ public FileLock tryLock(long position, long size, boolean shared)
248250
if (!shared && !writing)
249251
throw new NonWritableChannelException();
250252

253+
if (size == 0)
254+
size = Long.MAX_VALUE - Math.max(0, position);
255+
251256
// add to lock table
252257
FileLockImpl fli = addToFileLockTable(position, size, shared);
253258
if (fli == null)

src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousFileChannelImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2008, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -299,8 +299,10 @@ <A> Future<FileLock> implLock(final long position,
299299
if (!shared && !writing)
300300
throw new NonWritableChannelException();
301301

302+
long len = size != 0 ? size : Long.MAX_VALUE - Math.max(0, position);
303+
302304
// add to lock table
303-
FileLockImpl fli = addToFileLockTable(position, size, shared);
305+
FileLockImpl fli = addToFileLockTable(position, len, shared);
304306
if (fli == null) {
305307
Throwable exc = new ClosedChannelException();
306308
if (handler == null)

0 commit comments

Comments
 (0)