Struct Iso8211Reader
Provides a high-performance, low-allocation, forward-only reader for ISO 8211 (ISO/IEC 8211) formatted data.
public ref struct Iso8211Reader
- Inherited Members
Remarks
This reader is modeled after Utf8JsonReader and is designed for scenarios requiring minimal memory allocations and maximum throughput.
The reader exposes ISO 8211 data as a series of tokens that can be read or skipped. Call Read() to advance to the next token, then inspect TokenType and use the various value accessors to retrieve data.
Constructors
Iso8211Reader(ReadOnlySpan<byte>)
Initializes a new instance of the Iso8211Reader struct with a span of bytes.
public Iso8211Reader(ReadOnlySpan<byte> data)
Parameters
dataReadOnlySpan<byte>The ISO 8211 data to read.
Iso8211Reader(ReadOnlySpan<byte>, bool)
Initializes a new instance of the Iso8211Reader struct with a span of bytes and an indication of whether this is the final block of data.
public Iso8211Reader(ReadOnlySpan<byte> data, bool isFinalBlock)
Parameters
dataReadOnlySpan<byte>The ISO 8211 data to read.
isFinalBlockbooltrueif this is the final block of data; otherwise,false.
Iso8211Reader(ReadOnlySpan<byte>, bool, Iso8211StreamingReaderState)
Initializes a new instance of the Iso8211Reader struct with a span of bytes, an indication of whether this is the final block of data, and a previous reader state.
public Iso8211Reader(ReadOnlySpan<byte> data, bool isFinalBlock, Iso8211StreamingReaderState state)
Parameters
dataReadOnlySpan<byte>The ISO 8211 data to read.
isFinalBlockbooltrueif this is the final block of data; otherwise,false.stateIso8211StreamingReaderStateThe previous reader state to restore.
Remarks
This constructor is used in streaming scenarios where data arrives incrementally.
The data should contain only the unprocessed portion of the stream.
Properties
BytesConsumed
Gets the number of bytes consumed so far.
public readonly long BytesConsumed { get; }
Property Value
CurrentDepth
Gets the current depth within the ISO 8211 structure.
public readonly int CurrentDepth { get; }
Property Value
Remarks
0 = At root level (between records or at EndOfData) 1 = Inside a record 2 = Inside a field
CurrentDirectoryIndex
Gets the index of the current directory entry within the record.
public readonly int CurrentDirectoryIndex { get; }
Property Value
CurrentFieldIndex
Gets the index of the current field within the record.
public readonly int CurrentFieldIndex { get; }
Property Value
CurrentLeader
Gets the leader information for the current record.
public readonly Iso8211Leader CurrentLeader { get; }
Property Value
Remarks
Only valid when TokenType is StartRecord or when inside a record (DirectoryEntry, Field).
CurrentLength
Gets the length of the current directory entry or field.
public readonly int CurrentLength { get; }
Property Value
CurrentPosition
Gets the position of the current directory entry or field within the field area.
public readonly int CurrentPosition { get; }
Property Value
CurrentState
Gets the current reader state.
public readonly Iso8211ReaderState CurrentState { get; }
Property Value
CurrentTag
Gets the tag of the current directory entry or field.
public readonly ReadOnlySpan<byte> CurrentTag { get; }
Property Value
Remarks
Only valid when TokenType is DirectoryEntry or Field.
DirectoryEntryCount
Gets the total number of directory entries (and fields) in the current record.
public readonly int DirectoryEntryCount { get; }
Property Value
Remarks
Only valid after TokenType becomes StartRecord.
HasMoreDirectoryEntries
Gets whether there are more directory entries to read in the current record.
public readonly bool HasMoreDirectoryEntries { get; }
Property Value
HasMoreFields
Gets whether there are more fields to read in the current record.
public readonly bool HasMoreFields { get; }
Property Value
IsFinalBlock
Gets a value indicating whether this is the final block of data.
public readonly bool IsFinalBlock { get; }
Property Value
TokenType
Gets the type of the current token.
public readonly Iso8211TokenType TokenType { get; }
Property Value
ValueSpan
Gets the data of the current field.
public readonly ReadOnlySpan<byte> ValueSpan { get; }
Property Value
Remarks
Methods
CopyValueTo(Span<byte>)
Copies the current field data to the specified destination.
public readonly int CopyValueTo(Span<byte> destination)
Parameters
Returns
- int
The number of bytes copied.
GetCurrentState()
Gets the current state of the reader for use in streaming scenarios.
public readonly Iso8211StreamingReaderState GetCurrentState()
Returns
- Iso8211StreamingReaderState
The current reader state that can be used to resume reading.
GetTagString()
Gets the current field tag as a string.
public readonly string GetTagString()
Returns
- string
The tag string.
Exceptions
- InvalidOperationException
Thrown when no tag is available.
GetValueString(Encoding?)
Gets the current field data as a string using the specified encoding.
public readonly string GetValueString(Encoding? encoding = null)
Parameters
encodingEncodingThe encoding to use. Defaults to ASCII.
Returns
- string
The field data as a string.
Read()
Reads the next ISO 8211 token from the input data.
public bool Read()
Returns
- bool
trueif a token was successfully read;falseif the end of data was reached.
Skip()
Skips the current token and any children.
public void Skip()
Remarks
If the current token is StartRecord, skips the entire record. If the current token is Field, skips to the next field or end of record.
SkipDirectory()
Skips all remaining directory entries and positions at the first field.
public void SkipDirectory()
SkipRecord()
Skips the current record and positions at the start of the next record.
public void SkipRecord()
TryGetTagString(out string)
Tries to get the current field tag as a string.
public readonly bool TryGetTagString(out string tag)
Parameters
tagstringWhen this method returns, contains the tag string if successful.
Returns
- bool
trueif the tag was successfully retrieved; otherwise,false.
TryGetValueString(out string, Encoding?)
Tries to get the current field data as a string.
public readonly bool TryGetValueString(out string value, Encoding? encoding = null)
Parameters
valuestringWhen this method returns, contains the value string if successful.
encodingEncodingThe encoding to use. Defaults to ASCII.
Returns
- bool
trueif the value was successfully retrieved; otherwise,false.
TryReadFieldDirect(int, int)
Reads the next field using pre-parsed directory entry information, avoiding redundant directory re-parsing.
public bool TryReadFieldDirect(int position, int length)
Parameters
positionintThe field position within the field area (from the directory entry).
lengthintThe field length including terminator (from the directory entry).
Returns
- bool
trueif the field was successfully read;falseif there are no more fields.
Remarks
This method is intended for use by higher-level readers that have already parsed the directory entries and can supply the position and length directly.
TrySkip()
Attempts to skip the current token and any children.
public bool TrySkip()
Returns
- bool
trueif skipping succeeded;falseif more data is needed.
Remarks
If the current token is StartRecord, skips the entire record. If the current token is Field, skips to the next field or end of record.
TrySkipRecord()
Attempts to skip the current record and position at the start of the next record.
public bool TrySkipRecord()
Returns
- bool
trueif skipping succeeded;falseif more data is needed.