kotlinx-io icon indicating copy to clipboard operation
kotlinx-io copied to clipboard

Can you use peek to fill the buffer?

Open CLOVIS-AI opened this issue 1 year ago • 2 comments
trafficstars

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().

CLOVIS-AI avatar Sep 21 '24 17:09 CLOVIS-AI

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

TSampley avatar Sep 21 '24 18:09 TSampley

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

fzhinkin avatar Sep 21 '24 18:09 fzhinkin