Kotlin / kotlinx-io

Kotlin multiplatform I/O library
Apache License 2.0
1.29k stars 59 forks source link

Can you use peek to fill the buffer? #392

Open CLOVIS-AI opened 1 month ago

CLOVIS-AI commented 1 month ago

With a given source, if I run source.peek().readInt(), does this guarantee that 4 bytes will be read into the source's buffer?

If so, this should be documented in Source.peek().

TSampley commented 1 month ago

Do you mean the buffer of the Source returned by peek()? Or the original Source that you are invoking peek() on?

In either case, it seems covered by the documentation at the top of the Source interface. Specifically:

...
 * The buffer is refilled on reads as necessary, but it is also possible to ensure it contains enough data
 * using [require] or [request].
 * [Sink] also allows skipping unneeded prefix of data using [skip] and
 * provides look ahead into incoming data, buffering as much as necessary, using [peek].
...
fzhinkin commented 1 month ago

@CLOVIS-AI, yes, all the data accessed through the peek source will be available inside source's buffer. If there's not enough data in source's buffer to fulfill an operation on a peek source, new data will be fetched into the original source.