RandomAccessSource

A source that supports random access reads at arbitrary positions.

Unlike kotlinx-io Source which is sequential, this allows reading at any position without loading the entire file into memory.

This is critical for parsing large model files (100+ GB) where only metadata needs to be read initially, with tensor data loaded lazily.

Implementations should be thread-safe for concurrent reads from different positions.

Usage:

RandomAccessSource.open(filePath).use { source ->
val header = source.readAt(0, 24) // Read first 24 bytes
val tensorData = source.readAt(dataOffset, tensorSize) // Read specific tensor
}

Inheritors

Properties

Link copied to clipboard
abstract val size: Long

The total size of the source in bytes.

Functions

Link copied to clipboard
abstract fun readAt(position: Long, length: Int): ByteArray

Read bytes from the specified position.

abstract fun readAt(position: Long, buffer: ByteArray, offset: Int = 0, length: Int = buffer.size): Int

Read bytes into an existing buffer.

Link copied to clipboard
open fun readByteAt(position: Long): Byte

Read a single byte at the specified position. Convenience method for reading single values.