Deactivated User
12/24/2016, 2:32 PMsuspend private fun _read(size: Int): ByteArray = asyncFun {
val onCancel: Signal<Unit> = Signal()
try {
__read(size, onCancel)
} finally {
onCancel.invoke(Unit)
}
}
suspend private fun __read(size: Int, onCancel: Signal<Unit>): ByteArray = suspendCoroutine { c ->
val out = ByteArray(size)
val buffer = ByteBuffer.wrap(out)
sc.read(buffer, this, object : CompletionHandler<Int, AsyncClient> {
override fun completed(result: Int, attachment: AsyncClient): Unit = run {
if (result < 0) {
c.resumeWithException(RuntimeException("EOF"))
} else {
c.resume(Arrays.copyOf(out, result))
}
}
override fun failed(exc: Throwable, attachment: AsyncClient): Unit = run {
c.resumeWithException(exc)
}
})
onCancel.add {
// Do cancellation
}
}