Entry

sealed class Entry

One node in a bencoded hierarchy: the owning variant type, the port of libtorrent's entry (entry.hpp). Use this to build structures (a torrent file, a DHT message, resume data) and hand them to Bencode.encode.

A bencode string is a raw byte buffer, not text (BEP 3). The pieces field of a torrent, for example, is a run of concatenated 20-byte SHA-1 hashes. So Str holds a ByteArray. The text and str helpers cover the common case of UTF-8 keys and values.

Dictionary keys are byte strings too, but in practice always ASCII. Dict keeps them as String; Bencode.encode sorts them by raw byte order (required by the spec, and required for info-hashes to match other clients).

Inheritors

Types

Link copied to clipboard
object Companion
Link copied to clipboard
class Dict(val map: MutableMap<String, Entry> = LinkedHashMap()) : Entry

A dictionary (d…e). Iteration/encoding order is canonical (keys sorted by bytes).

Link copied to clipboard
data class Int(val value: Long) : Entry

A 64-bit integer (i…e).

Link copied to clipboard
class ListEntry(val items: MutableList<Entry> = mutableListOf()) : Entry

An ordered list (l…e).

Link copied to clipboard
class Preformatted(val bytes: ByteArray) : Entry

Raw, already-bencoded bytes spliced in verbatim (libtorrent's preformatted_type).

Link copied to clipboard
class Str(val bytes: ByteArray) : Entry

A byte string (<len>:<bytes>).

Link copied to clipboard
object Undefined : Entry

Uninitialized. It encodes as an empty string 0:, matching libtorrent.

Functions

Link copied to clipboard
Link copied to clipboard
fun findKey(key: String): Entry?

Dictionary lookup; returns null if this isn't a dict or the key is absent.

Link copied to clipboard
operator fun get(key: String): Entry

Dictionary index/insert. Throws if this entry is not a dict.

Link copied to clipboard
fun integer(): Long
Link copied to clipboard
Link copied to clipboard
operator fun set(key: String, value: Entry)
Link copied to clipboard
fun string(): String
Link copied to clipboard