Struct SliceDecoder
Provides methods to decode data encoded with Slice.
public ref struct SliceDecoder
- Inherited Members
- Extension Methods
Constructors
SliceDecoder(ReadOnlySequence<byte>, SliceEncoding, object?, int, IActivator?, int)
Constructs a new Slice decoder over a byte buffer.
public SliceDecoder(ReadOnlySequence<byte> buffer, SliceEncoding encoding, object? proxyDecodingContext = null, int maxCollectionAllocation = -1, IActivator? activator = null, int maxDepth = 3)
Parameters
buffer
ReadOnlySequence<byte>The byte buffer.
encoding
SliceEncodingThe Slice encoding version.
proxyDecodingContext
objectThe proxy decoding context.
maxCollectionAllocation
intThe 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.activator
IActivatorThe activator for decoding Slice1-encoded classes and exceptions.
maxDepth
intThe maximum depth when decoding a class recursively. The default is
3
.
SliceDecoder(ReadOnlyMemory<byte>, SliceEncoding, object?, int, IActivator?, int)
Constructs a new Slice decoder over a byte buffer.
public SliceDecoder(ReadOnlyMemory<byte> buffer, SliceEncoding encoding, object? proxyDecodingContext = null, int maxCollectionAllocation = -1, IActivator? activator = null, int maxDepth = 3)
Parameters
buffer
ReadOnlyMemory<byte>The byte buffer.
encoding
SliceEncodingThe Slice encoding version.
proxyDecodingContext
objectThe proxy decoding context.
maxCollectionAllocation
intThe 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.activator
IActivatorThe activator for decoding Slice1-encoded classes and exceptions.
maxDepth
intThe maximum depth when decoding a class recursively. The default is
3
.
Properties
Consumed
Gets the number of bytes decoded in the underlying buffer.
public readonly long Consumed { get; }
Property Value
Encoding
Gets the Slice encoding decoded by this decoder.
public readonly SliceEncoding Encoding { get; }
Property Value
End
Gets a value indicating whether this decoder has reached the end of its underlying buffer.
public readonly bool End { get; }
Property Value
ProxyDecodingContext
Gets the proxy decoding context.
public readonly object? ProxyDecodingContext { get; }
Property Value
Remarks
The proxy decoding context is a kind of cookie: the code that creates the decoder can store this context in the decoder for later retrieval.
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
CopyTo(IBufferWriter<byte>)
Copy all bytes from the underlying reader into the destination buffer writer.
public void CopyTo(IBufferWriter<byte> destination)
Parameters
destination
IBufferWriter<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.
DecodeClass<T>()
Decodes a class instance.
public T DecodeClass<T>() where T : SliceClass
Returns
- T
The decoded class instance.
Type Parameters
T
The class type.
DecodeException(string?)
Decodes a Slice exception.
public SliceException DecodeException(string? message = null)
Parameters
message
stringThe error message.
Returns
- SliceException
The decoded Slice exception.
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.
DecodeNullableClass<T>()
Decodes a nullable class instance.
public T? DecodeNullableClass<T>() where T : class
Returns
- T
The class instance, or null.
Type Parameters
T
The class type.
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 Slice2-encoded tagged field.
public T? DecodeTagged<T>(int tag, DecodeFunc<T> decodeFunc)
Parameters
tag
intThe tag.
decodeFunc
DecodeFunc<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
T
The 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?.
DecodeTagged<T>(int, TagFormat, DecodeFunc<T>, bool)
Decodes a Slice1-encoded tagged field.
public T? DecodeTagged<T>(int tag, TagFormat tagFormat, DecodeFunc<T> decodeFunc, bool useTagEndMarker)
Parameters
tag
intThe tag.
tagFormat
TagFormatThe expected tag format of this tag when found in the underlying buffer.
decodeFunc
DecodeFunc<T>A decode function that decodes the value of this tag.
useTagEndMarker
boolWhen true, a tag end marker marks the end of the tagged fields. When false, the end of the buffer marks the end of the tagged fields.
Returns
- T
The decoded value of the tagged field, or null if not found.
Type Parameters
T
The 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
bitSequenceSize
intThe minimum number of bits in the sequence.
Returns
- BitSequenceReader
A bit sequence reader.
IncreaseCollectionAllocation(int)
Increases the number of bytes in the decoder's collection allocation.
public void IncreaseCollectionAllocation(int byteCount)
Parameters
byteCount
intThe number of bytes to add.
Exceptions
- InvalidDataException
Thrown when the total number of bytes exceeds the max collection allocation.
- See Also
Skip(int)
Skip the given number of bytes.
public void Skip(int count)
Parameters
count
intThe number of bytes to skip.
SkipSize()
Skip Slice size.
public void SkipSize()
SkipTagged(bool)
Skips the remaining tagged fields.
public void SkipTagged(bool useTagEndMarker = true)
Parameters
useTagEndMarker
boolWhether or not the tagged fields use a tag end marker (Slice1 only).
TryDecodeUInt8(out byte)
Tries to decode a Slice uint8 into a byte.
public bool TryDecodeUInt8(out byte value)
Parameters
value
byteWhen 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
value
ulongWhen this method returns true, this value is set to the decoded ulong. Otherwise, this value is set to its default value.