readUTF8Line

suspend fun ByteReadChannel.readUTF8Line(max: Int = Int.MAX_VALUE): String?(source)

Deprecated

Use readLineStrict or readLine instead. See deprecation notes for more details.

Reads a UTF-8-encoded line from the ByteReadChannel. It recognizes CR, LF, and CRLF as line delimiters.

Deprecation

This function is deprecated. Use readLineStrict or readLine instead.

Use readLineStrict when:

  • You need to limit the line length and throw TooLongLineException when the limit is exceeded.

  • You expect every line to end with an explicit line delimiter. EOFException will be thrown if the channel is exhausted after reading line content but before a line delimiter is found.

Use readLine when:

  • You do not need to limit line length.

  • An explicit line delimiter at the end of the channel is not mandatory.

Compared to readUTF8Line, the new functions differ in the following ways:

  • They recognize LF and CRLF as line delimiters by default, which provides better performance. To keep current behavior and recognize all line delimiters (CR, LF, and CRLF), specify LineEnding.Lenient.

  • readLineStrict accepts Long instead of Int as a limit parameter.

The direct equivalent of readUTF8Line would be:

// Before
val line = channel.readUTF8Line(max = 1024)

// After
val buffer = StringBuilder()
val success = try {
channel.readLineStrictTo(buffer, limit = 1024, lineEnding = LineEnding.Lenient) >= 0
} catch (_: EOFException) {
true
}
val line = if (success) buffer.toString() else null

However, we recommend using the default line-ending behavior (LineEnding.Default) and do not ignore unexpected end-of-input:

val line = channel.readLineStrict(limit = 1024)

Report a problem

Return

the decoded line, or null if the channel is exhausted before a line can be read

Parameters

max

the maximum number of bytes in the UTF-8-encoded line, excluding the line delimiter. Defaults to Int.MAX_VALUE.

Throws

if the line exceeds max