Struct SliceDecoder
Provides methods to decode data encoded with Slice.
public ref struct SliceDecoder
- Inherited Members
- Extension Methods
Constructors
SliceDecoder(ReadOnlySequence<byte>, object?, int)
Constructs a new Slice decoder over a byte buffer.
public SliceDecoder(ReadOnlySequence<byte> buffer, object? decodingContext = null, int maxCollectionAllocation = -1)
Parameters
bufferReadOnlySequence<byte>The byte buffer.
decodingContextobjectThe decoding context.
maxCollectionAllocationintThe maximum cumulative allocation in bytes when decoding strings, sequences, and dictionaries from this buffer.
-1(the default) is equivalent to 8 times the buffer length, clamped to MaxValue.
SliceDecoder(ReadOnlyMemory<byte>, object?, int)
Constructs a new Slice decoder over a byte buffer.
public SliceDecoder(ReadOnlyMemory<byte> buffer, object? decodingContext = null, int maxCollectionAllocation = -1)
Parameters
bufferReadOnlyMemory<byte>The byte buffer.
decodingContextobjectThe decoding context.
maxCollectionAllocationintThe maximum cumulative allocation in bytes when decoding strings, sequences, and dictionaries from this buffer.
-1(the default) is equivalent to 8 times the buffer length, clamped to MaxValue.
Properties
Consumed
Gets the number of bytes decoded in the underlying buffer.
public readonly long Consumed { get; }
Property Value
DecodingContext
Gets the decoding context.
public readonly object? DecodingContext { get; }
Property Value
Remarks
The decoding context is a kind of cookie: the code that creates the decoder can store this context in the decoder for later retrieval.
End
Gets a value indicating whether this decoder has reached the end of its underlying buffer.
public readonly bool End { get; }
Property Value
Remaining
Gets the number of bytes remaining in the underlying buffer.
public readonly long Remaining { get; }
Property Value
- long
The number of bytes remaining in the underlying buffer.
Methods
CheckBoolValue(bool)
Checks if the in memory representation of the bool value is valid according to the Slice encoding.
public static void CheckBoolValue(bool value)
Parameters
valueboolThe value to check.
Exceptions
- InvalidDataException
If the value is out of the bool type accepted range.
CopyTo(IBufferWriter<byte>)
Copy all bytes from the underlying reader into the destination buffer writer.
public void CopyTo(IBufferWriter<byte> destination)
Parameters
destinationIBufferWriter<byte>The destination buffer writer.
Remarks
This method also moves the reader's Consumed property.
CopyTo(Span<byte>)
Copy bytes from the underlying reader into the destination to fill completely destination.
public void CopyTo(Span<byte> destination)
Parameters
Remarks
This method also moves the reader's Consumed property.
DecodeBool()
Decodes a slice bool into a bool.
public bool DecodeBool()
Returns
- bool
The bool decoded by this decoder.
DecodeFloat32()
Decodes a Slice float32 into a float.
public float DecodeFloat32()
Returns
- float
The float decoded by this decoder.
DecodeFloat64()
Decodes a Slice float64 into a double.
public double DecodeFloat64()
Returns
- double
The double decoded by this decoder.
DecodeInt16()
Decodes a Slice int16 into a short.
public short DecodeInt16()
Returns
- short
The short decoded by this decoder.
DecodeInt32()
Decodes a Slice int32 into an int.
public int DecodeInt32()
Returns
- int
The int decoded by this decoder.
DecodeInt64()
Decodes a Slice int64 into a long.
public long DecodeInt64()
Returns
- long
The long decoded by this decoder.
DecodeInt8()
Decodes a Slice int8 into an sbyte.
public sbyte DecodeInt8()
Returns
- sbyte
The sbyte decoded by this decoder.
DecodeSize()
Decodes a size encoded on a variable number of bytes.
public int DecodeSize()
Returns
- int
The size decoded by this decoder.
DecodeString()
Decodes a Slice string into a string.
public string DecodeString()
Returns
- string
The string decoded by this decoder.
DecodeTagged<T>(int, DecodeFunc<T>)
Decodes a tagged field.
public T? DecodeTagged<T>(int tag, DecodeFunc<T> decodeFunc)
Parameters
tagintThe tag.
decodeFuncDecodeFunc<T>A decode function that decodes the value of this tagged field.
Returns
- T
The decoded value of the tagged field, or null if not found.
Type Parameters
TThe type of the decoded value.
Remarks
We return a T? and not a T to avoid ambiguities in the generated code with nullable reference types such as string?.
DecodeUInt16()
Decodes a Slice uint16 into a ushort.
public ushort DecodeUInt16()
Returns
- ushort
The ushort decoded by this decoder.
DecodeUInt32()
Decodes a Slice uint32 into a uint.
public uint DecodeUInt32()
Returns
- uint
The uint decoded by this decoder.
DecodeUInt64()
Decodes a Slice uint64 into a ulong.
public ulong DecodeUInt64()
Returns
- ulong
The ulong decoded by this decoder.
DecodeUInt8()
Decodes a Slice uint8 into a byte.
public byte DecodeUInt8()
Returns
- byte
The byte decoded by this decoder.
DecodeVarInt32()
Decodes a Slice varint32 into an int.
public int DecodeVarInt32()
Returns
- int
The int decoded by this decoder.
DecodeVarInt62()
Decodes a Slice varint62 into a long.
public long DecodeVarInt62()
Returns
- long
The long decoded by this decoder.
DecodeVarUInt32()
Decodes a Slice varuint32 into a uint.
public uint DecodeVarUInt32()
Returns
- uint
The uint decoded by this decoder.
DecodeVarUInt62()
Decodes a Slice varuint62 into a ulong.
public ulong DecodeVarUInt62()
Returns
- ulong
The ulong decoded by this decoder.
GetBitSequenceReader(int)
Gets a bit sequence reader to read the underlying bit sequence later on.
public BitSequenceReader GetBitSequenceReader(int bitSequenceSize)
Parameters
bitSequenceSizeintThe minimum number of bits in the sequence.
Returns
- BitSequenceReader
A bit sequence reader.
IncreaseCollectionAllocation(int, int)
Increases the number of bytes in the decoder's collection allocation.
public void IncreaseCollectionAllocation(int count, int elementSize)
Parameters
- See Also
Skip(int)
Skip the given number of bytes.
public void Skip(int count)
Parameters
countintThe number of bytes to skip.
SkipSize()
Skip Slice size.
public void SkipSize()
SkipTagged()
Skips the remaining tagged fields.
public void SkipTagged()
TryDecodeUInt8(out byte)
Tries to decode a Slice uint8 into a byte.
public bool TryDecodeUInt8(out byte value)
Parameters
valuebyteWhen this method returns true, this value is set to the decoded byte. Otherwise, this value is set to its default value.
Returns
- bool
true if the decoder is not at the end of the buffer and the decode operation succeeded; otherwise, false.
TryDecodeVarUInt62(out ulong)
Tries to decode a Slice varuint62 into a ulong.
public bool TryDecodeVarUInt62(out ulong value)
Parameters
valueulongWhen this method returns true, this value is set to the decoded ulong. Otherwise, this value is set to its default value.