Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/share/classes/java/nio/channels/AsynchronousFileChannel.java
41159 views
1
/*
2
* Copyright (c) 2007, 2018, 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.nio.file.*;
29
import java.nio.file.attribute.FileAttribute;
30
import java.nio.file.spi.*;
31
import java.nio.ByteBuffer;
32
import java.io.IOException;
33
import java.util.concurrent.Future;
34
import java.util.concurrent.ExecutorService;
35
import java.util.Set;
36
import java.util.HashSet;
37
import java.util.Collections;
38
39
/**
40
* An asynchronous channel for reading, writing, and manipulating a file.
41
*
42
* <p> An asynchronous file channel is created when a file is opened by invoking
43
* one of the {@link #open open} methods defined by this class. The file contains
44
* a variable-length sequence of bytes that can be read and written and whose
45
* current size can be {@link #size() queried}. The size of the file increases
46
* when bytes are written beyond its current size; the size of the file decreases
47
* when it is {@link #truncate truncated}.
48
*
49
* <p> An asynchronous file channel does not have a <i>current position</i>
50
* within the file. Instead, the file position is specified to each read and
51
* write method that initiates asynchronous operations. A {@link CompletionHandler}
52
* is specified as a parameter and is invoked to consume the result of the I/O
53
* operation. This class also defines read and write methods that initiate
54
* asynchronous operations, returning a {@link Future} to represent the pending
55
* result of the operation. The {@code Future} may be used to check if the
56
* operation has completed, wait for its completion, and retrieve the result.
57
*
58
* <p> In addition to read and write operations, this class defines the
59
* following operations: </p>
60
*
61
* <ul>
62
*
63
* <li><p> Updates made to a file may be {@link #force <i>forced
64
* out</i>} to the underlying storage device, ensuring that data are not
65
* lost in the event of a system crash. </p></li>
66
*
67
* <li><p> A region of a file may be {@link #lock <i>locked</i>} against
68
* access by other programs. </p></li>
69
*
70
* </ul>
71
*
72
* <p> An {@code AsynchronousFileChannel} is associated with a thread pool to
73
* which tasks are submitted to handle I/O events and dispatch to completion
74
* handlers that consume the results of I/O operations on the channel. The
75
* completion handler for an I/O operation initiated on a channel is guaranteed
76
* to be invoked by one of the threads in the thread pool (This ensures that the
77
* completion handler is run by a thread with the expected <em>identity</em>).
78
* Where an I/O operation completes immediately, and the initiating thread is
79
* itself a thread in the thread pool, then the completion handler may be invoked
80
* directly by the initiating thread. When an {@code AsynchronousFileChannel} is
81
* created without specifying a thread pool then the channel is associated with
82
* a system-dependent default thread pool that may be shared with other
83
* channels. The default thread pool is configured by the system properties
84
* defined by the {@link AsynchronousChannelGroup} class.
85
*
86
* <p> Channels of this type are safe for use by multiple concurrent threads. The
87
* {@link Channel#close close} method may be invoked at any time, as specified
88
* by the {@link Channel} interface. This causes all outstanding asynchronous
89
* operations on the channel to complete with the exception {@link
90
* AsynchronousCloseException}. Multiple read and write operations may be
91
* outstanding at the same time. When multiple read and write operations are
92
* outstanding then the ordering of the I/O operations, and the order that the
93
* completion handlers are invoked, is not specified; they are not, in particular,
94
* guaranteed to execute in the order that the operations were initiated. The
95
* {@link java.nio.ByteBuffer ByteBuffers} used when reading or writing are not
96
* safe for use by multiple concurrent I/O operations. Furthermore, after an I/O
97
* operation is initiated then care should be taken to ensure that the buffer is
98
* not accessed until after the operation has completed.
99
*
100
* <p> As with {@link FileChannel}, the view of a file provided by an instance of
101
* this class is guaranteed to be consistent with other views of the same file
102
* provided by other instances in the same program. The view provided by an
103
* instance of this class may or may not, however, be consistent with the views
104
* seen by other concurrently-running programs due to caching performed by the
105
* underlying operating system and delays induced by network-filesystem protocols.
106
* This is true regardless of the language in which these other programs are
107
* written, and whether they are running on the same machine or on some other
108
* machine. The exact nature of any such inconsistencies are system-dependent
109
* and are therefore unspecified.
110
*
111
* @since 1.7
112
*/
113
114
public abstract class AsynchronousFileChannel
115
implements AsynchronousChannel
116
{
117
/**
118
* Initializes a new instance of this class.
119
*/
120
protected AsynchronousFileChannel() {
121
}
122
123
/**
124
* Opens or creates a file for reading and/or writing, returning an
125
* asynchronous file channel to access the file.
126
*
127
* <p> The {@code options} parameter determines how the file is opened.
128
* The {@link StandardOpenOption#READ READ} and {@link StandardOpenOption#WRITE
129
* WRITE} options determines if the file should be opened for reading and/or
130
* writing. If neither option is contained in the array then an existing file
131
* is opened for reading.
132
*
133
* <p> In addition to {@code READ} and {@code WRITE}, the following options
134
* may be present:
135
*
136
* <table class="striped">
137
* <caption style="display:none">additional options</caption>
138
* <thead>
139
* <tr> <th scope="col">Option</th> <th scope="col">Description</th> </tr>
140
* </thead>
141
* <tbody>
142
* <tr>
143
* <th scope="row"> {@link StandardOpenOption#TRUNCATE_EXISTING TRUNCATE_EXISTING} </th>
144
* <td> When opening an existing file, the file is first truncated to a
145
* size of 0 bytes. This option is ignored when the file is opened only
146
* for reading.</td>
147
* </tr>
148
* <tr>
149
* <th scope="row"> {@link StandardOpenOption#CREATE_NEW CREATE_NEW} </th>
150
* <td> If this option is present then a new file is created, failing if
151
* the file already exists. When creating a file the check for the
152
* existence of the file and the creation of the file if it does not exist
153
* is atomic with respect to other file system operations. This option is
154
* ignored when the file is opened only for reading. </td>
155
* </tr>
156
* <tr>
157
* <th scope="row" > {@link StandardOpenOption#CREATE CREATE} </th>
158
* <td> If this option is present then an existing file is opened if it
159
* exists, otherwise a new file is created. When creating a file the check
160
* for the existence of the file and the creation of the file if it does
161
* not exist is atomic with respect to other file system operations. This
162
* option is ignored if the {@code CREATE_NEW} option is also present or
163
* the file is opened only for reading. </td>
164
* </tr>
165
* <tr>
166
* <th scope="row" > {@link StandardOpenOption#DELETE_ON_CLOSE DELETE_ON_CLOSE} </th>
167
* <td> When this option is present then the implementation makes a
168
* <em>best effort</em> attempt to delete the file when closed by
169
* the {@link #close close} method. If the {@code close} method is not
170
* invoked then a <em>best effort</em> attempt is made to delete the file
171
* when the Java virtual machine terminates. </td>
172
* </tr>
173
* <tr>
174
* <th scope="row">{@link StandardOpenOption#SPARSE SPARSE} </th>
175
* <td> When creating a new file this option is a <em>hint</em> that the
176
* new file will be sparse. This option is ignored when not creating
177
* a new file. </td>
178
* </tr>
179
* <tr>
180
* <th scope="row"> {@link StandardOpenOption#SYNC SYNC} </th>
181
* <td> Requires that every update to the file's content or metadata be
182
* written synchronously to the underlying storage device. (see <a
183
* href="../file/package-summary.html#integrity"> Synchronized I/O file
184
* integrity</a>). </td>
185
* </tr>
186
* <tr>
187
* <th scope="row"> {@link StandardOpenOption#DSYNC DSYNC} </th>
188
* <td> Requires that every update to the file's content be written
189
* synchronously to the underlying storage device. (see <a
190
* href="../file/package-summary.html#integrity"> Synchronized I/O file
191
* integrity</a>). </td>
192
* </tr>
193
* </tbody>
194
* </table>
195
*
196
* <p> An implementation may also support additional options.
197
*
198
* <p> The {@code executor} parameter is the {@link ExecutorService} to
199
* which tasks are submitted to handle I/O events and dispatch completion
200
* results for operations initiated on resulting channel.
201
* The nature of these tasks is highly implementation specific and so care
202
* should be taken when configuring the {@code Executor}. Minimally it
203
* should support an unbounded work queue and should not run tasks on the
204
* caller thread of the {@link ExecutorService#execute execute} method.
205
* Shutting down the executor service while the channel is open results in
206
* unspecified behavior.
207
*
208
* <p> The {@code attrs} parameter is an optional array of file {@link
209
* FileAttribute file-attributes} to set atomically when creating the file.
210
*
211
* <p> The new channel is created by invoking the {@link
212
* FileSystemProvider#newAsynchronousFileChannel newAsynchronousFileChannel}
213
* method on the provider that created the {@code Path}.
214
*
215
* @param file
216
* The path of the file to open or create
217
* @param options
218
* Options specifying how the file is opened
219
* @param executor
220
* The thread pool or {@code null} to associate the channel with
221
* the default thread pool
222
* @param attrs
223
* An optional list of file attributes to set atomically when
224
* creating the file
225
*
226
* @return A new asynchronous file channel
227
*
228
* @throws IllegalArgumentException
229
* If the set contains an invalid combination of options
230
* @throws UnsupportedOperationException
231
* If the {@code file} is associated with a provider that does not
232
* support creating asynchronous file channels, or an unsupported
233
* open option is specified, or the array contains an attribute that
234
* cannot be set atomically when creating the file
235
* @throws FileAlreadyExistsException
236
* If a file of that name already exists and the {@link
237
* StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified
238
* and the file is being opened for writing
239
* <i>(<a href="../file/package-summary.html#optspecex">optional
240
* specific exception</a>)</i>
241
* @throws IOException
242
* If an I/O error occurs
243
* @throws SecurityException
244
* If a security manager is installed and it denies an
245
* unspecified permission required by the implementation.
246
* In the case of the default provider, the {@link
247
* SecurityManager#checkRead(String)} method is invoked to check
248
* read access if the file is opened for reading. The {@link
249
* SecurityManager#checkWrite(String)} method is invoked to check
250
* write access if the file is opened for writing
251
*/
252
public static AsynchronousFileChannel open(Path file,
253
Set<? extends OpenOption> options,
254
ExecutorService executor,
255
FileAttribute<?>... attrs)
256
throws IOException
257
{
258
FileSystemProvider provider = file.getFileSystem().provider();
259
return provider.newAsynchronousFileChannel(file, options, executor, attrs);
260
}
261
262
@SuppressWarnings({"unchecked", "rawtypes"}) // generic array construction
263
private static final FileAttribute<?>[] NO_ATTRIBUTES = new FileAttribute[0];
264
265
/**
266
* Opens or creates a file for reading and/or writing, returning an
267
* asynchronous file channel to access the file.
268
*
269
* <p> An invocation of this method behaves in exactly the same way as the
270
* invocation
271
* <pre>
272
* ch.{@link #open(Path,Set,ExecutorService,FileAttribute[])
273
* open}(file, opts, null, new FileAttribute&lt;?&gt;[0]);
274
* </pre>
275
* where {@code opts} is a {@code Set} containing the options specified to
276
* this method.
277
*
278
* <p> The resulting channel is associated with default thread pool to which
279
* tasks are submitted to handle I/O events and dispatch to completion
280
* handlers that consume the result of asynchronous operations performed on
281
* the resulting channel.
282
*
283
* @param file
284
* The path of the file to open or create
285
* @param options
286
* Options specifying how the file is opened
287
*
288
* @return A new asynchronous file channel
289
*
290
* @throws IllegalArgumentException
291
* If the set contains an invalid combination of options
292
* @throws UnsupportedOperationException
293
* If the {@code file} is associated with a provider that does not
294
* support creating file channels, or an unsupported open option is
295
* specified
296
* @throws FileAlreadyExistsException
297
* If a file of that name already exists and the {@link
298
* StandardOpenOption#CREATE_NEW CREATE_NEW} option is specified
299
* and the file is being opened for writing
300
* <i>(<a href="../file/package-summary.html#optspecex">optional
301
* specific exception</a>)</i>
302
* @throws IOException
303
* If an I/O error occurs
304
* @throws SecurityException
305
* If a security manager is installed and it denies an
306
* unspecified permission required by the implementation.
307
* In the case of the default provider, the {@link
308
* SecurityManager#checkRead(String)} method is invoked to check
309
* read access if the file is opened for reading. The {@link
310
* SecurityManager#checkWrite(String)} method is invoked to check
311
* write access if the file is opened for writing
312
*/
313
public static AsynchronousFileChannel open(Path file, OpenOption... options)
314
throws IOException
315
{
316
Set<OpenOption> set;
317
if (options.length == 0) {
318
set = Collections.emptySet();
319
} else {
320
set = new HashSet<>();
321
Collections.addAll(set, options);
322
}
323
return open(file, set, null, NO_ATTRIBUTES);
324
}
325
326
/**
327
* Returns the current size of this channel's file.
328
*
329
* @return The current size of this channel's file, measured in bytes
330
*
331
* @throws ClosedChannelException
332
* If this channel is closed
333
* @throws IOException
334
* If some other I/O error occurs
335
*/
336
public abstract long size() throws IOException;
337
338
/**
339
* Truncates this channel's file to the given size.
340
*
341
* <p> If the given size is less than the file's current size then the file
342
* is truncated, discarding any bytes beyond the new end of the file. If
343
* the given size is greater than or equal to the file's current size then
344
* the file is not modified. </p>
345
*
346
* @param size
347
* The new size, a non-negative byte count
348
*
349
* @return This file channel
350
*
351
* @throws NonWritableChannelException
352
* If this channel was not opened for writing
353
*
354
* @throws ClosedChannelException
355
* If this channel is closed
356
*
357
* @throws IllegalArgumentException
358
* If the new size is negative
359
*
360
* @throws IOException
361
* If some other I/O error occurs
362
*/
363
public abstract AsynchronousFileChannel truncate(long size) throws IOException;
364
365
/**
366
* Forces any updates to this channel's file to be written to the storage
367
* device that contains it.
368
*
369
* <p> If this channel's file resides on a local storage device then when
370
* this method returns it is guaranteed that all changes made to the file
371
* since this channel was created, or since this method was last invoked,
372
* will have been written to that device. This is useful for ensuring that
373
* critical information is not lost in the event of a system crash.
374
*
375
* <p> If the file does not reside on a local device then no such guarantee
376
* is made.
377
*
378
* <p> The {@code metaData} parameter can be used to limit the number of
379
* I/O operations that this method is required to perform. Passing
380
* {@code false} for this parameter indicates that only updates to the
381
* file's content need be written to storage; passing {@code true}
382
* indicates that updates to both the file's content and metadata must be
383
* written, which generally requires at least one more I/O operation.
384
* Whether this parameter actually has any effect is dependent upon the
385
* underlying operating system and is therefore unspecified.
386
*
387
* <p> Invoking this method may cause an I/O operation to occur even if the
388
* channel was only opened for reading. Some operating systems, for
389
* example, maintain a last-access time as part of a file's metadata, and
390
* this time is updated whenever the file is read. Whether or not this is
391
* actually done is system-dependent and is therefore unspecified.
392
*
393
* <p> This method is only guaranteed to force changes that were made to
394
* this channel's file via the methods defined in this class.
395
*
396
* @param metaData
397
* If {@code true} then this method is required to force changes
398
* to both the file's content and metadata to be written to
399
* storage; otherwise, it need only force content changes to be
400
* written
401
*
402
* @throws ClosedChannelException
403
* If this channel is closed
404
*
405
* @throws IOException
406
* If some other I/O error occurs
407
*/
408
public abstract void force(boolean metaData) throws IOException;
409
410
/**
411
* Acquires a lock on the given region of this channel's file.
412
*
413
* <p> This method initiates an operation to acquire a lock on the given
414
* region of this channel's file. The {@code handler} parameter is a
415
* completion handler that is invoked when the lock is acquired (or the
416
* operation fails). The result passed to the completion handler is the
417
* resulting {@code FileLock}.
418
*
419
* <p> The region specified by the {@code position} and {@code size}
420
* parameters need not be contained within, or even overlap, the actual
421
* underlying file. Lock regions are fixed in size; if a locked region
422
* initially contains the end of the file and the file grows beyond the
423
* region then the new portion of the file will not be covered by the lock.
424
* If a file is expected to grow in size and a lock on the entire file is
425
* required then a region starting at zero, and no smaller than the
426
* expected maximum size of the file, should be locked. The two-argument
427
* {@link #lock(Object,CompletionHandler)} method simply locks a region
428
* of size {@link Long#MAX_VALUE}. If a lock that overlaps the requested
429
* region is already held by this Java virtual machine, or this method has
430
* been invoked to lock an overlapping region and that operation has not
431
* completed, then this method throws {@link OverlappingFileLockException}.
432
*
433
* <p> Some operating systems do not support a mechanism to acquire a file
434
* lock in an asynchronous manner. Consequently an implementation may
435
* acquire the file lock in a background thread or from a task executed by
436
* a thread in the associated thread pool. If there are many lock operations
437
* outstanding then it may consume threads in the Java virtual machine for
438
* indefinite periods.
439
*
440
* <p> Some operating systems do not support shared locks, in which case a
441
* request for a shared lock is automatically converted into a request for
442
* an exclusive lock. Whether the newly-acquired lock is shared or
443
* exclusive may be tested by invoking the resulting lock object's {@link
444
* FileLock#isShared() isShared} method.
445
*
446
* <p> File locks are held on behalf of the entire Java virtual machine.
447
* They are not suitable for controlling access to a file by multiple
448
* threads within the same virtual machine.
449
*
450
* @param <A>
451
* The type of the attachment
452
* @param position
453
* The position at which the locked region is to start; must be
454
* non-negative
455
* @param size
456
* The size of the locked region; must be non-negative, and the sum
457
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative
458
* @param shared
459
* {@code true} to request a shared lock, in which case this
460
* channel must be open for reading (and possibly writing);
461
* {@code false} to request an exclusive lock, in which case this
462
* channel must be open for writing (and possibly reading)
463
* @param attachment
464
* The object to attach to the I/O operation; can be {@code null}
465
* @param handler
466
* The handler for consuming the result
467
*
468
* @throws OverlappingFileLockException
469
* If a lock that overlaps the requested region is already held by
470
* this Java virtual machine, or there is already a pending attempt
471
* to lock an overlapping region
472
* @throws IllegalArgumentException
473
* If the preconditions on the parameters do not hold
474
* @throws NonReadableChannelException
475
* If {@code shared} is true but this channel was not opened for reading
476
* @throws NonWritableChannelException
477
* If {@code shared} is false but this channel was not opened for writing
478
*/
479
public abstract <A> void lock(long position,
480
long size,
481
boolean shared,
482
A attachment,
483
CompletionHandler<FileLock,? super A> handler);
484
485
/**
486
* Acquires an exclusive lock on this channel's file.
487
*
488
* <p> This method initiates an operation to acquire a lock on the given
489
* region of this channel's file. The {@code handler} parameter is a
490
* completion handler that is invoked when the lock is acquired (or the
491
* operation fails). The result passed to the completion handler is the
492
* resulting {@code FileLock}.
493
*
494
* <p> An invocation of this method of the form {@code ch.lock(att,handler)}
495
* behaves in exactly the same way as the invocation
496
* <pre>
497
* ch.{@link #lock(long,long,boolean,Object,CompletionHandler) lock}(0L, Long.MAX_VALUE, false, att, handler)
498
* </pre>
499
*
500
* @param <A>
501
* The type of the attachment
502
* @param attachment
503
* The object to attach to the I/O operation; can be {@code null}
504
* @param handler
505
* The handler for consuming the result
506
*
507
* @throws OverlappingFileLockException
508
* If a lock is already held by this Java virtual machine, or there
509
* is already a pending attempt to lock a region
510
* @throws NonWritableChannelException
511
* If this channel was not opened for writing
512
*/
513
public final <A> void lock(A attachment,
514
CompletionHandler<FileLock,? super A> handler)
515
{
516
lock(0L, Long.MAX_VALUE, false, attachment, handler);
517
}
518
519
/**
520
* Acquires a lock on the given region of this channel's file.
521
*
522
* <p> This method initiates an operation to acquire a lock on the given
523
* region of this channel's file. The method behaves in exactly the same
524
* manner as the {@link #lock(long, long, boolean, Object, CompletionHandler)}
525
* method except that instead of specifying a completion handler, this
526
* method returns a {@code Future} representing the pending result. The
527
* {@code Future}'s {@link Future#get() get} method returns the {@link
528
* FileLock} on successful completion.
529
*
530
* @param position
531
* The position at which the locked region is to start; must be
532
* non-negative
533
* @param size
534
* 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
* @param shared
537
* {@code true} to request a shared lock, in which case this
538
* channel must be open for reading (and possibly writing);
539
* {@code false} to request an exclusive lock, in which case this
540
* channel must be open for writing (and possibly reading)
541
*
542
* @return a {@code Future} object representing the pending result
543
*
544
* @throws OverlappingFileLockException
545
* If a lock is already held by this Java virtual machine, or there
546
* is already a pending attempt to lock a region
547
* @throws IllegalArgumentException
548
* If the preconditions on the parameters do not hold
549
* @throws NonReadableChannelException
550
* If {@code shared} is true but this channel was not opened for reading
551
* @throws NonWritableChannelException
552
* If {@code shared} is false but this channel was not opened for writing
553
*/
554
public abstract Future<FileLock> lock(long position, long size, boolean shared);
555
556
/**
557
* Acquires an exclusive lock on this channel's file.
558
*
559
* <p> This method initiates an operation to acquire an exclusive lock on this
560
* channel's file. The method returns a {@code Future} representing the
561
* pending result of the operation. The {@code Future}'s {@link Future#get()
562
* get} method returns the {@link FileLock} on successful completion.
563
*
564
* <p> An invocation of this method behaves in exactly the same way as the
565
* invocation
566
* <pre>
567
* ch.{@link #lock(long,long,boolean) lock}(0L, Long.MAX_VALUE, false)
568
* </pre>
569
*
570
* @return a {@code Future} object representing the pending result
571
*
572
* @throws OverlappingFileLockException
573
* If a lock is already held by this Java virtual machine, or there
574
* is already a pending attempt to lock a region
575
* @throws NonWritableChannelException
576
* If this channel was not opened for writing
577
*/
578
public final Future<FileLock> lock() {
579
return lock(0L, Long.MAX_VALUE, false);
580
}
581
582
/**
583
* Attempts to acquire a lock on the given region of this channel's file.
584
*
585
* <p> This method does not block. An invocation always returns immediately,
586
* either having acquired a lock on the requested region or having failed to
587
* do so. If it fails to acquire a lock because an overlapping lock is held
588
* by another program then it returns {@code null}. If it fails to acquire
589
* a lock for any other reason then an appropriate exception is thrown.
590
*
591
* @param position
592
* The position at which the locked region is to start; must be
593
* non-negative
594
*
595
* @param size
596
* The size of the locked region; must be non-negative, and the sum
597
* {@code position}&nbsp;+&nbsp;{@code size} must be non-negative
598
*
599
* @param shared
600
* {@code true} to request a shared lock,
601
* {@code false} to request an exclusive lock
602
*
603
* @return A lock object representing the newly-acquired lock,
604
* or {@code null} if the lock could not be acquired
605
* because another program holds an overlapping lock
606
*
607
* @throws IllegalArgumentException
608
* If the preconditions on the parameters do not hold
609
* @throws ClosedChannelException
610
* If this channel is closed
611
* @throws OverlappingFileLockException
612
* If a lock that overlaps the requested region is already held by
613
* this Java virtual machine, or if another thread is already
614
* blocked in this method and is attempting to lock an overlapping
615
* region of the same file
616
* @throws NonReadableChannelException
617
* If {@code shared} is true but this channel was not opened for reading
618
* @throws NonWritableChannelException
619
* If {@code shared} is false but this channel was not opened for writing
620
*
621
* @throws IOException
622
* If some other I/O error occurs
623
*
624
* @see #lock(Object,CompletionHandler)
625
* @see #lock(long,long,boolean,Object,CompletionHandler)
626
* @see #tryLock()
627
*/
628
public abstract FileLock tryLock(long position, long size, boolean shared)
629
throws IOException;
630
631
/**
632
* Attempts to acquire an exclusive lock on this channel's file.
633
*
634
* <p> An invocation of this method of the form {@code ch.tryLock()}
635
* behaves in exactly the same way as the invocation
636
*
637
* <pre>
638
* ch.{@link #tryLock(long,long,boolean) tryLock}(0L, Long.MAX_VALUE, false) </pre>
639
*
640
* @return A lock object representing the newly-acquired lock,
641
* or {@code null} if the lock could not be acquired
642
* because another program holds an overlapping lock
643
*
644
* @throws ClosedChannelException
645
* If this channel is closed
646
* @throws OverlappingFileLockException
647
* If a lock that overlaps the requested region is already held by
648
* this Java virtual machine, or if another thread is already
649
* blocked in this method and is attempting to lock an overlapping
650
* region
651
* @throws NonWritableChannelException
652
* If {@code shared} is false but this channel was not opened for writing
653
*
654
* @throws IOException
655
* If some other I/O error occurs
656
*
657
* @see #lock(Object,CompletionHandler)
658
* @see #lock(long,long,boolean,Object,CompletionHandler)
659
* @see #tryLock(long,long,boolean)
660
*/
661
public final FileLock tryLock() throws IOException {
662
return tryLock(0L, Long.MAX_VALUE, false);
663
}
664
665
/**
666
* Reads a sequence of bytes from this channel into the given buffer,
667
* starting at the given file position.
668
*
669
* <p> This method initiates the reading of a sequence of bytes from this
670
* channel into the given buffer, starting at the given file position. The
671
* result of the read is the number of bytes read or {@code -1} if the given
672
* position is greater than or equal to the file's size at the time that the
673
* read is attempted.
674
*
675
* <p> This method works in the same manner as the {@link
676
* AsynchronousByteChannel#read(ByteBuffer,Object,CompletionHandler)}
677
* method, except that bytes are read starting at the given file position.
678
* If the given file position is greater than the file's size at the time
679
* that the read is attempted then no bytes are read.
680
*
681
* @param <A>
682
* The type of the attachment
683
* @param dst
684
* The buffer into which bytes are to be transferred
685
* @param position
686
* The file position at which the transfer is to begin;
687
* must be non-negative
688
* @param attachment
689
* The object to attach to the I/O operation; can be {@code null}
690
* @param handler
691
* The handler for consuming the result
692
*
693
* @throws IllegalArgumentException
694
* If the position is negative or the buffer is read-only
695
* @throws NonReadableChannelException
696
* If this channel was not opened for reading
697
*/
698
public abstract <A> void read(ByteBuffer dst,
699
long position,
700
A attachment,
701
CompletionHandler<Integer,? super A> handler);
702
703
/**
704
* Reads a sequence of bytes from this channel into the given buffer,
705
* starting at the given file position.
706
*
707
* <p> This method initiates the reading of a sequence of bytes from this
708
* channel into the given buffer, starting at the given file position. This
709
* method returns a {@code Future} representing the pending result of the
710
* operation. The {@code Future}'s {@link Future#get() get} method returns
711
* the number of bytes read or {@code -1} if the given position is greater
712
* than or equal to the file's size at the time that the read is attempted.
713
*
714
* <p> This method works in the same manner as the {@link
715
* AsynchronousByteChannel#read(ByteBuffer)} method, except that bytes are
716
* read starting at the given file position. If the given file position is
717
* greater than the file's size at the time that the read is attempted then
718
* no bytes are read.
719
*
720
* @param dst
721
* The buffer into which bytes are to be transferred
722
* @param position
723
* The file position at which the transfer is to begin;
724
* must be non-negative
725
*
726
* @return A {@code Future} object representing the pending result
727
*
728
* @throws IllegalArgumentException
729
* If the position is negative or the buffer is read-only
730
* @throws NonReadableChannelException
731
* If this channel was not opened for reading
732
*/
733
public abstract Future<Integer> read(ByteBuffer dst, long position);
734
735
/**
736
* Writes a sequence of bytes to this channel from the given buffer, starting
737
* at the given file position.
738
*
739
* <p> This method works in the same manner as the {@link
740
* AsynchronousByteChannel#write(ByteBuffer,Object,CompletionHandler)}
741
* method, except that bytes are written starting at the given file position.
742
* If the given position is greater than the file's size, at the time that
743
* the write is attempted, then the file will be grown to accommodate the new
744
* bytes; the values of any bytes between the previous end-of-file and the
745
* newly-written bytes are unspecified.
746
*
747
* @param <A>
748
* The type of the attachment
749
* @param src
750
* The buffer from which bytes are to be transferred
751
* @param position
752
* The file position at which the transfer is to begin;
753
* must be non-negative
754
* @param attachment
755
* The object to attach to the I/O operation; can be {@code null}
756
* @param handler
757
* The handler for consuming the result
758
*
759
* @throws IllegalArgumentException
760
* If the position is negative
761
* @throws NonWritableChannelException
762
* If this channel was not opened for writing
763
*/
764
public abstract <A> void write(ByteBuffer src,
765
long position,
766
A attachment,
767
CompletionHandler<Integer,? super A> handler);
768
769
/**
770
* Writes a sequence of bytes to this channel from the given buffer, starting
771
* at the given file position.
772
*
773
* <p> This method initiates the writing of a sequence of bytes to this
774
* channel from the given buffer, starting at the given file position. The
775
* method returns a {@code Future} representing the pending result of the
776
* write operation. The {@code Future}'s {@link Future#get() get} method
777
* returns the number of bytes written.
778
*
779
* <p> This method works in the same manner as the {@link
780
* AsynchronousByteChannel#write(ByteBuffer)} method, except that bytes are
781
* written starting at the given file position. If the given position is
782
* greater than the file's size, at the time that the write is attempted,
783
* then the file will be grown to accommodate the new bytes; the values of
784
* any bytes between the previous end-of-file and the newly-written bytes
785
* are unspecified.
786
*
787
* @param src
788
* The buffer from which bytes are to be transferred
789
* @param position
790
* The file position at which the transfer is to begin;
791
* must be non-negative
792
*
793
* @return A {@code Future} object representing the pending result
794
*
795
* @throws IllegalArgumentException
796
* If the position is negative
797
* @throws NonWritableChannelException
798
* If this channel was not opened for writing
799
*/
800
public abstract Future<Integer> write(ByteBuffer src, long position);
801
}
802
803