Show / Hide Table of Contents

Struct Base64Encoder

Represents base64 encoder suitable for encoding large binary data using streaming approach.

Implements
IResettable
Inherited Members
ValueType.Equals(object)
ValueType.GetHashCode()
ValueType.ToString()
object.Equals(object, object)
object.GetType()
object.ReferenceEquals(object, object)
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

| Edit this page View Source

MaxBufferedDataSize

Gets the maximum number of bytes that can be buffered by the encoder.

Declaration
public const int MaxBufferedDataSize = 2
Field Value
Type Description
int
| Edit this page View Source

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
| Edit this page View Source

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

| Edit this page View Source

BufferedData

Gets the buffered data.

Declaration
public readonly ReadOnlySpan<byte> BufferedData { get; }
Property Value
Type Description
ReadOnlySpan<byte>
Remarks

The length of returned span is in [0..MaxBufferedDataSize] range.

| Edit this page View Source

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

| Edit this page View Source

EncodeToUtf16(ReadOnlySpan<byte>, ref BufferWriterSlim<char>, bool)

Encodes a block of bytes to base64-encoded characters.

Declaration
public void EncodeToUtf16(ReadOnlySpan<byte> bytes, 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 bytes is greater than MaxInputSize.

| Edit this page View Source

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 bytes is greater than MaxInputSize.

| Edit this page View Source

EncodeToUtf16(ReadOnlySpan<byte>, IBufferWriter<char>, bool)

Encodes a block of bytes to base64-encoded characters.

Declaration
public void EncodeToUtf16(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

chars is null.

ArgumentException

The length of bytes is greater than MaxInputSize.

| Edit this page View Source

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.

| Edit this page View Source

EncodeToUtf8(ReadOnlySpan<byte>, ref BufferWriterSlim<byte>, bool)

Encodes a block of bytes to base64-encoded UTF-8 characters.

Declaration
public void EncodeToUtf8(ReadOnlySpan<byte> bytes, 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 bytes is greater than MaxInputSize.

| Edit this page View Source

EncodeToUtf8(ReadOnlySpan<byte>, MemoryAllocator<byte>?, bool)

Encodes a block of bytes to base64-encoded UTF-8 characters.

Declaration
public MemoryOwner<byte> EncodeToUtf8(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 bytes is greater than MaxInputSize.

| Edit this page View Source

EncodeToUtf8(ReadOnlySpan<byte>, IBufferWriter<byte>, bool)

Encodes a block of bytes to base64-encoded UTF-8 characters.

Declaration
public void EncodeToUtf8(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

chars is null.

ArgumentException

The length of bytes is greater than MaxInputSize.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

Reset()

Resets the internal state of the encoder.

Declaration
public void Reset()

Implements

IResettable

Extension Methods

BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
Collection.ToAsyncEnumerator<TEnumerator, T>(TEnumerator, CancellationToken)
Collection.ToClassicEnumerator<TEnumerator, T>(TEnumerator)
Enumerator.Skip<TEnumerator, T>(ref TEnumerator, int)
ExpressionBuilder.Const<T>(T)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