Skip to content

Source implementation for already-buffered data #444

Open
@CLOVIS-AI

Description

@CLOVIS-AI

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions