Table of Contents

Class ByteArrayBuilder

Namespace
Plugin.ByteArrays
Assembly
Plugin.ByteArrays.dll

Provides a builder for constructing byte arrays from various types and encodings.

public sealed class ByteArrayBuilder : IDisposable, IAsyncDisposable
Inheritance
ByteArrayBuilder
Implements
Inherited Members
Extension Methods

Constructors

ByteArrayBuilder()

Initializes a new instance of the ByteArrayBuilder class.

public ByteArrayBuilder()

ByteArrayBuilder(int)

Initializes a new instance of the ByteArrayBuilder class with the specified initial capacity.

public ByteArrayBuilder(int initialCapacity)

Parameters

initialCapacity int

The initial capacity of the underlying stream.

Properties

Capacity

Gets the current capacity of the underlying stream.

public int Capacity { get; }

Property Value

int

Length

Gets the current length of the byte array being built.

public int Length { get; }

Property Value

int

Methods

Append(DateTime)

Append a DateTime to the ByteArrayBuilder.

public ByteArrayBuilder Append(DateTime dateTime)

Parameters

dateTime DateTime

The DateTime to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

Append(DateTimeOffset)

Append a DateTimeOffset to the ByteArrayBuilder.

public ByteArrayBuilder Append(DateTimeOffset dateTimeOffset)

Parameters

dateTimeOffset DateTimeOffset

The DateTimeOffset to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

Append(Guid)

Append a GUID to the ByteArrayBuilder.

public ByteArrayBuilder Append(Guid guidValue)

Parameters

guidValue Guid

The GUID to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

Append(IPAddress)

Append an IPAddress to the ByteArrayBuilder.

public ByteArrayBuilder Append(IPAddress ipAddress)

Parameters

ipAddress IPAddress

The IPAddress to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

Append(TimeSpan)

Append a TimeSpan to the ByteArrayBuilder.

public ByteArrayBuilder Append(TimeSpan timeSpan)

Parameters

timeSpan TimeSpan

The TimeSpan to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendAsciiString(string)

Append an ASCII encoded string to the ByteArrayBuilder.

public ByteArrayBuilder AppendAsciiString(string value)

Parameters

value string

The input string.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

Remarks

If the input string is empty or null, nothing is appended.

AppendBase64String(string)

Append a Base64 string to the ByteArrayBuilder.

public ByteArrayBuilder AppendBase64String(string value)

Parameters

value string

The input Base64 string.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

Remarks

If the input string is empty or null, nothing is appended.

Exceptions

FormatException

Thrown if the input string is not a valid Base64 string.

AppendBigEndian(short)

Append a 16-bit integer in big-endian format.

public ByteArrayBuilder AppendBigEndian(short value)

Parameters

value short

The value to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendBigEndian(int)

Append a 32-bit integer in big-endian format.

public ByteArrayBuilder AppendBigEndian(int value)

Parameters

value int

The value to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendBigEndian(long)

Append a 64-bit integer in big-endian format.

public ByteArrayBuilder AppendBigEndian(long value)

Parameters

value long

The value to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendFromStream(Stream, int)

Append data from a stream.

public ByteArrayBuilder AppendFromStream(Stream stream, int count = -1)

Parameters

stream Stream

The stream to read from.

count int

The number of bytes to read, or -1 to read all.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendHexString(string)

Append a hexadecimal string to the ByteArrayBuilder.

public ByteArrayBuilder AppendHexString(string value)

Parameters

value string

The input hexadecimal string.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

Remarks

The input string must have an even number of characters, as each pair of characters represents a byte. If the string is empty or null, nothing is appended.

Exceptions

ArgumentException

Thrown if the input string is not a valid hexadecimal string.

AppendIf(bool, Func<ByteArrayBuilder, ByteArrayBuilder>)

Conditionally append data based on a condition.

public ByteArrayBuilder AppendIf(bool condition, Func<ByteArrayBuilder, ByteArrayBuilder> action)

Parameters

condition bool

The condition to evaluate.

action Func<ByteArrayBuilder, ByteArrayBuilder>

The action to perform if condition is true.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendLengthPrefixedString(string, Encoding)

Append a length-prefixed string to the ByteArrayBuilder.

public ByteArrayBuilder AppendLengthPrefixedString(string value, Encoding encoding)

Parameters

value string

The input string.

encoding Encoding

The encoding to use.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendLittleEndian(short)

Append a 16-bit integer in little-endian format.

public ByteArrayBuilder AppendLittleEndian(short value)

Parameters

value short

The value to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendLittleEndian(int)

