Skip to content

Commit 3764e34

Browse files
author
Sergey Mashkov
committed
Introduce suspending consumeEachRemaining (#22)
1 parent fdae6eb commit 3764e34

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

binary-compatibility-validator/reference-public-api/kotlinx-coroutines-io-jvm.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ public abstract interface class kotlinx/coroutines/io/LookAheadSession {
262262

263263
public final class kotlinx/coroutines/io/LookAheadSessionKt {
264264
public static final fun consumeEachRemaining (Lkotlinx/coroutines/io/LookAheadSession;Lkotlin/jvm/functions/Function1;)V
265+
public static final fun consumeEachRemaining (Lkotlinx/coroutines/io/LookAheadSuspendSession;Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
265266
}
266267

267268
public abstract interface class kotlinx/coroutines/io/LookAheadSuspendSession : kotlinx/coroutines/io/LookAheadSession {

kotlinx-coroutines-io/kotlinx-coroutines-io-jvm/src/main/kotlin/kotlinx/coroutines/experimental/io/LookAheadSession.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,21 @@ inline fun LookAheadSession.consumeEachRemaining(visitor: (ByteBuffer) -> Boolea
4545
} while (true)
4646
}
4747

48+
@Suppress("REDUNDANT_INLINE_SUSPEND_FUNCTION_TYPE")
49+
@ExperimentalIoApi
50+
suspend inline fun LookAheadSuspendSession.consumeEachRemaining(visitor: suspend (ByteBuffer) -> Boolean) {
51+
do {
52+
val buffer = request(0, 1)
53+
if (buffer == null) {
54+
if (!awaitAtLeast(1)) break
55+
continue
56+
}
57+
58+
val s = buffer.remaining()
59+
val rc = visitor(buffer)
60+
consumed(s)
61+
62+
if (!rc) break
63+
} while (true)
64+
}
65+

0 commit comments

Comments
 (0)