Skip to content

okio / okio / BufferedSource / readUtf8LineStrict

readUtf8LineStrict

(js, jvm, native) abstract fun readUtf8LineStrict():String(js)

Removes and returns characters up to but not including the next line break. A line break is either "\n" or "\r\n"; these characters are not included in the result.

On the end of the stream this method throws. Every call must consume either ‘\r\n’ or ‘\n’. If these characters are absent in the stream, an java.io.EOFException is thrown. Use this for machine-generated data where a missing line break implies truncated input.

(jvm)

Removes and returns characters up to but not including the next line break. A line break is either "\n" or "\r\n"; these characters are not included in the result.

On the end of the stream this method throws. Every call must consume either ‘\r\n’ or ‘\n’. If these characters are absent in the stream, an java.io.EOFException is thrown. Use this for machine-generated data where a missing line break implies truncated input.

(js, jvm, native) abstract fun readUtf8LineStrict(limit:Long):String

Like readUtf8LineStrict, except this allows the caller to specify the longest allowed match. Use this to protect against streams that may not include "\n" or "\r\n".

The returned string will have at most limit UTF-8 bytes, and the maximum number of bytes scanned is limit + 2. If limit == 0 this will always throw an EOFException because no bytes will be scanned.

This method is safe. No bytes are discarded if the match fails, and the caller is free to try another match:

Buffer buffer = new Buffer();
buffer.writeUtf8("12345\r\n");

// This will throw! There must be \r\n or \n at the limit or before it.
buffer.readUtf8LineStrict(4);

// No bytes have been consumed so the caller can retry.
assertEquals("12345", buffer.readUtf8LineStrict(5));