-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated to non-cancellable suspending calls
Closes #6
- Loading branch information
Showing
3 changed files
with
43 additions
and
28 deletions.
There are no files selected for viewing
18 changes: 11 additions & 7 deletions
18
lib/src/main/kotlin/io/github/agcom/knio2/AsyncByteChannel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,29 @@ | ||
package io.github.agcom.knio2 | ||
|
||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import java.nio.ByteBuffer | ||
import java.nio.channels.AsynchronousByteChannel | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
/** | ||
* Suspending version of [read][AsynchronousByteChannel.read] function. | ||
* | ||
* The operation is not actually cancellable, because the underlying channel ([AsynchronousByteChannel]) provides no guarantee for cancellation. | ||
* In case of cancellation, you may ignore the results. | ||
* The call is not cancellable (suspends until success or failure), because the underlying channel ([AsynchronousByteChannel]) provides no guarantee for cancellation. | ||
* Note that closing the channel (probably) continues every call with a failure (and that covers most use cases). | ||
* | ||
* However, you can mimic cancellation by ignoring the call (hence, ignoring the results). | ||
*/ | ||
public suspend fun AsynchronousByteChannel.readAwait(dst: ByteBuffer): Int = suspendCancellableCoroutine { | ||
public suspend fun AsynchronousByteChannel.readAwait(dst: ByteBuffer): Int = suspendCoroutine { | ||
read(dst, Unit, it.asCompletionHandler()) | ||
} | ||
|
||
/** | ||
* Suspending version of [write][AsynchronousByteChannel.write] function. | ||
* | ||
* The operation is not actually cancellable, because the underlying channel ([AsynchronousByteChannel]) provides no guarantee for cancellation. | ||
* In case of cancellation, you may ignore the results. | ||
* The call is not cancellable (suspends until success or failure), because the underlying channel ([AsynchronousByteChannel]) provides no guarantee for cancellation. | ||
* Note that closing the channel (probably) continues every call with a failure (and that covers most use cases). | ||
* | ||
* However, you can mimic cancellation by ignoring the call (hence, ignoring the results). | ||
*/ | ||
public suspend fun AsynchronousByteChannel.writeAwait(src: ByteBuffer): Int = suspendCancellableCoroutine { | ||
public suspend fun AsynchronousByteChannel.writeAwait(src: ByteBuffer): Int = suspendCoroutine { | ||
write(src, Unit, it.asCompletionHandler()) | ||
} |
26 changes: 16 additions & 10 deletions
26
lib/src/main/kotlin/io/github/agcom/knio2/AsyncFileChannel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,52 @@ | ||
package io.github.agcom.knio2 | ||
|
||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import java.nio.ByteBuffer | ||
import java.nio.channels.AsynchronousFileChannel | ||
import java.nio.channels.FileLock | ||
import kotlin.coroutines.suspendCoroutine | ||
|
||
/** | ||
* Suspending version of [lock][AsynchronousFileChannel.lock] function. | ||
* | ||
* The operation is not actually cancellable, because the underlying channel ([AsynchronousFileChannel]) provides no guarantee for cancellation. | ||
* In case of cancellation, you may ignore the results. | ||
* The call is not cancellable (suspends until success or failure), because the underlying channel ([AsynchronousFileChannel]) provides no guarantee for cancellation. | ||
* Note that closing the channel (probably) continues every call with a failure (and that covers most use cases). | ||
* | ||
* However, you can mimic cancellation by ignoring the call (hence, ignoring the results). | ||
*/ | ||
public suspend fun AsynchronousFileChannel.lockAwait( | ||
position: Long = 0, | ||
size: Long = Long.MAX_VALUE, | ||
shared: Boolean = false | ||
): FileLock = suspendCancellableCoroutine { | ||
): FileLock = suspendCoroutine { | ||
lock(position, size, shared, Unit, it.asCompletionHandler()) | ||
} | ||
|
||
/** | ||
* Suspending version of [lock][AsynchronousFileChannel.read] function. | ||
* | ||
* The operation is not actually cancellable, because the underlying channel ([AsynchronousFileChannel]) provides no guarantee for cancellation. | ||
* In case of cancellation, you may ignore the results. | ||
* The call is not cancellable (suspends until success or failure), because the underlying channel ([AsynchronousFileChannel]) provides no guarantee for cancellation. | ||
* Note that closing the channel (probably) continues every call with a failure (and that covers most use cases). | ||
* | ||
* However, you can mimic cancellation by ignoring the call (hence, ignoring the results). | ||
*/ | ||
public suspend fun AsynchronousFileChannel.readAwait( | ||
dst: ByteBuffer, | ||
position: Long | ||
): Int = suspendCancellableCoroutine { | ||
): Int = suspendCoroutine { | ||
read(dst, position, Unit, it.asCompletionHandler()) | ||
} | ||
|
||
/** | ||
* Suspending version of [lock][AsynchronousFileChannel.write] function. | ||
* | ||
* The operation is not actually cancellable, because the underlying channel ([AsynchronousFileChannel]) provides no guarantee for cancellation. | ||
* In case of cancellation, you may ignore the results. | ||
* The call is not cancellable (suspends until success or failure), because the underlying channel ([AsynchronousFileChannel]) provides no guarantee for cancellation. | ||
* Note that closing the channel (probably) continues every call with a failure (and that covers most use cases). | ||
* | ||
* However, you can mimic cancellation by ignoring the call (hence, ignoring the results). | ||
*/ | ||
public suspend fun AsynchronousFileChannel.writeAwait( | ||
src: ByteBuffer, | ||
position: Long | ||
): Int = suspendCancellableCoroutine { | ||
): Int = suspendCoroutine { | ||
write(src, position, Unit, it.asCompletionHandler()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters