From 9723dfd49d8d05870c1a42548b0ada25d20c4c73 Mon Sep 17 00:00:00 2001 From: Rustam Date: Tue, 3 Jan 2023 10:39:00 +0100 Subject: [PATCH] KTOR-5372 Fix EOF in ByteBufferChannel when min is 0 (#3327) --- .../jvm/src/io/ktor/utils/io/ByteBufferChannel.kt | 2 +- .../jvm/src/io/ktor/utils/io/jvm/nio/Writing.kt | 5 +++-- .../io/ktor/utils/io/ByteBufferChannelTest.kt | 15 +++++++++++++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt b/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt index f66dd70f230..dc21a82947c 100644 --- a/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt +++ b/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt @@ -1655,7 +1655,7 @@ internal open class ByteBufferChannel( } if (!read) { - if (isClosedForRead) { + if (isClosedForRead && min > 0) { throw EOFException("Got EOF but at least $min bytes were expected") } diff --git a/ktor-io/jvm/src/io/ktor/utils/io/jvm/nio/Writing.kt b/ktor-io/jvm/src/io/ktor/utils/io/jvm/nio/Writing.kt index d5ea47807fa..7a0bf5201c7 100644 --- a/ktor-io/jvm/src/io/ktor/utils/io/jvm/nio/Writing.kt +++ b/ktor-io/jvm/src/io/ktor/utils/io/jvm/nio/Writing.kt @@ -5,8 +5,9 @@ import java.nio.* import java.nio.channels.* /** - * Copy up to [limit] bytes to blocking NIO [channel]. Copying to non-blocking channel requires selection and - * not supported. It does suspend if no data available in byte channel but may block if destination NIO channel blocks. + * Copy up to [limit] bytes to blocking NIO [channel]. + * Copying to a non-blocking channel requires selection and not supported. + * It is suspended if no data are available in a byte channel but may block if destination NIO channel blocks. * * @return number of bytes copied */ diff --git a/ktor-io/jvm/test/io/ktor/utils/io/ByteBufferChannelTest.kt b/ktor-io/jvm/test/io/ktor/utils/io/ByteBufferChannelTest.kt index 8cb15312fb3..b823b26f5bd 100644 --- a/ktor-io/jvm/test/io/ktor/utils/io/ByteBufferChannelTest.kt +++ b/ktor-io/jvm/test/io/ktor/utils/io/ByteBufferChannelTest.kt @@ -253,4 +253,19 @@ class ByteBufferChannelTest { reader.await() writer.join() } + + @Test + fun testReadWithNoMinDoesntThrow() = runBlocking { + val channel = ByteChannel(true) + + channel.writeByte(1) + channel.read(0) { + assertEquals(1, it.remaining()) + it.get() + } + channel.close() + channel.read(0) { + assertEquals(0, it.remaining()) + } + } }