Skip to content

Commit

Permalink
KTOR-5372 Fix EOF in ByteBufferChannel when min is 0 (ktorio#3327)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsinukov authored and Rattenkrieg committed Jan 5, 2023
1 parent 0e5657c commit 9723dfd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt
Expand Up @@ -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")
}

Expand Down
5 changes: 3 additions & 2 deletions ktor-io/jvm/src/io/ktor/utils/io/jvm/nio/Writing.kt
Expand Up @@ -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
*/
Expand Down
15 changes: 15 additions & 0 deletions ktor-io/jvm/test/io/ktor/utils/io/ByteBufferChannelTest.kt
Expand Up @@ -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())
}
}
}

0 comments on commit 9723dfd

Please sign in to comment.