Skip to content

Commit 5f59ebf

Browse files
committed
Added AsynchronousFileChannel suspending extensions
Closes #2
1 parent 233cff4 commit 5f59ebf

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ For Maven and Ivy snippets, see the [distribution page](https://bintray.com/agco
2323
- Suspending extension functions for following classes,
2424
- `AsynchronousByteChannel`
2525
- `AsynchronousSocketChannel`
26+
- `AsynchronousFileChannel`
2627

2728
The functions are named after their original name plus `await` suffix.
2829

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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

Comments
 (0)