ReadValue Method
ReadValue(Int32)
Read the TLV at the current position, return the value as a byte array, and move the position to the byte beyond the current TLV.
public ReadOnlyMemory<byte> ReadValue(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 value, and only the value, of the current TLV. If there is no value (length is 0), the result will be an empty object (Length is 0).
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 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 a value that is the collection of 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.
value = reader.ReadValue(0x7C);
This will return a new ReadOnlyMemory object that points to
01 01 14 02 02 01 80 05 04 00 89 2C 33
Length is 13
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. There's nothing more to read.
Suppose the internal position is at 5, the beginning of the second
sub-element.
value = reader.ReadValue(0x02);
This will return a new ReadOnlyMemory object that points to
01 80
Length is 2
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.
Note that the value returned is a reference to the input encoding. Do not clear or alter the encoding until after the full encoding has been read and each value operated on.