Class SimpleSpanEncoding
Custom text encoding based on Span types simple to implement.
public abstract class SimpleSpanEncoding : Encoding, ICloneable
- Inheritance
-
SimpleSpanEncoding
- Implements
- Inherited Members
Constructors
SimpleSpanEncoding(int)
Initializes a new instance of the SimpleSpanEncoding class.
protected SimpleSpanEncoding(int codePage)
Parameters
codePage
intThe code page of the encoding.
Remarks
The encoder and decoder fallback throw exceptions.
SimpleSpanEncoding(int, EncoderFallback, DecoderFallback)
Initializes a new instance of the SimpleSpanEncoding class.
protected SimpleSpanEncoding(int codePage, EncoderFallback encoderFallback, DecoderFallback decoderFallback)
Parameters
codePage
intThe code page of the encoding.
encoderFallback
EncoderFallbackThe encoder fallback.
decoderFallback
DecoderFallbackThe decoder fallback.
Properties
BodyName
Gets a name for the current encoding that can be used with mail agent body tags.
public override string BodyName { get; }
Property Value
EncodingName
Gets the human-readable description of the current encoding.
public override abstract string EncodingName { get; }
Property Value
HeaderName
Gets a name for the current encoding that can be used with mail agent header tags.
public override string HeaderName { get; }
Property Value
IsBrowserDisplay
Gets a value indicating whether the current encoding can be used by browser clients for displaying content.
As it's a custom encoding and the other side may not have it, by default is false
.
public override bool IsBrowserDisplay { get; }
Property Value
IsBrowserSave
Gets a value indicating whether the current encoding can be used by browser clients for saving content.
As it's a custom encoding and the other side may not have it, by default is false
.
public override bool IsBrowserSave { get; }
Property Value
IsMailNewsDisplay
Gets a value indicating whether the current encoding can be used by mail and news clients for displaying content.
As it's a custom encoding and the other side may not have it, by default is false
.
public override bool IsMailNewsDisplay { get; }
Property Value
IsMailNewsSave
Gets a value indicating whether the current encoding can be used by mail and news clients for saving content.
As it's a custom encoding and the other side may not have it, by default is false
.
public override bool IsMailNewsSave { get; }
Property Value
IsSingleByte
Gets a value indicating whether the current encoding uses single-byte code points.
public override bool IsSingleByte { get; }
Property Value
WebName
Gets the name registered with the Internet Assigned Numbers Authority (IANA) for the current encoding. As it's a custom encoding, no name is registered in IANA, return empty string.
public override string WebName { get; }
Property Value
WindowsCodePage
Gets the Windows operating system code page that most closely corresponds to the current encoding.
This property is not in used anymore. By default is 0
.
public override int WindowsCodePage { get; }
Property Value
Methods
Decode(ReadOnlySpan<byte>, SpanStream<char>)
Decodes the bytes into a char stream.
protected abstract void Decode(ReadOnlySpan<byte> bytes, SpanStream<char> buffer)
Parameters
bytes
ReadOnlySpan<byte>The bytes to decode.
buffer
SpanStream<char>The output char stream.
DecodeUnknownBytes(SpanStream<char>, int, params byte[])
Reports invalid encoded bytes and write the fallback text to the stream depending on the fallback strategy.
protected void DecodeUnknownBytes(SpanStream<char> buffer, int index, params byte[] bytesUnknown)
Parameters
buffer
SpanStream<char>The output chars buffer to write the fallback text.
index
intThe index where the invalid data was found.
bytesUnknown
byte[]The invalid unknown bytes found.
Encode(ReadOnlySpan<char>, SpanStream<byte>, bool)
Encodes the characters into the stream.
protected abstract void Encode(ReadOnlySpan<char> chars, SpanStream<byte> buffer, bool isFallbackText = false)
Parameters
chars
ReadOnlySpan<char>The characters to encode.
buffer
SpanStream<byte>The output byte stream.
isFallbackText
boolValue indicating if the unknown char comes from fallback text already.
EncodeUnknownChar(SpanStream<byte>, int, int, bool)
Reports an invalid code-point for this encoding and encode the fallback text.
protected void EncodeUnknownChar(SpanStream<byte> buffer, int codePoint, int index, bool isFallbackText)
Parameters
buffer
SpanStream<byte>The current output byte buffer to encode the fallback text.
codePoint
intThe UTF-32 code-point that cannot be encoded.
index
intThe index where the code-point was found in the buffer.
isFallbackText
boolValue indicating if the unknown char comes from fallback text already.
Exceptions
- FormatException
Thrown when the fallback text cannot be encoded.
GetByteCount(char[])
Calculates the number of bytes produced by encoding all the characters in the specified character array.
public override int GetByteCount(char[] chars)
Parameters
chars
char[]The character array containing the characters to encode.
Returns
- int
The number of bytes produced by encoding all the characters in the specified character array.
GetByteCount(char[], int, int)
Calculates the number of bytes produced by encoding a set of characters from the specified character array.
public override int GetByteCount(char[] chars, int index, int count)
Parameters
chars
char[]The character array containing the set of characters to encode.
index
intThe index of the first character to encode.
count
intThe number of characters to encode.
Returns
- int
The number of bytes produced by encoding the specified characters.
GetByteCount(ReadOnlySpan<char>)
Calculates the number of bytes produced by encoding the characters in the specified character span.
public override int GetByteCount(ReadOnlySpan<char> chars)
Parameters
chars
ReadOnlySpan<char>The span of characters to encode.
Returns
- int
The number of bytes produced by encoding the specified character span.
GetByteCount(string)
Calculates the number of bytes produced by encoding the characters in the specified string.
public override int GetByteCount(string s)
Parameters
s
stringThe string containing the set of characters to encode.
Returns
- int
The number of bytes produced by encoding the specified characters.
GetBytes(char[])
When overridden in a derived class, encodes all the characters in the specified character array into a sequence of bytes.
public override byte[] GetBytes(char[] chars)
Parameters
chars
char[]The character array containing the characters to encode.
Returns
- byte[]
A byte array containing the results of encoding the specified set of characters.
Exceptions
- ArgumentNullException
chars
is null.- EncoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
EncoderFallback is set to EncoderExceptionFallback.
GetBytes(char[], int, int)
When overridden in a derived class, encodes a set of characters from the specified character array into a sequence of bytes.
public override byte[] GetBytes(char[] chars, int index, int count)
Parameters
chars
char[]The character array containing the set of characters to encode.
index
intThe index of the first character to encode.
count
intThe number of characters to encode.
Returns
- byte[]
A byte array containing the results of encoding the specified set of characters.
Exceptions
- ArgumentNullException
chars
is null.- ArgumentOutOfRangeException
index
orcount
is less than zero.-or-
index
andcount
do not denote a valid range inchars
.- EncoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
EncoderFallback is set to EncoderExceptionFallback.
GetBytes(char[], int, int, byte[], int)
When overridden in a derived class, encodes a set of characters from the specified character array into the specified byte array.
public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
Parameters
chars
char[]The character array containing the set of characters to encode.
charIndex
intThe index of the first character to encode.
charCount
intThe number of characters to encode.
bytes
byte[]The byte array to contain the resulting sequence of bytes.
byteIndex
intThe index at which to start writing the resulting sequence of bytes.
Returns
- int
The actual number of bytes written into
bytes
.
Exceptions
- ArgumentNullException
chars
is null.-or-
bytes
is null.- ArgumentOutOfRangeException
charIndex
orcharCount
orbyteIndex
is less than zero.-or-
charIndex
andcharCount
do not denote a valid range inchars
.-or-
byteIndex
is not a valid index inbytes
.- ArgumentException
bytes
does not have enough capacity frombyteIndex
to the end of the array to accommodate the resulting bytes.- EncoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
EncoderFallback is set to EncoderExceptionFallback.
GetBytes(ReadOnlySpan<char>)
Encodes the characters.
public byte[] GetBytes(ReadOnlySpan<char> chars)
Parameters
chars
ReadOnlySpan<char>The characters to encode.
Returns
- byte[]
The output encoded data.
GetBytes(ReadOnlySpan<char>, Span<byte>)
When overridden in a derived class, encodes into a span of bytes a set of characters from the specified read-only span.
public override int GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes)
Parameters
chars
ReadOnlySpan<char>The span containing the set of characters to encode.
bytes
Span<byte>The byte span to hold the encoded bytes.
Returns
- int
The number of encoded bytes.
GetBytes(string)
When overridden in a derived class, encodes all the characters in the specified string into a sequence of bytes.
public override byte[] GetBytes(string s)
Parameters
s
stringThe string containing the characters to encode.
Returns
- byte[]
A byte array containing the results of encoding the specified set of characters.
Exceptions
- ArgumentNullException
s
is null.- EncoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
EncoderFallback is set to EncoderExceptionFallback.
GetBytes(string, int, int, byte[], int)
When overridden in a derived class, encodes a set of characters from the specified string into the specified byte array.
public override int GetBytes(string s, int charIndex, int charCount, byte[] bytes, int byteIndex)
Parameters
s
stringThe string containing the set of characters to encode.
charIndex
intThe index of the first character to encode.
charCount
intThe number of characters to encode.
bytes
byte[]The byte array to contain the resulting sequence of bytes.
byteIndex
intThe index at which to start writing the resulting sequence of bytes.
Returns
- int
The actual number of bytes written into
bytes
.
Exceptions
- ArgumentNullException
s
is null.-or-
bytes
is null.- ArgumentOutOfRangeException
charIndex
orcharCount
orbyteIndex
is less than zero.-or-
charIndex
andcharCount
do not denote a valid range inchars
.-or-
byteIndex
is not a valid index inbytes
.- ArgumentException
bytes
does not have enough capacity frombyteIndex
to the end of the array to accommodate the resulting bytes.- EncoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
EncoderFallback is set to EncoderExceptionFallback.
GetCharCount(byte[])
When overridden in a derived class, calculates the number of characters produced by decoding all the bytes in the specified byte array.
public override int GetCharCount(byte[] bytes)
Parameters
bytes
byte[]The byte array containing the sequence of bytes to decode.
Returns
- int
The number of characters produced by decoding the specified sequence of bytes.
Exceptions
- ArgumentNullException
bytes
is null.- DecoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
DecoderFallback is set to DecoderExceptionFallback.
GetCharCount(byte[], int, int)
When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.
public override int GetCharCount(byte[] bytes, int index, int count)
Parameters
bytes
byte[]The byte array containing the sequence of bytes to decode.
index
intThe index of the first byte to decode.
count
intThe number of bytes to decode.
Returns
- int
The number of characters produced by decoding the specified sequence of bytes.
Exceptions
- ArgumentNullException
bytes
is null.- ArgumentOutOfRangeException
index
orcount
is less than zero.-or-
index
andcount
do not denote a valid range inbytes
.- DecoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
DecoderFallback is set to DecoderExceptionFallback.
GetCharCount(ReadOnlySpan<byte>)
When overridden in a derived class, calculates the number of characters produced by decoding the provided read-only byte span.
public override int GetCharCount(ReadOnlySpan<byte> bytes)
Parameters
bytes
ReadOnlySpan<byte>A read-only byte span to decode.
Returns
- int
The number of characters produced by decoding the byte span.
GetChars(byte[])
When overridden in a derived class, decodes all the bytes in the specified byte array into a set of characters.
public override char[] GetChars(byte[] bytes)
Parameters
bytes
byte[]The byte array containing the sequence of bytes to decode.
Returns
- char[]
A character array containing the results of decoding the specified sequence of bytes.
Exceptions
- ArgumentNullException
bytes
is null.- DecoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
DecoderFallback is set to DecoderExceptionFallback.
GetChars(byte[], int, int)
When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a set of characters.
public override char[] GetChars(byte[] bytes, int index, int count)
Parameters
bytes
byte[]The byte array containing the sequence of bytes to decode.
index
intThe index of the first byte to decode.
count
intThe number of bytes to decode.
Returns
- char[]
A character array containing the results of decoding the specified sequence of bytes.
Exceptions
- ArgumentNullException
bytes
is null.- ArgumentOutOfRangeException
index
orcount
is less than zero.-or-
index
andcount
do not denote a valid range inbytes
.- DecoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
DecoderFallback is set to DecoderExceptionFallback.
GetChars(byte[], int, int, char[], int)
When overridden in a derived class, decodes a sequence of bytes from the specified byte array into the specified character array.
public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
Parameters
bytes
byte[]The byte array containing the sequence of bytes to decode.
byteIndex
intThe index of the first byte to decode.
byteCount
intThe number of bytes to decode.
chars
char[]The character array to contain the resulting set of characters.
charIndex
intThe index at which to start writing the resulting set of characters.
Returns
- int
The actual number of characters written into
chars
.
Exceptions
- ArgumentNullException
bytes
is null.-or-
chars
is null.- ArgumentOutOfRangeException
byteIndex
orbyteCount
orcharIndex
is less than zero.-or-
byteindex
andbyteCount
do not denote a valid range inbytes
.-or-
charIndex
is not a valid index inchars
.- ArgumentException
chars
does not have enough capacity fromcharIndex
to the end of the array to accommodate the resulting characters.- DecoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
DecoderFallback is set to DecoderExceptionFallback.
GetChars(ReadOnlySpan<byte>)
Decodes the bytes.
public char[] GetChars(ReadOnlySpan<byte> bytes)
Parameters
bytes
ReadOnlySpan<byte>The data to decode.
Returns
- char[]
The decoded characters.
GetChars(ReadOnlySpan<byte>, Span<char>)
When overridden in a derived class, decodes all the bytes in the specified read-only byte span into a character span.
public override int GetChars(ReadOnlySpan<byte> bytes, Span<char> chars)
Parameters
bytes
ReadOnlySpan<byte>A read-only span containing the sequence of bytes to decode.
chars
Span<char>The character span receiving the decoded bytes.
Returns
- int
The number of decoded bytes.
GetString(byte[])
When overridden in a derived class, decodes all the bytes in the specified byte array into a string.
public override string GetString(byte[] bytes)
Parameters
bytes
byte[]The byte array containing the sequence of bytes to decode.
Returns
- string
A string that contains the results of decoding the specified sequence of bytes.
Exceptions
- ArgumentException
The byte array contains invalid Unicode code points.
- ArgumentNullException
bytes
is null.- DecoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
DecoderFallback is set to DecoderExceptionFallback.
GetString(byte[], int, int)
When overridden in a derived class, decodes a sequence of bytes from the specified byte array into a string.
public override string GetString(byte[] bytes, int index, int count)
Parameters
bytes
byte[]The byte array containing the sequence of bytes to decode.
index
intThe index of the first byte to decode.
count
intThe number of bytes to decode.
Returns
- string
A string that contains the results of decoding the specified sequence of bytes.
Exceptions
- ArgumentException
The byte array contains invalid Unicode code points.
- ArgumentNullException
bytes
is null.- ArgumentOutOfRangeException
index
orcount
is less than zero.-or-
index
andcount
do not denote a valid range inbytes
.- DecoderFallbackException
A fallback occurred (for more information, see Character Encoding in .NET)
-and-
DecoderFallback is set to DecoderExceptionFallback.