1
+ package io.github.agcom.knio2
2
+
3
+ import kotlinx.coroutines.suspendCancellableCoroutine
4
+ import java.nio.ByteBuffer
5
+ import java.nio.channels.AsynchronousFileChannel
6
+ import java.nio.channels.FileLock
7
+
8
+ /* *
9
+ * Suspending version of [lock][AsynchronousFileChannel.lock] function.
10
+ *
11
+ * The operation is not actually cancellable, because the underlying channel ([AsynchronousFileChannel]) provides no guarantee for cancellation.
12
+ * In case of cancellation, you may ignore the results.
13
+ */
14
+ public suspend fun AsynchronousFileChannel.lockAwait (
15
+ position : Long = 0,
16
+ size : Long = Long .MAX_VALUE ,
17
+ shared : Boolean = false
18
+ ): FileLock = suspendCancellableCoroutine {
19
+ lock(position, size, shared, Unit , it.asCompletionHandler())
20
+ }
21
+
22
+ /* *
23
+ * Suspending version of [lock][AsynchronousFileChannel.read] function.
24
+ *
25
+ * The operation is not actually cancellable, because the underlying channel ([AsynchronousFileChannel]) provides no guarantee for cancellation.
26
+ * In case of cancellation, you may ignore the results.
27
+ */
28
+ public suspend fun AsynchronousFileChannel.readAwait (
29
+ dst : ByteBuffer ,
30
+ position : Long
31
+ ): Int = suspendCancellableCoroutine {
32
+ read(dst, position, Unit , it.asCompletionHandler())
33
+ }
34
+
35
+ /* *
36
+ * Suspending version of [lock][AsynchronousFileChannel.write] function.
37
+ *
38
+ * The operation is not actually cancellable, because the underlying channel ([AsynchronousFileChannel]) provides no guarantee for cancellation.
39
+ * In case of cancellation, you may ignore the results.
40
+ */
41
+ public suspend fun AsynchronousFileChannel.writeAwait (
42
+ src : ByteBuffer ,
43
+ position : Long
44
+ ): Int = suspendCancellableCoroutine {
45
+ write(src, position, Unit , it.asCompletionHandler())
46
+ }
0 commit comments