Bodu.Text.Bencode.Reader Namespace
- Package
-
Bodu.Text.Bencode 1.0.0
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 structtoken cursor:Read/Skip/TrySkip,TokenType(a BencodeTokenType),ValueSpan, and the typed accessorsGetString/GetBytes/GetInt32/GetInt64/GetUInt64with theirTryGet*counterparts, plusValueTextEqualsfor allocation-free key matching. - BencodeReaderOptions -
MaxDepth, and theAllowUnsortedKeys/AllowDuplicateKeysrelaxations 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.
ValueSpanandGetBytesreturn the raw byte string;GetStringdecodes 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.