Skip to content

Commit 615305e

Browse files
authored
KTOR-5372 Fix EOF in ByteBufferChannel when min is 0 (#3327)
1 parent 9a15ea0 commit 615305e

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1655,7 +1655,7 @@ internal open class ByteBufferChannel(
16551655
}
16561656

16571657
if (!read) {
1658-
if (isClosedForRead) {
1658+
if (isClosedForRead && min > 0) {
16591659
throw EOFException("Got EOF but at least $min bytes were expected")
16601660
}
16611661

ktor-io/jvm/src/io/ktor/utils/io/jvm/nio/Writing.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import java.nio.*
55
import java.nio.channels.*
66

77
/**
8-
* Copy up to [limit] bytes to blocking NIO [channel]. Copying to non-blocking channel requires selection and
9-
* not supported. It does suspend if no data available in byte channel but may block if destination NIO channel blocks.
8+
* Copy up to [limit] bytes to blocking NIO [channel].
9+
* Copying to a non-blocking channel requires selection and not supported.
10+
* It is suspended if no data are available in a byte channel but may block if destination NIO channel blocks.
1011
*
1112
* @return number of bytes copied
1213
*/

ktor-io/jvm/test/io/ktor/utils/io/ByteBufferChannelTest.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,19 @@ class ByteBufferChannelTest {
253253
reader.await()
254254
writer.join()
255255
}
256+
257+
@Test
258+
fun testReadWithNoMinDoesntThrow() = runBlocking {
259+
val channel = ByteChannel(true)
260+
261+
channel.writeByte(1)
262+
channel.read(0) {
263+
assertEquals(1, it.remaining())
264+
it.get()
265+
}
266+
channel.close()
267+
channel.read(0) {
268+
assertEquals(0, it.remaining())
269+
}
270+
}
256271
}

0 commit comments

Comments
 (0)