Append a 32-bit integer in little-endian format.

public ByteArrayBuilder AppendLittleEndian(int value)

Parameters

value int

The value to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendLittleEndian(long)

Append a 64-bit integer in little-endian format.

public ByteArrayBuilder AppendLittleEndian(long value)

Parameters

value long

The value to append.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendMany<T>(IEnumerable<T>)

Append multiple values from a collection.

public ByteArrayBuilder AppendMany<T>(IEnumerable<T> values)

Parameters

values IEnumerable<T>

The collection of values.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

Type Parameters

T

The type of values to append.

AppendNullTerminatedString(string, Encoding)

Append a null-terminated string to the ByteArrayBuilder.

public ByteArrayBuilder AppendNullTerminatedString(string value, Encoding encoding)

Parameters

value string

The input string.

encoding Encoding

The encoding to use.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendRepeated(byte, int)

Append a byte value repeated a specified number of times.

public ByteArrayBuilder AppendRepeated(byte value, int count)

Parameters

value byte

The byte value to repeat.

count int

The number of times to repeat the value.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendRepeated(byte[], int)

Append a byte pattern repeated a specified number of times.

public ByteArrayBuilder AppendRepeated(byte[] pattern, int times)

Parameters

pattern byte[]

The pattern to repeat.

times int

The number of times to repeat the pattern.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendString(string, Encoding)

Append a string with custom encoding to the ByteArrayBuilder.

public ByteArrayBuilder AppendString(string value, Encoding encoding)

Parameters

value string

The input string.

encoding Encoding

The encoding to use.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendUtf16String(string)

Append a UTF-16 encoded string to the ByteArrayBuilder.

public ByteArrayBuilder AppendUtf16String(string value)

Parameters

value string

The input string.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendUtf32String(string)

Append a UTF-32 encoded string to the ByteArrayBuilder.

public ByteArrayBuilder AppendUtf32String(string value)

Parameters

value string

The input string.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

AppendUtf8String(string)

Append a UTF-8 encoded string to the ByteArrayBuilder.

public ByteArrayBuilder AppendUtf8String(string value)

Parameters

value string

The input string.

Returns

ByteArrayBuilder

The ByteArrayBuilder.

Remarks

If the input string is empty or null, nothing is appended.

Append<T>(T)

Appends a value of any supported type to the byte array.

public ByteArrayBuilder Append<T>(T value)

Parameters

value T

The value to append. Supported types include primitives, enums, and byte arrays.

Returns

ByteArrayBuilder

The current ByteArrayBuilder instance.

Type Parameters

T

The type of the value to append.

Exceptions

ArgumentException

Thrown if the type is not supported.

Clear()

Clears the contents of the builder, resetting it to an empty state.

public ByteArrayBuilder Clear()

Returns

ByteArrayBuilder

The current ByteArrayBuilder instance for method chaining.

Dispose()

Releases the resources used by the ByteArrayBuilder.

public void Dispose()

DisposeAsync()

Asynchronously releases the resources used by the ByteArrayBuilder.

public ValueTask DisposeAsync()

Returns

ValueTask

A task that represents the asynchronous dispose operation.

ToArraySegment()

Convert to ArraySegment.

public ArraySegment<byte> ToArraySegment()

Returns

ArraySegment<byte>

An ArraySegment representing the built data.

ToByteArray(int?)

Returns the byte array built by this instance.

public byte[] ToByteArray(int? maxSize = null)

Parameters

maxSize int?

The maximum allowed size of the byte array. If exceeded, an exception is thrown.

Returns

byte[]

The constructed byte array.

Exceptions

InvalidOperationException

Thrown if the resulting array exceeds the specified maxSize.

ToMemory()

Convert to Memory.

public Memory<byte> ToMemory()

Returns

Memory<byte>

A Memory representing the built data.

ToReadOnlyMemory()

Convert to ReadOnlyMemory.

public ReadOnlyMemory<byte> ToReadOnlyMemory()

Returns

ReadOnlyMemory<byte>

A ReadOnlyMemory representing the built data.

ToString()

Returns a string representation of the byte array.

public override string ToString()

Returns

string

A comma-separated string of byte values.

WithCapacity(int)

Creates a new ByteArrayBuilder with the specified initial capacity.

public static ByteArrayBuilder WithCapacity(int initialCapacity)

Parameters

initialCapacity int

The initial capacity to allocate.

Returns

ByteArrayBuilder

A new ByteArrayBuilder instance.

WriteTo(Stream)

Write the contents to a stream.

public void WriteTo(Stream stream)

Parameters

stream Stream

The stream to write to.