Struct SliceEncoder
Provides methods to encode data with Slice.
public ref struct SliceEncoder
- Inherited Members
- Extension Methods
-
CompressionFormatSliceEncoderExtensions.EncodeCompressionFormat(ref SliceEncoder, CompressionFormat)IceObjectProxySliceEncoderExtensions.EncodeNullableIceObjectProxy(ref SliceEncoder, IceObjectProxy?)
Constructors
SliceEncoder(IBufferWriter<byte>, SliceEncoding, ClassFormat)
Constructs a Slice encoder.
public SliceEncoder(IBufferWriter<byte> bufferWriter, SliceEncoding encoding, ClassFormat classFormat = ClassFormat.Compact)
Parameters
bufferWriterIBufferWriter<byte>A buffer writer that writes to byte buffers. See important remarks below.
encodingSliceEncodingThe Slice encoding.
classFormatClassFormatThe class format (Slice1 only).
Remarks
Warning: the Slice encoding requires rewriting buffers, and many buffer writers do not support this behavior. It is safe to use a pipe writer or a buffer writer that writes to a single fixed-size buffer (without reallocation).
Properties
EncodedByteCount
Gets the number of bytes encoded by this encoder into the underlying buffer writer.
public readonly int EncodedByteCount { get; }
Property Value
Encoding
Gets the Slice encoding of this encoder.
public readonly SliceEncoding Encoding { get; }
Property Value
Methods
EncodeBool(bool)
Encodes a bool into a Slice bool.
public void EncodeBool(bool v)
Parameters
vboolThe boolean to encode.
EncodeClass(SliceClass)
Encodes a class instance.
public void EncodeClass(SliceClass v)
Parameters
vSliceClassThe class instance to encode.
EncodeFloat32(float)
Encodes a float into a Slice float32.
public void EncodeFloat32(float v)
Parameters
vfloatThe float to encode.
EncodeFloat64(double)
Encodes a double into a Slice float64.
public void EncodeFloat64(double v)
Parameters
vdoubleThe double to encode.
EncodeInt16(short)
Encodes a short into a Slice int16.
public void EncodeInt16(short v)
Parameters
vshortThe short to encode.
EncodeInt32(int)
Encodes an int into a Slice int32.
public void EncodeInt32(int v)
Parameters
vintThe int to encode.
EncodeInt32(int, Span<byte>)
Encodes an int as a Slice int32 into a span of 4 bytes.
public static void EncodeInt32(int value, Span<byte> into)
Parameters
valueintThe value to encode.
intoSpan<byte>The destination byte buffer, which must be 4 bytes long.
EncodeInt64(long)
Encodes a long into a Slice int64.
public void EncodeInt64(long v)
Parameters
vlongThe long to encode.
EncodeInt8(sbyte)
Encodes an sbyte into a Slice int8.
public void EncodeInt8(sbyte v)
Parameters
vsbyteThe sbyte to encode.
EncodeNullableClass(SliceClass?)
Encodes a class instance, or null.
public void EncodeNullableClass(SliceClass? v)
Parameters
vSliceClassThe class instance to encode, or null.
EncodeSize(int)
Encodes a size on variable number of bytes.
public void EncodeSize(int value)
Parameters
valueintThe size to encode.
EncodeString(string)
Encodes a string into a Slice string.
public void EncodeString(string v)
Parameters
vstringThe string to encode.
EncodeTagged<T>(int, int, T, EncodeAction<T>)
Encodes a non-null encoded tagged value. The number of bytes needed to encode the value is known before encoding the value. With Slice1 encoding this method always use the VSize tag format.
public void EncodeTagged<T>(int tag, int size, T v, EncodeAction<T> encodeAction) where T : notnull
Parameters
tagintThe tag.
sizeintThe number of bytes needed to encode the value.
vTThe value to encode.
encodeActionEncodeAction<T>The delegate that encodes the value after the tag header.
Type Parameters
TThe type of the value being encoded.
EncodeTagged<T>(int, TagFormat, T, EncodeAction<T>)
Encodes a non-null Slice1 encoded tagged value. The number of bytes needed to encode the value is not known before encoding this value.
public void EncodeTagged<T>(int tag, TagFormat tagFormat, T v, EncodeAction<T> encodeAction) where T : notnull
Parameters
tagintThe tag. Must be either FSize or OptimizedVSize.
tagFormatTagFormatThe tag format.
vTThe value to encode.
encodeActionEncodeAction<T>The delegate that encodes the value after the tag header.
Type Parameters
TThe type of the value being encoded.
Exceptions
- ArgumentException
Thrown if
tagFormatis VSize.
EncodeTagged<T>(int, T, EncodeAction<T>)
Encodes a non-null Slice2 encoded tagged value. The number of bytes needed to encode the value is not known before encoding this value (Slice2 only).
public void EncodeTagged<T>(int tag, T v, EncodeAction<T> encodeAction) where T : notnull
Parameters
tagintThe tag.
vTThe value to encode.
encodeActionEncodeAction<T>The delegate that encodes the value after the tag header.
Type Parameters
TThe type of the value being encoded.
EncodeUInt16(ushort)
Encodes a ushort into a Slice uint16.
public void EncodeUInt16(ushort v)
Parameters
vushortThe ushort to encode.
EncodeUInt32(uint)
Encodes a uint into a Slice uint32.
public void EncodeUInt32(uint v)
Parameters
vuintThe uint to encode.
EncodeUInt64(ulong)
Encodes a ulong into a Slice uint64.
public void EncodeUInt64(ulong v)
Parameters
vulongThe ulong to encode.
EncodeUInt8(byte)
Encodes a byte into a Slice uint8.
public void EncodeUInt8(byte v)
Parameters
vbyteThe byte to encode.
EncodeVarInt32(int)
Encodes an int into a Slice varint32.
public void EncodeVarInt32(int v)
Parameters
vintThe int to encode.
EncodeVarInt62(long)
Encodes a long into a Slice varint62, with the minimum number of bytes required by the encoding.
public void EncodeVarInt62(long v)
Parameters
vlongThe long to encode. It must be in the range [-2^61..2^61 - 1].
EncodeVarUInt32(uint)
Encodes a uint into a Slice varuint32.
public void EncodeVarUInt32(uint v)
Parameters
vuintThe uint to encode.
EncodeVarUInt62(ulong)
Encodes a ulong into a Slice varuint62, with the minimum number of bytes required by the encoding.
public void EncodeVarUInt62(ulong v)
Parameters
vulongThe ulong to encode. It must be in the range [0..2^62 - 1].
EncodeVarUInt62(ulong, Span<byte>)
Encodes a ulong as a Slice varuint62 into a span of bytes using a fixed number of bytes.
public static void EncodeVarUInt62(ulong value, Span<byte> into)
Parameters
valueulongThe value to encode.
intoSpan<byte>The destination byte buffer, which must be 1, 2, 4 or 8 bytes long.
GetBitSequenceWriter(int)
Allocates a new bit sequence in the underlying buffer(s) and returns a writer for this bit sequence.
public BitSequenceWriter GetBitSequenceWriter(int bitSequenceSize)
Parameters
bitSequenceSizeintThe minimum number of bits in the bit sequence.
Returns
- BitSequenceWriter
The bit sequence writer.
GetPlaceholderSpan(int)
Gets a placeholder to be filled-in later.
public Span<byte> GetPlaceholderSpan(int size)
Parameters
sizeintThe size of the placeholder, typically a small number like 4.
Returns
Remarks
We make the assumption the underlying buffer writer allows rewriting memory it provided even after successive calls to GetMemory/GetSpan and Advance.
GetSizeLength(int)
Computes the minimum number of bytes needed to encode a variable-length size.
public readonly int GetSizeLength(int size)
Parameters
sizeintThe size.
Returns
- int
The minimum number of bytes.
GetVarInt62EncodedSize(long)
Computes the minimum number of bytes required to encode a long value using the Slice encoding's variable-size encoded representation.
public static int GetVarInt62EncodedSize(long value)
Parameters
valuelongThe long value.
Returns
- int
The minimum number of bytes required to encode
value. Can be 1, 2, 4 or 8.
GetVarUInt62EncodedSize(ulong)
Computes the minimum number of bytes required to encode a ulong value using the Slice encoding's variable-size encoded representation.
public static int GetVarUInt62EncodedSize(ulong value)
Parameters
valueulongThe ulong value.
Returns
- int
The minimum number of bytes required to encode
value. Can be 1, 2, 4 or 8.
WriteByteSpan(ReadOnlySpan<byte>)
Copies a span of bytes to the underlying buffer writer.
public void WriteByteSpan(ReadOnlySpan<byte> span)
Parameters
spanReadOnlySpan<byte>The span to copy.