Skip to content

Commit d133303

Browse files
author
Sergey Mashkov
committed
Clarify await and awaitAtLeast contract (#24)
1 parent 632a54a commit d133303

File tree

4 files changed

+9
-0
lines changed

4 files changed

+9
-0
lines changed

kotlinx-coroutines-io/common/src/kotlinx/coroutines/io/ByteChannelSequential.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ abstract class ByteChannelSequentialBase(initial: IoBuffer, override val autoFlu
474474

475475
override suspend fun await(atLeast: Int): Boolean {
476476
require(atLeast >= 0) { "atLeast parameter shouldn't be negative: $atLeast"}
477+
require(atLeast <= 4088) { "atLeast parameter shouldn't be larger than max buffer size of 4088: $atLeast" }
477478

478479
completeReading()
479480

kotlinx-coroutines-io/common/src/kotlinx/coroutines/io/ReadSession.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ interface SuspendableReadSession : ReadSession {
3838
* @return true if there are [atLeast] bytes available or false if end of stream encountered (there still could be
3939
* bytes available but less than [atLeast])
4040
* @throws Throwable if the channel has been closed with an exception or cancelled
41+
* @throws IllegalArgumentException if [atLeast] is negative to too big (usually bigger that 4088)
4142
*/
4243
suspend fun await(atLeast: Int = 1): Boolean
4344
}

kotlinx-coroutines-io/jvm/src/kotlinx/coroutines/io/ByteBufferChannel.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,9 @@ internal class ByteBufferChannel(
19301930
}
19311931

19321932
final override suspend fun awaitAtLeast(n: Int): Boolean {
1933+
require(n >= 0) { "atLeast parameter shouldn't be negative: $n" }
1934+
require(n <= 4088) { "atLeast parameter shouldn't be larger than max buffer size of 4088: $n" }
1935+
19331936
if (state.capacity.availableForRead >= n) {
19341937
if (state.idle || state is ReadWriteBufferState.Writing) setupStateForRead()
19351938
return true
@@ -2464,6 +2467,9 @@ internal class ByteBufferChannel(
24642467
override fun request(skip: Int, atLeast: Int): ByteBuffer? = null
24652468

24662469
override suspend fun awaitAtLeast(n: Int): Boolean {
2470+
require(n >= 0) { "atLeast parameter shouldn't be negative: $n" }
2471+
require(n <= 4088) { "atLeast parameter shouldn't be larger than max buffer size of 4088: $n" }
2472+
24672473
return false
24682474
}
24692475
}

kotlinx-coroutines-io/jvm/src/kotlinx/coroutines/io/LookAheadSession.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface LookAheadSession {
2727
interface LookAheadSuspendSession : LookAheadSession {
2828
/**
2929
* Suspend until [n] bytes become available or end of stream encountered (possibly due to exceptional close)
30+
* @see SuspendableReadSession.await
3031
*/
3132
suspend fun awaitAtLeast(n: Int): Boolean
3233
}

0 commit comments

Comments
 (0)