Struct Base64Encoder
Represents base64 encoder suitable for encoding large binary data using streaming approach.
Implements
Inherited Members
Namespace: DotNext.Buffers.Text
Assembly: DotNext.dll
Syntax
public struct Base64Encoder : IResettable
Remarks
This type maintains internal state for correct encoding of streaming data.
Therefore, it must be passed by reference to any routine. It's not a ref struct
to allow construction of high-level encoders in the form of classes.
The output can be in the form of UTF-8 encoded bytes or Unicode characters.
Encoding methods should not be intermixed by the caller code.
Fields
View SourceMaxBufferedDataSize
Gets the maximum number of bytes that can be buffered by the encoder.
Declaration
public const int MaxBufferedDataSize = 2
Field Value
| Type | Description |
|---|---|
| int |
MaxCharsToFlush
Gets the maximum number of characters that can be produced by Flush(scoped Span<byte>) or Flush(Span<char>) methods.
Declaration
public const int MaxCharsToFlush = 4
Field Value
| Type | Description |
|---|---|
| int |
MaxInputSize
Gets the maximum size of the input block of bytes to encode.
Declaration
public const int MaxInputSize = 1610612731
Field Value
| Type | Description |
|---|---|
| int |
Properties
View SourceBufferedData
Gets the buffered data.
Declaration
[UnscopedRef]
public readonly ReadOnlySpan<byte> BufferedData { get; }
Property Value
| Type | Description |
|---|---|
| ReadOnlySpan<byte> |
Remarks
The length of returned span is in [0..MaxBufferedDataSize] range.
HasBufferedData
Indicates that the size of the encoded data is not a multiple of 3 and this encoder expects input data.
Declaration
public readonly bool HasBufferedData { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
View SourceEncodeToUtf16(scoped ReadOnlySpan<byte>, scoped ref BufferWriterSlim<char>, bool)
Encodes a block of bytes to base64-encoded characters.
Declaration
public void EncodeToUtf16(scoped ReadOnlySpan<byte> bytes, scoped ref BufferWriterSlim<char> chars, bool flush = false)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | bytes | A block of bytes to encode. |
| BufferWriterSlim<char> | chars | The buffer of characters to write into. |
| bool | flush | true to encode the final block and insert padding if necessary; false to encode a fragment without padding. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The length of |
EncodeToUtf16(ReadOnlySpan<byte>, MemoryAllocator<char>?, bool)
Encodes a block of bytes to base64-encoded characters.
Declaration
public MemoryOwner<char> EncodeToUtf16(ReadOnlySpan<byte> bytes, MemoryAllocator<char>? allocator = null, bool flush = false)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | bytes | A block of bytes to encode. |
| MemoryAllocator<char> | allocator | The allocator of the result buffer. |
| bool | flush | true to encode the final block and insert padding if necessary; false to encode a fragment without padding. |
Returns
| Type | Description |
|---|---|
| MemoryOwner<char> | The buffer containing encoded bytes. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The length of |
EncodeToUtf16(scoped ReadOnlySpan<byte>, IBufferWriter<char>, bool)
Encodes a block of bytes to base64-encoded characters.
Declaration
public void EncodeToUtf16(scoped ReadOnlySpan<byte> bytes, IBufferWriter<char> chars, bool flush = false)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | bytes | A block of bytes to encode. |
| IBufferWriter<char> | chars | The output buffer. |
| bool | flush | true to encode the final block and insert padding if necessary; false to encode a fragment without padding. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentException | The length of |
EncodeToUtf16Async(IAsyncEnumerable<ReadOnlyMemory<byte>>, MemoryAllocator<char>?, CancellationToken)
Encodes a sequence of bytes to characters using base64 encoding.
Declaration
public static IAsyncEnumerable<ReadOnlyMemory<char>> EncodeToUtf16Async(IAsyncEnumerable<ReadOnlyMemory<byte>> bytes, MemoryAllocator<char>? allocator = null, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IAsyncEnumerable<ReadOnlyMemory<byte>> | bytes | A collection of buffers. |
| MemoryAllocator<char> | allocator | Characters buffer allocator. |
| CancellationToken | token | The token that can be used to cancel the encoding. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<ReadOnlyMemory<char>> | A collection of encoded bytes. |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been canceled. |
EncodeToUtf8(scoped ReadOnlySpan<byte>, scoped ref BufferWriterSlim<byte>, bool)
Encodes a block of bytes to base64-encoded UTF-8 characters.
Declaration
public void EncodeToUtf8(scoped ReadOnlySpan<byte> bytes, scoped ref BufferWriterSlim<byte> chars, bool flush = false)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | bytes | A block of bytes to encode. |
| BufferWriterSlim<byte> | chars | The buffer of UTF-8 characters to write into. |
| bool | flush | true to encode the final block and insert padding if necessary; false to encode a fragment without padding. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The length of |
EncodeToUtf8(scoped ReadOnlySpan<byte>, MemoryAllocator<byte>?, bool)
Encodes a block of bytes to base64-encoded UTF-8 characters.
Declaration
public MemoryOwner<byte> EncodeToUtf8(scoped ReadOnlySpan<byte> bytes, MemoryAllocator<byte>? allocator = null, bool flush = false)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | bytes | A block of bytes to encode. |
| MemoryAllocator<byte> | allocator | The allocator of the result buffer. |
| bool | flush | true to encode the final block and insert padding if necessary; false to encode a fragment without padding. |
Returns
| Type | Description |
|---|---|
| MemoryOwner<byte> | The buffer containing encoded bytes. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentException | The length of |
EncodeToUtf8(scoped ReadOnlySpan<byte>, IBufferWriter<byte>, bool)
Encodes a block of bytes to base64-encoded UTF-8 characters.
Declaration
public void EncodeToUtf8(scoped ReadOnlySpan<byte> bytes, IBufferWriter<byte> chars, bool flush = false)
Parameters
| Type | Name | Description |
|---|---|---|
| ReadOnlySpan<byte> | bytes | A block of bytes to encode. |
| IBufferWriter<byte> | chars | The output buffer. |
| bool | flush | true to encode the final block and insert padding if necessary; false to encode a fragment without padding. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
| ArgumentException | The length of |
EncodeToUtf8Async(IAsyncEnumerable<ReadOnlyMemory<byte>>, MemoryAllocator<byte>?, CancellationToken)
Encodes a sequence of bytes to characters using base64 encoding.
Declaration
public static IAsyncEnumerable<ReadOnlyMemory<byte>> EncodeToUtf8Async(IAsyncEnumerable<ReadOnlyMemory<byte>> bytes, MemoryAllocator<byte>? allocator = null, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IAsyncEnumerable<ReadOnlyMemory<byte>> | bytes | A collection of buffers. |
| MemoryAllocator<byte> | allocator | Characters buffer allocator. |
| CancellationToken | token | The token that can be used to cancel the encoding. |
Returns
| Type | Description |
|---|---|
| IAsyncEnumerable<ReadOnlyMemory<byte>> | A collection of encoded bytes. |
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been canceled. |
Flush(scoped Span<byte>)
Flushes the buffered data as base64-encoded UTF-8 characters to the output buffer.
Declaration
public int Flush(scoped Span<byte> output)
Parameters
| Type | Name | Description |
|---|---|---|
| Span<byte> | output | The output buffer of size 4. |
Returns
| Type | Description |
|---|---|
| int | The number of written bytes. |
Flush(Span<char>)
Flushes the buffered data as base64-encoded characters to the output buffer.
Declaration
public int Flush(Span<char> output)
Parameters
| Type | Name | Description |
|---|---|---|
| Span<char> | output | The buffer of characters. |
Returns
| Type | Description |
|---|---|
| int | The number of written characters. |
Reset()
Resets the internal state of the encoder.
Declaration
public void Reset()