kotlinx-io icon indicating copy to clipboard operation
kotlinx-io copied to clipboard

Support read-only views with random-access capability

Open ptitjes opened this issue 2 years ago • 0 comments
trafficstars

For a lot of my use-cases, I would need some shared read-only buffer views with random-access capability.

The needed features would be:

  • being backed by a list of segments, to support large views
  • support random-access reads
  • support shared slices of views

Here is a proposed tentative design:

public fun Source.readView(byteCount: Long): BufferView

public fun Buffer.copyToView(startIndex: Long = 0L, endIndex: Long = size): BufferView

public class BufferView : AutoCloseableAlias {
    override fun close()

    public val size: Long

    public fun getByte(position: Long): Byte
    public fun getShort(position: Long): Short
    public fun getInt(position: Long): Int
    public fun getLong(position: Long): Long

    public fun slice(startIndex: Long = 0L, endIndex: Long = size): BufferView
}

I experimented with this design in this branch: https://github.com/ptitjes/kotlinx-io/tree/read-random-access

Corresponding discussion on kotlinlang's Slack: https://kotlinlang.slack.com/archives/CKCAAV8VB/p1694429247586879

This issue is dependent on https://github.com/Kotlin/kotlinx-io/issues/135.

ptitjes avatar Sep 23 '23 09:09 ptitjes