ReadEncoded Method
ReadEncoded(Int32)
Return the entire encoding of the next element.
public ReadOnlyMemory<byte> ReadEncoded(int expectedTag)
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | expectedTag | The tag that should be at the current position. |
Returns
A new ReadOnlyMemory object containing the tag, length and value of the current TLV.
Exceptions
Type | Condition |
---|---|
TlvException | The tag was not the expected value, the tag or length is unsupported, or there was not enough data for the lengths given. |
Remarks
The method will verify that the tag is expected. If it is, it will read the length, verify that the encoding has at least that many bytes, then create a new ReadOnlyMemory object that points to the entire TLV (not just the value). Note that the ReadOnlyMemory object will point to the existing encoding, it will not copy the data into a new buffer.
Note that this will not treat a NestedTlv any different from a single element. That is, if the current position points to a NestedTlv, the method will return the entire encoding, including the sub-elements.
If the tag at the current position in the encoding is not what was given as the expectedTag, or if the tag and/or length make up an invalid encoding, or if there are not enough bytes left in the encoding to read the tag, length, and value, this method will throw an exception.
For example, suppose the encoding is the following.
7C 0D 01 01 14 02 02 01 80 05 04 00 89 2C 33
This is a NestedTlv
7C 0D
01 01
14
02 02
01 80
05 04
00 89 2C 33
Suppose the internal position is at 0, the beginning of full encoding.
encoded = reader.ReadEncoded(0x7C);
This will return a new ReadOnlyMemory object that points to
7C 0D 01 01 14 02 02 01 80 05 04 00 89 2C 33
Length is 15
After the call, the internal position of the reader is at the
position just after the last byte of the value, which is beyond the
end of the full encoding.
Suppose the internal position is at 5, the beginning of the second
sub-element.
value = reader.ReadEncoded(0x02);
This will return a new ReadOnlyMemory object that points to
02 02 01 80
Length is 4
After the call, the internal position of the reader is at the
position just after the last byte of the value, which is the
next TLV: 05 04 etc.