Bitfield

class Bitfield

A bitfield of arbitrary length. This is a pure-Kotlin port of libtorrent's bitfield (bitfield.hpp). It tracks which pieces a peer has, and which we have.

Stored MSB-first within each byte (bit 0 is 0x80 of byte 0), which is exactly the on-the-wire layout of the BitTorrent bitfield message. data can therefore be sent, and assign can read a received bitfield, with no reordering. libtorrent reaches the same layout via network-byte-order 32-bit words; a byte array is the simpler equivalent on the JVM/Native/JS, where we have no raw-pointer aliasing.

Constructors

Link copied to clipboard
constructor()
constructor(bits: Int, value: Boolean = false)

A bitfield of bits length, all zero (or all value if given).

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard

True if every bit is 1.

Link copied to clipboard
fun assign(bytes: ByteArray, bitCount: Int)

Copy bitCount bits from bytes (rounded up to whole bytes).

Link copied to clipboard
fun clear()

Drop all bits, making the bitfield empty.

Link copied to clipboard
fun clearAll()

Set every bit to 0.

Link copied to clipboard
fun clearBit(index: Int)
Link copied to clipboard
fun count(): Int

Number of bits set to 1.

Link copied to clipboard

The backing bytes, ready to drop into a wire bitfield message.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard

Index of the first set (1) bit, or -1 if none.

Link copied to clipboard

Index of the last cleared (0) bit, or -1 if every bit is set.

Link copied to clipboard
operator fun get(index: Int): Boolean
Link copied to clipboard
fun getBit(index: Int): Boolean
Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard

True if the bitfield has zero length.

Link copied to clipboard

True if no bit is 1.

Link copied to clipboard
fun numBytes(): Int

Number of bytes backing the bits.

Link copied to clipboard
fun resize(newBits: Int, value: Boolean = false)

Resize to newBits length; bits added on growth take value.

Link copied to clipboard
fun setAll()

Set every bit to 1 (trailing bits in the final byte are left clear).

Link copied to clipboard
fun setBit(index: Int)
fun setBit(index: Int, value: Boolean)
Link copied to clipboard
fun size(): Int

Number of bits in the bitfield.

Link copied to clipboard
open override fun toString(): String