blob: 0a33777ab54942fe5727bb57a813049fb29dbd75 [file] [log] [blame]
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03001/*
2 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3 */
4
Sergey Mashkov4cd00142017-09-05 19:25:32 +03005package kotlinx.coroutines.experimental.io
6
7interface LookAheadSession {
8 /**
9 * Marks [n] bytes as consumed so the corresponding range becomes available for writing
10 */
11 fun consumed(n: Int)
12
13 /**
14 * Request byte buffer range skipping [skip] bytes and [atLeast] bytes length
15 * @return byte buffer for the requested range or null if it is impossible to provide such a buffer
16 *
17 * There are the following reasons for this function to return `null`:
18 * - not enough bytes available yet (should be at least `skip + atLeast` bytes available)
19 * - due to buffer fragmentation is is impossible to represent the requested range as a single byte buffer
20 * - end of stream encountered and all bytes were consumed
21 * - channel has been closed with an exception so buffer has been recycled
22 */
23 fun request(skip: Int, atLeast: Int): ByteBuffer?
24}
25
26interface LookAheadSuspendSession : LookAheadSession {
27 /**
28 * Suspend until [n] bytes become available or end of stream encountered (possibly due to exceptional close)
29 */
Sergey Mashkov09b621e2017-12-22 20:42:18 +030030 suspend fun awaitAtLeast(n: Int): Boolean
Sergey Mashkov4cd00142017-09-05 19:25:32 +030031}
32
33inline fun LookAheadSession.consumeEachRemaining(visitor: (ByteBuffer) -> Boolean) {
34 do {
35 val cont = request(0, 1)?.let {
36 val s = it.remaining()
37 val rc = visitor(it)
38 consumed(s)
39 rc
40 } ?: false
41
42 if (!cont) break
43 } while (true)
44}
45