kotlinx-io
kotlinx-io copied to clipboard
Source implementation for already-buffered data
I have a situation in which I'm reading data from a stream. However, the stream is already split into chunks of a known size, where each chunk is parsed independently. Each chunk is represented by a ByteArray that I know will never mutate.
The entities I'm parsing are recursive, so I represent each entity as the entire ByteArray and an IntRange to know where it starts and ends. Writing a RawSource implementation based on this was very simple.
However, Kotlinx-io only offers utilities for Source, in particular the methods to parse integers in big-endian and little-endian notations. I also need .peek() to be able to know some things. At the moment this means I have to use myCustomRawSource.buffered() to get an instance of Source, but this creates the entire Buffer machinery which is frankly unnecessary here, since all data is already within a ByteArray. .peek() could be trivially implemented using a single integer to remember the current index, etc.
However, since Source is sealed, I cannot create a custom implementation that would take advantage of the source data already being buffered.
I don't understand why the library forbids other Source implementations and makes Source.buffer part of the (semi)-public API.