Table of Contents

Runnable samples

The repository ships a runnable, self-contained sample project for Bodu.Text.Bencode under samples/Text.Bencode/. It is offline and deterministic - every scenario reads one committed 278-byte Data/sample.torrent fixture - and is a member of bodu.slnx, built and executed by CI. The README documents every scenario individually: its intent, what the code does, the output to expect, and the APIs demonstrated.

Run it from the repository root:

dotnet run --project samples/Text.Bencode/Bodu.Text.Bencode.Samples.TorrentFile

The sample

Bodu.Text.Bencode.Samples.TorrentFile

A real BitTorrent metainfo file (BEP 3) - the format Bencode was invented for - read, verified, and re-authored through all of the library's layers:

  • ParseTorrent - the read-only BencodeDocument DOM: one parse, cheap BencodeElement cursors, GetString vs GetBytes for text vs binary byte strings (the pieces SHA-1 run), and TryGetProperty probing for optional keys.
  • CanonicalRoundTrip - Bencode's defining property: keys ascend in raw-byte order, so a value has exactly one encoding. Parse → re-emit reproduces the file byte for byte; the Utf8BencodeWriter sorts each dictionary as it closes and rejects duplicates; the strict Utf8BencodeReader rejects unsorted input unless AllowUnsortedKeys opts into lenient ingestion.
  • InfoHashRawSlice - the info-hash is the SHA-1 of the info dictionary's exact encoded bytes: BencodeElement.GetRawBytes() hands back precisely that slice, and Utf8BencodeWriter.WriteRawValue splices it untouched into a re-authored torrent, proving the hash survives. The digest renders through Bodu.Text.Encoding's Base16 - a cross-package demonstration.
  • PocoTorrent - the typed layer: BencodeSerializer maps the metainfo onto POCOs whose [PropertyName] attributes carry the keys with spaces (creation date, piece length), with byte[] binding for binary values - and even the POCO round trip is byte-exact.

Packages: Bodu.Text.Bencode, Bodu.Text.Encoding (info-hash rendering).

Guarded documentation

The Bencode guides under docs/guides/serialization/bencode/ carry compile-guarded snippets: examples marked with a <!-- compile --> sentinel are compiled against the current public API by DocumentationSnippetCompileTests in the library's test project (Regression tier).