Skip to content

Easy-to-use inline method to consume all bytes from Source in efficient chunks #477

@rnett

Description

@rnett

I've been gradually trying to switch from Java nio to Kotlinx.io in my projects, and there's one particular method I keep needing to integrate with various other producers or consumers of bytes: A method to efficiently read blocks of bytes from a Source and call a lambda with them. Something like

inline fun Source.readAll(handler: (ByteString) -> Unit)

The exact block type isn't important, it could be ByteArray or w/e is most efficient. The contract wouldn't say anything about the size of the chunks, it would be whatever is most efficient for the source. The primary use-cases are needing to do aggregation over all of the bytes and needing to pass them to some other IO library.

It's not terribly hard to write, you can do it using transterTo a custom sink implementation. However, that limits it to blocking when there's no real reason to. The actual implementation can be an inline function, allowing handler to suspend if it wants to. But I can't write that function myself because Buffer.completeSegmentByteCount() is internal. And IMO it's a straightforward enough use-case that a function should be available for it.

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