Struct WireWriter
Writes the wire's primitives (03-wire-protocol § 2) into a caller-owned span.
public ref struct WireWriter
- Inherited Members
Remarks
Encoders always write the canonical form: minimal varints, little-endian integers, NaN as 0x7E00 in a half. Two encoders fed the same values
therefore produce the same bytes, which is what lets a golden vector be a byte comparison.
Running out of span throws InvalidOperationException: the caller sized the buffer, so overflow is a bug on this side, never input from a peer. Values that cannot be represented (a string over its cap, a negative count) throw ArgumentException for the same reason.
Constructors
WireWriter(Span<byte>)
Creates a writer over buffer, positioned at its first byte.
public WireWriter(Span<byte> buffer)
Parameters
Properties
Position
Bytes written so far.
public readonly int Position { get; }
Property Value
Written
The bytes written so far.
public readonly ReadOnlySpan<byte> Written { get; }
Property Value
Methods
BeginLengthPrefixed()
Reserves room for a varu length prefix and returns a mark for EndLengthPrefixed(int). The content is written next, and the prefix is
patched in minimal form afterwards, so the caller never has to know a block's size before encoding it.
public int BeginLengthPrefixed()
Returns
- int
The mark.
Remarks
The reservation is five bytes whatever the final prefix, so a buffer must have up to four bytes of slack beyond the encoded size per open prefix. Prefixes nest, and must be ended innermost first.
EndLengthPrefixed(int)
Writes the minimal varu length of everything written since mark and moves the content down behind it.
public void EndLengthPrefixed(int mark)
Parameters
markintThe value BeginLengthPrefixed() returned.
VaruSize(uint)
The number of bytes WriteVaru(uint) writes for value.
public static int VaruSize(uint value)
Parameters
valueuintThe value.
Returns
- int
1 to 5.
WriteBits(uint, int)
Writes the low bits ∈ {8, 16, 24, 32} bits of value: an unsigned code, or a signed code's two's complement.
public void WriteBits(uint value, int bits)
Parameters
WriteBlob(scoped ReadOnlySpan<byte>, int)
Writes a blob: a varu length, then the bytes.
public void WriteBlob(scoped ReadOnlySpan<byte> bytes, int maxBytes)
Parameters
bytesReadOnlySpan<byte>The bytes.
maxBytesintThe declared cap.
WriteBytes(scoped ReadOnlySpan<byte>)
Writes raw bytes.
public void WriteBytes(scoped ReadOnlySpan<byte> bytes)
Parameters
bytesReadOnlySpan<byte>The bytes.
WriteF16(double)
Writes a little-endian IEEE half converted straight from value (W10): ties to even, overflow to ∞, NaN as 0x7E00.
public void WriteF16(double value)
Parameters
valuedoubleThe value, in its source precision — never narrowed through a float first.
WriteF32(double)
Writes a little-endian IEEE single, narrowing value with ties to even; NaN as 0x7FC00000.
public void WriteF32(double value)
Parameters
valuedoubleThe value.
WriteI16(short)
Writes a little-endian i16.
public void WriteI16(short value)
Parameters
valueshortThe value.
WriteI32(int)
Writes a little-endian i32.
public void WriteI32(int value)
Parameters
valueintThe value.
WriteI8(sbyte)
Writes an i8.
public void WriteI8(sbyte value)
Parameters
valuesbyteThe value.
WriteStr(string, int)
Writes a str: a varu byte length, then the UTF-8 bytes.
public void WriteStr(string value, int maxBytes)
Parameters
WriteU16(ushort)
Writes a little-endian u16.
public void WriteU16(ushort value)
Parameters
valueushortThe value.
WriteU24(uint)
Writes the low 24 bits of value, little-endian.
public void WriteU24(uint value)
Parameters
WriteU32(uint)
Writes a little-endian u32.
public void WriteU32(uint value)
Parameters
valueuintThe value.
WriteU8(byte)
Writes a u8.
public void WriteU8(byte value)
Parameters
valuebyteThe value.
WriteVari(int)
Writes a vari: zigzag, then varu.
public void WriteVari(int value)
Parameters
valueintThe value.
WriteVaru(uint)
Writes a varu: minimal unsigned LEB128.
public void WriteVaru(uint value)
Parameters
valueuintThe value.