Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | */ |
| 4 | |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental.io |
| 6 | |
| 7 | interface 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 | |
| 26 | interface LookAheadSuspendSession : LookAheadSession { |
| 27 | /** |
| 28 | * Suspend until [n] bytes become available or end of stream encountered (possibly due to exceptional close) |
| 29 | */ |
Sergey Mashkov | 09b621e | 2017-12-22 20:42:18 +0300 | [diff] [blame] | 30 | suspend fun awaitAtLeast(n: Int): Boolean |
Sergey Mashkov | 4cd0014 | 2017-09-05 19:25:32 +0300 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | inline 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 | |