in ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt [1901:1930]
override suspend fun <R> lookAheadSuspend(visitor: suspend LookAheadSuspendSession.() -> R): R {
if (state === ReadWriteBufferState.Terminated) {
return visitor(TerminatedLookAhead)
}
var result: Any? = null
val rc = reading {
result = visitor(this@ByteBufferChannel)
true
}
if (!rc) {
if (closed != null || state === ReadWriteBufferState.Terminated) return visitor(TerminatedLookAhead)
try {
result = visitor(this)
} finally {
val stateSnapshot = state
if (!stateSnapshot.idle && stateSnapshot !== ReadWriteBufferState.Terminated) {
if (stateSnapshot is ReadWriteBufferState.Reading || stateSnapshot is ReadWriteBufferState.ReadingWriting) {
restoreStateAfterRead()
}
tryTerminate()
}
}
}
@Suppress("UNCHECKED_CAST")
return result as R
}