From 1555a8854a7cdf8624acb2529a7edba7db4eed3d Mon Sep 17 00:00:00 2001 From: "leonid.stashevsky" Date: Mon, 10 Oct 2022 11:40:21 +0200 Subject: [PATCH] fixup! KTOR-2588 Fix long line reading --- .../jvm/src/io/ktor/utils/io/ByteBufferChannel.kt | 4 ++-- .../ktor/utils/io/charsets/ByteChannelTextTest.kt | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 2 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 27239eb5e1..f0eb60c8f2 100644 --- a/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt +++ b/ktor-io/jvm/src/io/ktor/utils/io/ByteBufferChannel.kt @@ -1962,12 +1962,12 @@ internal open class ByteBufferChannel( newLine = true } - if (it.hasRemaining() && it[it.position()] == '\r'.code.toByte()) { + if (requiredBytes != -1 && it.hasRemaining() && it[it.position()] == '\r'.code.toByte()) { it.position(it.position() + 1) caret = true } - if (it.hasRemaining() && it[it.position()] == '\n'.code.toByte()) { + if (requiredBytes != -1 && it.hasRemaining() && it[it.position()] == '\n'.code.toByte()) { it.position(it.position() + 1) newLine = true } diff --git a/ktor-io/jvm/test/io/ktor/utils/io/charsets/ByteChannelTextTest.kt b/ktor-io/jvm/test/io/ktor/utils/io/charsets/ByteChannelTextTest.kt index fc7bcee1c3..1158a6c45c 100644 --- a/ktor-io/jvm/test/io/ktor/utils/io/charsets/ByteChannelTextTest.kt +++ b/ktor-io/jvm/test/io/ktor/utils/io/charsets/ByteChannelTextTest.kt @@ -42,4 +42,18 @@ class ByteChannelTextTest { val result = channel.readUTF8Line() assertEquals(line, result) } + + @Test + fun test2EmptyLines() { + val text = ByteReadChannel("\r\n\r\n") + + runBlocking { + assertEquals(4, text.availableForRead) + assertEquals("", text.readUTF8Line()) + assertEquals(2, text.availableForRead) + assertEquals(2, text.totalBytesRead) + assertEquals("", text.readUTF8Line()) + assertNull(text.readUTF8Line()) + } + } }