Table of Contents

Class MetadataEncoder

Namespace
DatabentoDotNet.Dbn
Assembly
DatabentoDotNet.Dbn.dll

Encodes a Metadata back into the DBN header bytes that open a stream.

public static class MetadataEncoder
Inheritance
MetadataEncoder
Inherited Members

Remarks

The encoder exists to close the loop on the decoder: a header decoded and re-encoded under AsIs must reproduce the original bytes exactly. That is a stronger check than comparing fields — a decoder that mis-sizes the length field or forgets version 3's end padding corrupts the stream from the next record onward while still comparing equal field by field, and byte-identity catches both. It does not catch a dropped reserved run: every reserved run in the conformance corpus is all-zero, so a decoder that read one and discarded it re-encodes zeros in its place and still matches byte for byte.

The length field and the write sequence must agree. The prelude's length is computed up front by EncodedLength(Metadata) and then the body is written; nothing in the language ties the two together, which is exactly the drift upstream warns about. This encoder checks the bytes it actually wrote against the length it declared before returning, so a future edit that changes one and not the other fails immediately instead of producing a stream that reads eight bytes off from the second record onward.

Fields

MinEncodedLength

The smallest a DBN metadata block can be: 8 bytes of prelude, the 100-byte fixed section, and the five 32-bit counts that stand in for five empty variable-length sections.

public const int MinEncodedLength = 128

Field Value

int

Methods

Encode(Metadata)

Encodes metadata into a new array.

public static byte[] Encode(Metadata metadata)

Parameters

metadata Metadata

The metadata to encode.

Returns

byte[]

The encoded bytes, prelude included.

Exceptions

ArgumentNullException

metadata is null.

DbnEncodeException

The metadata cannot be encoded as DBN.

Encode(Metadata, Span<byte>)

Encodes metadata into destination.

public static int Encode(Metadata metadata, Span<byte> destination)

Parameters

metadata Metadata

The metadata to encode.

destination Span<byte>

A buffer of at least EncodedLength(Metadata) bytes. Bytes past the encoded block are left untouched.

Returns

int

The number of bytes written.

Exceptions

ArgumentNullException

metadata is null.

ArgumentException

destination is too small.

DbnEncodeException

The metadata cannot be encoded as DBN.

EncodedLength(Metadata)

Returns the exact number of bytes Encode(Metadata, Span<byte>) will write, prelude included.

public static int EncodedLength(Metadata metadata)

Parameters

metadata Metadata

The metadata to measure.

Returns

int

The total encoded size in bytes.

Exceptions

ArgumentNullException

metadata is null.

DbnEncodeException

The block would be larger than MaxValue bytes. That is the only thing measuring can reject: an out-of-range version, a zero symbol width, and an over-long or non-ASCII symbol are all rejected by Encode(Metadata, Span<byte>) when the bytes are actually written, not here — this method sums widths and never inspects content.