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 line of UTF-8 characters from the ByteReadChannel. It recognizes CR, LF, and CRLF as a line delimiter.

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 reached.

  • You expect an explicit line break at the end of a line. EOFException will be thrown when the channel is closed before the line break.

Use readLine when:

  • You do not need to limit line length.

  • An explicit line break 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(out, 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

a string containing the line read, or null if channel is closed

Parameters

max

the maximum number of characters to read. Default is Int.MAX_VALUE.

Throws

if max is reached before encountering a line delimiter or end of input