Table of Contents

Bodu.Text.Bencode.Reader Namespace

Package

Bodu.Text.Bencode.Reader

Purpose

Bodu.Text.Bencode.Reader is the lowest tier of Bodu.Text.Bencode: a forward-only, allocation-free token reader over canonical Bencode (BEP 3) bytes, in the manner of Utf8JsonReader. The BencodeSerializer, the mutable BencodeNode DOM, and the read-only BencodeDocument DOM are all built over it, and a custom BencodeConverter<T> consumes values through it. Use it directly when you need full control over a token stream without materializing a document.

Key types

  • Utf8BencodeReader - the ref struct token cursor: Read / Skip / TrySkip, TokenType (a BencodeTokenType), ValueSpan, and the typed accessors GetString / GetBytes / GetInt32 / GetInt64 / GetUInt64 with their TryGet* counterparts, plus ValueTextEquals for allocation-free key matching.
  • BencodeReaderOptions - MaxDepth, and the AllowUnsortedKeys / AllowDuplicateKeys relaxations for non-canonical producers.

Example

using Bodu.Text.Bencode;
using Bodu.Text.Bencode.Reader;

var reader = new Utf8BencodeReader("d6:lengthi1024e4:name10:ubuntu.isoe"u8);

while (reader.Read())
{
    if (reader.TokenType == BencodeTokenType.PropertyName && reader.ValueTextEquals("name"u8))
    {
        reader.Read();
        string name = reader.GetString();   // "ubuntu.iso"
    }
}

Notes

  • Canonical by default. The reader rejects unsorted or duplicate dictionary keys and non-minimal integers unless the corresponding option is set; malformed bytes throw BencodeFormatException.
  • Byte strings, not text. ValueSpan and GetBytes return the raw byte string; GetString decodes it as UTF-8.
  • See also: the Bodu.Text.Bencode introduction and the Using Bencode guide (Pattern 9 - Process tokens by hand).

Structs

BencodeReaderOptions

Defines the customizations applied when creating a Utf8BencodeReader.

Utf8BencodeReader

Provides a high-performance, forward-only, allocation-light reader for Bencode (BEP 3) bytes. The reader is a ref struct, so it lives on the stack and cannot be boxed or captured; pass it by ref to thread it through a converter.