Skip to content

Feature Request: Missing function ByteReadChannel.readTo(sink: RawSink, byteCount: Long) [io.ktor.utils.io] #5037

@remmerw

Description

@remmerw

The following function would be nice to have

ByteReadChannel.readTo(sink: RawSink, byteCount: Long) [io.ktor.utils.io]

It would allow to store directly into a file (instead of copying data inbetween

so something like this

@OptIn(InternalAPI::class)
suspend fun ByteReadChannel.readTo(sink: RawSink, byteCount: Long) {

var remaining = byteCount

while (remaining > 0 && !isClosedForRead) {
    if (readBuffer.exhausted()) awaitContent()

    val size = minOf(remaining.toLong(), readBuffer.remaining)
    readBuffer.readTo(sink, size)
    remaining -= size.toInt()
}

}

Not sure if it is working (not tested)

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