Table of Contents

Struct Iso8211Reader

Namespace
EncDotNet.Iso8211
Assembly
EncDotNet.Iso8211.dll

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

data ReadOnlySpan<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

data ReadOnlySpan<byte>

The ISO 8211 data to read.

isFinalBlock bool

true if 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

data ReadOnlySpan<byte>

The ISO 8211 data to read.

isFinalBlock bool

true if this is the final block of data; otherwise, false.

state Iso8211StreamingReaderState

The 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

long

CurrentDepth

Gets the current depth within the ISO 8211 structure.

public readonly int CurrentDepth { get; }

Property Value

int

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

int

CurrentFieldIndex

Gets the index of the current field within the record.

public readonly int CurrentFieldIndex { get; }

Property Value

int

CurrentLeader

Gets the leader information for the current record.

public readonly Iso8211Leader CurrentLeader { get; }

Property Value

Iso8211Leader

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

int

CurrentPosition

Gets the position of the current directory entry or field within the field area.

public readonly int CurrentPosition { get; }

Property Value

int

CurrentState

Gets the current reader state.

public readonly Iso8211ReaderState CurrentState { get; }

Property Value

Iso8211ReaderState

CurrentTag

Gets the tag of the current directory entry or field.

public readonly ReadOnlySpan<byte> CurrentTag { get; }

Property Value

ReadOnlySpan<byte>

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

int

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

bool

HasMoreFields

Gets whether there are more fields to read in the current record.

public readonly bool HasMoreFields { get; }

Property Value

bool

IsFinalBlock

Gets a value indicating whether this is the final block of data.

public readonly bool IsFinalBlock { get; }

Property Value

bool

TokenType

Gets the type of the current token.

public readonly Iso8211TokenType TokenType { get; }

Property Value

Iso8211TokenType

ValueSpan

Gets the data of the current field.

public readonly ReadOnlySpan<byte> ValueSpan { get; }

Property Value

ReadOnlySpan<byte>

Remarks

Only valid when TokenType is Field.

Methods

CopyValueTo(Span<byte>)

Copies the current field data to the specified destination.

public readonly int CopyValueTo(Span<byte> destination)

Parameters

destination Span<byte>

The destination span.

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

encoding Encoding

The 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

true if a token was successfully read; false if 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

tag string

When this method returns, contains the tag string if successful.

Returns

bool

true if 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

value string

When this method returns, contains the value string if successful.

encoding Encoding

The encoding to use. Defaults to ASCII.

Returns

bool

true if 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

position int

The field position within the field area (from the directory entry).

length int

The field length including terminator (from the directory entry).

Returns

bool

true if the field was successfully read; false if 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

true if skipping succeeded; false if 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

true if skipping succeeded; false if more data is needed.