peek

open override fun peek(): Source(source)

Returns a new Source that can read data from this source without consuming it. The returned source becomes invalid once this source is next read or closed.

Peek could be used to lookahead and read the same data multiple times.

If peek source needs to access more data that this Source has in its buffer, more data will be requested from the underlying source and on success, it'll be added to the buffer of this Source. If the underlying source was exhausted or some error occurred on attempt to fill the buffer, a corresponding exception will be thrown.

Throws

when the source is closed.

Samples

import kotlinx.io.*
import kotlin.test.*

fun main() { 
   //sampleStart 
   val source: Source = Buffer().also { it.writeString("hello world") }

val peek = source.peek().buffered()
assertEquals("hello", peek.readString(5))
peek.skip(1)
assertEquals("world", peek.readString(5))
assertTrue(peek.exhausted())

assertEquals("hello world", source.readString()) 
   //sampleEnd
}