Skip to content

Source implementation for already-buffered data #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
CLOVIS-AI opened this issue Apr 4, 2025 · 0 comments
Open

Source implementation for already-buffered data #444

CLOVIS-AI opened this issue Apr 4, 2025 · 0 comments

Comments

@CLOVIS-AI
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant