Class BinaryGenerator
Abstract class providing mechanisms supporting generating documents from binary template files.
Inherited Members
Namespace: SuperOffice.IO
Assembly: SoCore.dll
Syntax
public abstract class BinaryGenerator : Generator
Constructors
BinaryGenerator(Stream, TagContainer, GeneratorEncoding)
Protected constructor used by sub-classes
Declaration
protected BinaryGenerator(Stream template, TagContainer tags, GeneratorEncoding encoding)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | template | Stream holding a template. |
| TagContainer | tags | Class holding values for the tags used by the generator |
| GeneratorEncoding | encoding | Format of the document. |
Properties
GeneratorState
Abstract class providing mechanisms supporting generating documents from binary template files.
Declaration
protected BinaryGenerator.State GeneratorState { get; }
Property Value
| Type | Description |
|---|---|
| BinaryGenerator.State |
Reader
BinaryReader that the template is read from.
Declaration
protected BinaryReader Reader { get; }
Property Value
| Type | Description |
|---|---|
| BinaryReader |
Writer
BinaryWriter that the document is written to.
Declaration
protected BinaryWriter Writer { get; }
Property Value
| Type | Description |
|---|---|
| BinaryWriter |
Methods
AbortTag()
Abort seeking for an end tag and start seeking for a start tag.
Declaration
protected void AbortTag()
AbortTag(int)
Abort seeking for an end tag and start seeking for a start tag.
Declaration
protected void AbortTag(int bytes)
Parameters
| Type | Name | Description |
|---|---|---|
| int | bytes |
Remarks
After a start tag is found, the search for an end-tag can be aborted.
This method aborts seeking for an end tag. If a next start tag is found
while seeking for a start tag, the input argument will contain the number
of bytes that should be re-parsed looking for a start tag.
Examples
case BinaryGenerator.State.LookForTagEnd:
{
int bytes = StartTag(buffer);
if (bytes > 0)
{
AbortTag(bytes);
SetState(BinaryGenerator.State.LookForTagEnd, bytes);
break;
}
EndTag(int)
Identify a tag end.
Declaration
protected abstract int EndTag(int buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| int | buffer | Last 4 bytes parsed. This method is called each time a new byte is read from the template. |
Returns
| Type | Description |
|---|---|
| int | 0 if no tag has been identified. A positive number is the number of bytes held by the end tag. |
GenerateAsync(Stream, CancellationToken)
Generate the document from the template.
Declaration
public override Task GenerateAsync(Stream output, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | output | |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
Overrides
IsValidTagData(int)
Called for each byte in a tag, to verify that the data within the tags are valid.
Declaration
protected virtual bool IsValidTagData(int buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| int | buffer |
Returns
| Type | Description |
|---|---|
| bool | True if it is valid data. |
Remarks
The default implementation will allways return true.
OnEnd()
Called when the template has been parsed.
Declaration
protected abstract void OnEnd()
OnProcessDataAsync(long, int, State, CancellationToken)
Process data as it is read from the template.
Declaration
protected virtual Task OnProcessDataAsync(long position, int buffer, BinaryGenerator.State state, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| long | position | Current position in the template, where the first byte in the template is 1. |
| int | buffer | Last 4 bytes parsed. This method is called each time a new byte is read from the template. |
| BinaryGenerator.State | state | State of the generator. The state is either to look for a start or to look for an end tag. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
OnProcessTagAsync(long, byte[], CancellationToken)
Called when a tag has been extracted from the template file.
Declaration
protected abstract Task<byte[]> OnProcessTagAsync(long position, byte[] tagBuffer, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| long | position | Position in the template where the tag starts. |
| byte[] | tagBuffer | Buffer holding the tag, including tag start and stop bytes, |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<byte[]> | Buffer holding data to be written to the document instead of the tag. |
OnStart()
Called when starting to parse the template.
Declaration
protected abstract void OnStart()
SetStateAsync(State, int, CancellationToken)
Change the state of the parser between seeking for start or end tag.
Declaration
protected Task SetStateAsync(BinaryGenerator.State state, int tagSize, CancellationToken cancellationToken)
Parameters
| Type | Name | Description |
|---|---|---|
| BinaryGenerator.State | state | The new state. |
| int | tagSize | Size of the tag in bytes. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
StartTag(int)
Identify a tag start.
Declaration
protected abstract int StartTag(int buffer)
Parameters
| Type | Name | Description |
|---|---|---|
| int | buffer | Last 4 bytes parsed. This method is called each time a new byte is read from the template. |
Returns
| Type | Description |
|---|---|
| int | 0 if no tag has been identified. A positive number is the number of bytes held by the start tag. |