DiskIo

interface DiskIo

The disk subsystem boundary. This is the Kotlin counterpart of libtorrent's disk_interface (disk_interface.hpp). libtorrent dispatches each operation to a thread pool and hands back the result through a completion callback; here every operation simply suspends, so the caller writes straight-line coroutine code and the implementation is free to run blocking file I/O on an I/O dispatcher.

Implementations live per-platform (a kotlinx-io-backed one for JVM/Android/native); this interface keeps the engine itself storage-agnostic, exactly as libtorrent's does, which is what makes an in-memory test double trivial.

Inheritors

Functions

Link copied to clipboard
abstract suspend fun checkExistingFiles(): BooleanArray

Which pieces do we already have on disk? Drives the initial resume/recheck.

Link copied to clipboard
abstract suspend fun close()

Release file handles and stop.

Link copied to clipboard
abstract suspend fun flush()

Flush any buffered writes to durable storage.

Link copied to clipboard
abstract suspend fun hashPiece(piece: Int): PieceHashes

Hash a fully-downloaded piece for verification against the torrent's hashes.

Link copied to clipboard
open suspend fun move(newSavePath: String)

Move all storage to newSavePath, the counterpart of libtorrent's async_move_storage (storage_utils.cpp move_storage). Open handles are flushed and closed, the on-disk files (and any part-file) are relocated under the new save path, and subsequent reads/writes target the new location. A best-effort, no-op default is provided for backends without on-disk state (e.g. the in-memory double).

Link copied to clipboard
abstract suspend fun read(piece: Int, offset: Int, length: Int): ByteArray

Read length bytes at offset within piece.

Link copied to clipboard
open suspend fun rename(fileIndex: Int, newName: String)

Rename the file at fileIndex to newName, the counterpart of libtorrent's async_rename_file. newName is a torrent-relative path ('/'-separated). The default is a no-op for backends without on-disk file names.

Link copied to clipboard
abstract suspend fun write(piece: Int, offset: Int, data: ByteArray)

Write data at offset within piece. The bytes are owned by the callee after the call.