Skip to content

Instantly share code, notes, and snippets.

@mrenouf
Last active April 25, 2023 01:08
Show Gist options
  • Save mrenouf/41e7d22b2fe93c6d43a989a787087978 to your computer and use it in GitHub Desktop.
Save mrenouf/41e7d22b2fe93c6d43a989a787087978 to your computer and use it in GitHub Desktop.
suspend fun AsynchronousByteChannel.readInto(buffer: ByteBuffer): Int = suspendCancellableCoroutine {
read(buffer, null, object : CompletionHandler<Int, Any?> {
override fun completed(result: Int, attachment: Any?) { it.resume(result) }
override fun failed(exc: Throwable, attachment: Any?) = it.resumeWithException(exc)
})
}
suspend fun AsynchronousByteChannel.writeFrom(buffer: ByteBuffer): Int = suspendCancellableCoroutine {
write(buffer, null, object : CompletionHandler<Int, Any?> {
override fun completed(result: Int, attachment: Any?) = it.resume(result)
override fun failed(exc: Throwable, attachment: Any?) = it.resumeWithException(exc)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment