https://kotlinlang.org logo
#ktor
Title
# ktor
c

cy

01/03/2020, 3:15 PM
readUntilDelimiter should do the trick
r

rrva

01/03/2020, 3:34 PM
i have to manage buffers so slightly more work
but thanks just wanted to know if there was anything else
Copy code
private suspend fun ByteReadChannel.readCRLFLine(
        buffer: ByteBuffer,
        charBuffer: CharBuffer,
        charsetDecoder: CharsetDecoder,
        stringBuilder: StringBuilder
    ): String {
        buffer.clear()
        val bytesRead = this.readUntilDelimiter(crlf, buffer)
        buffer.flip()
        return when {
            bytesRead == 0 -> {
                this.skipDelimiter(crlf)
                stringBuilder.toString()
            }
            bytesRead < buffer.limit() -> {
                charBuffer.clear()
                val coderResult = charsetDecoder.decode(buffer, charBuffer, true)
                charBuffer.flip()
                check(coderResult == CoderResult.UNDERFLOW)
                stringBuilder.append(charBuffer)
                this.skipDelimiter(crlf)
                stringBuilder.toString()
            }
            else -> {
                charBuffer.clear()
                val coderResult = charsetDecoder.decode(buffer, charBuffer, false)
                charBuffer.flip()
                check(coderResult == CoderResult.UNDERFLOW)
                stringBuilder.append(charBuffer)
                readCRLFLine(buffer, charBuffer, charsetDecoder, stringBuilder)
            }
        }
    }
10 Views