Decoder 类
定义
将已编码字节的序列转换为一组字符。Converts a sequence of encoded bytes into a set of characters.
public ref class Decoder abstract
public abstract class Decoder
[System.Serializable]
public abstract class Decoder
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Decoder
type Decoder = class
[<System.Serializable>]
type Decoder = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Decoder = class
Public MustInherit Class Decoder
- 继承
-
Decoder
- 属性
示例
下面的示例演示如何使用将 Decoder 两个不同的字节数组转换为字符数组。The following example demonstrates the use of a Decoder to convert two different byte arrays into a character array. 字符的一个字节会跨越数组。One of the character's bytes spans the arrays. 这类似于在 StreamReader 读取流时对象所执行的操作。This is similar to what a StreamReader object does internally when reading a stream.
using namespace System;
using namespace System::Text;
int main()
{
// These bytes in UTF-8 correspond to 3 different Unicode
// characters: space (U+0020), # (U+0023), and the biohazard
// symbol (U+2623). Note the biohazard symbol requires 3 bytes
// in UTF-8 (hexadecimal e2, 98, a3). Decoders store state across
// multiple calls to GetChars, handling the case when one char
// is in multiple byte arrays.
array<Byte>^bytes1 = {0x20,0x23,0xe2};
array<Byte>^bytes2 = {0x98,0xa3};
array<Char>^chars = gcnew array<Char>(3);
Decoder^ d = Encoding::UTF8->GetDecoder();
int charLen = d->GetChars( bytes1, 0, bytes1->Length, chars, 0 );
// The value of charLen should be 2 now.
charLen += d->GetChars( bytes2, 0, bytes2->Length, chars, charLen );
for ( UInt16 index(0); index < chars->Length; ++index )
{
Console::Write( "U+{0:X4} ", static_cast<UInt16>(chars[ index ]) );
}
}
using System;
using System.Text;
public class dec
{
public static void Main()
{
// These bytes in UTF-8 correspond to 3 different Unicode
// characters: space (U+0020), # (U+0023), and the biohazard
// symbol (U+2623). Note the biohazard symbol requires 3 bytes
// in UTF-8 (hexadecimal e2, 98, a3). Decoders store state across
// multiple calls to GetChars, handling the case when one char
// is in multiple byte arrays.
byte[] bytes1 = { 0x20, 0x23, 0xe2 };
byte[] bytes2 = { 0x98, 0xa3 };
char[] chars = new char[3];
Decoder d = Encoding.UTF8.GetDecoder();
int charLen = d.GetChars(bytes1, 0, bytes1.Length, chars, 0);
// The value of charLen should be 2 now.
charLen += d.GetChars(bytes2, 0, bytes2.Length, chars, charLen);
foreach(char c in chars)
Console.Write("U+{0:X4} ", (ushort)c);
}
}
Imports System.Text
Public Class dec
Public Shared Sub Main()
' These bytes in UTF-8 correspond to 3 different Unicode
' characters: space (U+0020), # (U+0023), and the biohazard
' symbol (U+2623). Note the biohazard symbol requires 3 bytes
' in UTF-8 (hexadecimal e2, 98, a3). Decoders store state across
' multiple calls to GetChars, handling the case when one char
' is in multiple byte arrays.
Dim bytes1 As Byte() = {&H20, &H23, &HE2}
Dim bytes2 As Byte() = {&H98, &HA3}
Dim chars(3) As Char
Dim d As Decoder = Encoding.UTF8.GetDecoder()
Dim charLen As Integer = d.GetChars(bytes1, 0, bytes1.Length, chars, 0)
' The value of charLen should be 2 now.
charLen += d.GetChars(bytes2, 0, bytes2.Length, chars, charLen)
Dim c As Char
For Each c In chars
Console.Write("U+{0:X4} ", Convert.ToUInt16(c) )
Next c
End Sub
End Class
注解
若要获取类的实现的实例 Decoder ,应用程序应使用 GetDecoder 实现的方法 Encoding 。To obtain an instance of an implementation of the Decoder class, the application should use the GetDecoder method of an Encoding implementation.
GetCharCount方法确定多少个字符会导致对一个字节序列进行解码,并且该 GetChars 方法执行实际解码。The GetCharCount method determines how many characters result in decoding a sequence of bytes, and the GetChars method performs the actual decoding. 类中提供了这两种方法的几个版本 Decoder 。There are several versions of both of these methods available in the Decoder class. 有关详细信息,请参阅 Encoding.GetChars。For more information, see Encoding.GetChars. Decoder对象在对或方法的连续调用之间维护状态信息 GetChars Convert ,以便能够正确地对跨块的字节序列进行解码。A Decoder object maintains state information between successive calls to GetChars or Convert methods so it can correctly decode byte sequences that span blocks. Decoder还保留数据块末尾的尾随字节,并在下一个解码操作中使用尾随字节。The Decoder also preserves trailing bytes at the end of data blocks and uses the trailing bytes in the next decoding operation. 因此, GetDecoder 和 GetEncoder 对于网络传输和文件操作非常有用,因为这些操作经常处理数据块而不是完整的数据流。Therefore, GetDecoder and GetEncoder are useful for network transmission and file operations because those operations often deal with blocks of data instead of a complete data stream.
备注
当应用程序使用数据流完成时,应确保通过 flush true 在相应的方法调用中将参数设置为来刷新状态信息。When the application is done with a stream of data, it should make sure that the state information is flushed by setting the flush parameter to true in the appropriate method call. 如果发生异常或应用程序切换流,则它应调用 Reset 以清除对象的内部状态 Decoder 。If an exception occurs or if the application switches streams, it should call Reset to clear the internal state of the Decoder object.
版本注意事项Version Considerations
在 Decoder Encoder 转换操作期间,可以序列化或对象。A Decoder or Encoder object can be serialized during a conversion operation. 如果在同一版本的 .NET Framework 中对对象进行反序列化,则会保留对象的状态,但如果在其他版本中反序列化,则会丢失。The state of the object is retained if it is deserialized in the same version of the .NET Framework, but lost if it is deserialized in another version.
实施者说明
当应用程序从此类继承时,必须重写所有成员。When your application inherits from this class, it must override all the members.
构造函数
| Decoder() |
初始化 Decoder 类的新实例。Initializes a new instance of the Decoder class. |
属性
| Fallback |
获取或设置当前 DecoderFallback 对象的 Decoder 对象。Gets or sets a DecoderFallback object for the current Decoder object. |
| FallbackBuffer |
获取与当前 DecoderFallbackBuffer 对象关联的 Decoder 对象。Gets the DecoderFallbackBuffer object associated with the current Decoder object. |
方法
| Convert(Byte*, Int32, Char*, Int32, Boolean, Int32, Int32, Boolean) |
将编码字节的缓冲区转换为 UTF-16 编码字符,并将结果存储在另一个缓冲区中。Converts a buffer of encoded bytes to UTF-16 encoded characters and stores the result in another buffer. |
| Convert(Byte[], Int32, Int32, Char[], Int32, Int32, Boolean, Int32, Int32, Boolean) |
将编码字节的数组转换为 UTF-16 编码字符,并将结果存储在字符数组中。Converts an array of encoded bytes to UTF-16 encoded characters and stores the result in a character array. |
| Convert(ReadOnlySpan<Byte>, Span<Char>, Boolean, Int32, Int32, Boolean) |
将编码字节的范围转换为 UTF-16 编码字符,并将结果存储在另一个范围缓冲区中。Converts a span of encoded bytes to UTF-16 encoded characters and stores the result in another span buffer. |
| Equals(Object) |
确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object. (继承自 Object) |
| GetCharCount(Byte*, Int32, Boolean) |
在派生类中重写时,计算对字节序列(从指定的字节指针开始)进行解码所产生的字符数。When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes starting at the specified byte pointer. 一个参数,该参数指示是否在计算后清除解码器的内部状态。A parameter indicates whether to clear the internal state of the decoder after the calculation. |
| GetCharCount(Byte[], Int32, Int32) |
在派生类中重写时,计算对字节序列(从指定字节数组开始)进行解码所产生的字符数。When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. |
| GetCharCount(Byte[], Int32, Int32, Boolean) |
在派生类中重写时,计算对字节序列(从指定字节数组开始)进行解码所产生的字符数。When overridden in a derived class, calculates the number of characters produced by decoding a sequence of bytes from the specified byte array. 一个参数,该参数指示是否在计算后清除解码器的内部状态。A parameter indicates whether to clear the internal state of the decoder after the calculation. |
| GetCharCount(ReadOnlySpan<Byte>, Boolean) |
在派生类中重写时,计算对范围中字节序列进行解码所产生的字符数。When overridden in a derived class, calculates the number of characters produced by decoding the sequence of bytes in the span. 一个参数,该参数指示是否在计算后清除解码器的内部状态。A parameter indicates whether to clear the internal state of the decoder after the calculation. |
| GetChars(Byte*, Int32, Char*, Int32, Boolean) |
在派生类中重写时,将字节序列(从指定的字节指针处开始)和任何内部缓冲区中的字节解码为从指定字符指针开始存储的一组字符。When overridden in a derived class, decodes a sequence of bytes starting at the specified byte pointer and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. 一个参数,指示转换后是否要清除解码器的内部状态。A parameter indicates whether to clear the internal state of the decoder after the conversion. |
| GetChars(Byte[], Int32, Int32, Char[], Int32) |
在派生类中重写时,将指定字节数组的字节序列和内部缓冲区中的任何字节解码到指定的字符数组。When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array. |
| GetChars(Byte[], Int32, Int32, Char[], Int32, Boolean) |
在派生类中重写时,将指定字节数组的字节序列和内部缓冲区中的任何字节解码到指定的字符数组。When overridden in a derived class, decodes a sequence of bytes from the specified byte array and any bytes in the internal buffer into the specified character array. 一个参数,指示转换后是否要清除解码器的内部状态。A parameter indicates whether to clear the internal state of the decoder after the conversion. |
| GetChars(ReadOnlySpan<Byte>, Span<Char>, Boolean) |
在派生类中重写时,将范围字节序列和内部缓冲区中的任何字节解码为从指定字符指针开始存储的一组字符。When overridden in a derived class, decodes a sequence of span bytes and any bytes in the internal buffer into a set of characters that are stored starting at the specified character pointer. 一个参数,指示转换后是否要清除解码器的内部状态。A parameter indicates whether to clear the internal state of the decoder after the conversion. |
| GetHashCode() |
作为默认哈希函数。Serves as the default hash function. (继承自 Object) |
| GetType() |
获取当前实例的 Type。Gets the Type of the current instance. (继承自 Object) |
| MemberwiseClone() |
创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (继承自 Object) |
| Reset() |
在派生类中重写时,将解码器设置回它的初始状态。When overridden in a derived class, sets the decoder back to its initial state. |
| ToString() |
返回表示当前对象的字符串。Returns a string that represents the current object. (继承自 Object) |
扩展方法
| Convert(Decoder, ReadOnlySequence<Byte>, IBufferWriter<Char>, Boolean, Int64, Boolean) |
将 ReadOnlySequence<T> 转换为 UTF-16 编码字符,并将结果写入 |
| Convert(Decoder, ReadOnlySpan<Byte>, IBufferWriter<Char>, Boolean, Int64, Boolean) |
使用 |