Encoder 클래스
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
문자 집합을 바이트 시퀀스로 변환합니다.
public ref class Encoder abstract
public abstract class Encoder
[System.Serializable]
public abstract class Encoder
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Encoder
type Encoder = class
[<System.Serializable>]
type Encoder = class
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Encoder = class
Public MustInherit Class Encoder
- 상속
-
Encoder
- 특성
예제
다음 예제에서는 지정된 인코딩을 사용하여 유니코드 문자 배열을 바이트 블록으로 변환하는 방법을 보여 줍니다. 비교를 위해 문자 배열은 먼저 .를 사용하여 UTF7Encoding인코딩됩니다. 다음으로 문자 배열은 .를 사용하여 Encoder인코딩됩니다.
using System;
using System.Text;
class EncoderTest {
public static void Main() {
// The characters to encode.
Char[] chars = new Char[] {
'\u0023', // #
'\u0025', // %
'\u03a0', // Pi
'\u03a3' // Sigma
};
// Encode characters using an Encoding object.
Encoding encoding = Encoding.UTF7;
Console.WriteLine("Using Encoding\n--------------");
// Encode complete array for comparison.
Byte[] allCharactersFromEncoding = encoding.GetBytes(chars);
Console.WriteLine("All characters encoded:");
ShowArray(allCharactersFromEncoding);
// Encode characters, one-by-one.
// The Encoding object will NOT maintain state between calls.
Byte[] firstchar = encoding.GetBytes(chars, 0, 1);
Console.WriteLine("First character:");
ShowArray(firstchar);
Byte[] secondchar = encoding.GetBytes(chars, 1, 1);
Console.WriteLine("Second character:");
ShowArray(secondchar);
Byte[] thirdchar = encoding.GetBytes(chars, 2, 1);
Console.WriteLine("Third character:");
ShowArray(thirdchar);
Byte[] fourthchar = encoding.GetBytes(chars, 3, 1);
Console.WriteLine("Fourth character:");
ShowArray(fourthchar);
// Now, encode characters using an Encoder object.
Encoder encoder = encoding.GetEncoder();
Console.WriteLine("Using Encoder\n-------------");
// Encode complete array for comparison.
Byte[] allCharactersFromEncoder = new Byte[encoder.GetByteCount(chars, 0, chars.Length, true)];
encoder.GetBytes(chars, 0, chars.Length, allCharactersFromEncoder, 0, true);
Console.WriteLine("All characters encoded:");
ShowArray(allCharactersFromEncoder);
// Do not flush state; i.e. maintain state between calls.
bool bFlushState = false;
// Encode characters one-by-one.
// By maintaining state, the Encoder will not store extra bytes in the output.
Byte[] firstcharNoFlush = new Byte[encoder.GetByteCount(chars, 0, 1, bFlushState)];
encoder.GetBytes(chars, 0, 1, firstcharNoFlush, 0, bFlushState);
Console.WriteLine("First character:");
ShowArray(firstcharNoFlush);
Byte[] secondcharNoFlush = new Byte[encoder.GetByteCount(chars, 1, 1, bFlushState)];
encoder.GetBytes(chars, 1, 1, secondcharNoFlush, 0, bFlushState);
Console.WriteLine("Second character:");
ShowArray(secondcharNoFlush);
Byte[] thirdcharNoFlush = new Byte[encoder.GetByteCount(chars, 2, 1, bFlushState)];
encoder.GetBytes(chars, 2, 1, thirdcharNoFlush, 0, bFlushState);
Console.WriteLine("Third character:");
ShowArray(thirdcharNoFlush);
// Must flush state on last call to GetBytes().
bFlushState = true;
Byte[] fourthcharNoFlush = new Byte[encoder.GetByteCount(chars, 3, 1, bFlushState)];
encoder.GetBytes(chars, 3, 1, fourthcharNoFlush, 0, bFlushState);
Console.WriteLine("Fourth character:");
ShowArray(fourthcharNoFlush);
}
public static void ShowArray(Array theArray) {
foreach (Object o in theArray) {
Console.Write("[{0}]", o);
}
Console.WriteLine("\n");
}
}
/* This code example produces the following output.
Using Encoding
--------------
All characters encoded:
[43][65][67][77][65][74][81][79][103][65][54][77][45]
First character:
[43][65][67][77][45]
Second character:
[43][65][67][85][45]
Third character:
[43][65][54][65][45]
Fourth character:
[43][65][54][77][45]
Using Encoder
-------------
All characters encoded:
[43][65][67][77][65][74][81][79][103][65][54][77][45]
First character:
[43][65][67]
Second character:
[77][65][74]
Third character:
[81][79][103]
Fourth character:
[65][54][77][45]
*/
Imports System.Text
Imports Microsoft.VisualBasic.Strings
Class EncoderTest
Public Shared Sub Main()
' Unicode characters.
' ChrW(35) = #
' ChrW(37) = %
' ChrW(928) = Pi
' ChrW(931) = Sigma
Dim chars() As Char = {ChrW(35), ChrW(37), ChrW(928), ChrW(931)}
' Encode characters using an Encoding object.
Dim encoding As Encoding = Encoding.UTF7
Console.WriteLine( _
"Using Encoding" & _
ControlChars.NewLine & _
"--------------" _
)
' Encode complete array for comparison.
Dim allCharactersFromEncoding As Byte() = encoding.GetBytes(chars)
Console.WriteLine("All characters encoded:")
ShowArray(allCharactersFromEncoding)
' Encode characters, one-by-one.
' The Encoding object will NOT maintain state between calls.
Dim firstchar As Byte() = encoding.GetBytes(chars, 0, 1)
Console.WriteLine("First character:")
ShowArray(firstchar)
Dim secondchar As Byte() = encoding.GetBytes(chars, 1, 1)
Console.WriteLine("Second character:")
ShowArray(secondchar)
Dim thirdchar As Byte() = encoding.GetBytes(chars, 2, 1)
Console.WriteLine("Third character:")
ShowArray(thirdchar)
Dim fourthchar As Byte() = encoding.GetBytes(chars, 3, 1)
Console.WriteLine("Fourth character:")
ShowArray(fourthchar)
' Now, encode characters using an Encoder object.
Dim encoder As Encoder = encoding.GetEncoder()
Console.WriteLine( _
"Using Encoder" & _
ControlChars.NewLine & _
"-------------" _
)
' Encode complete array for comparison.
Dim allCharactersFromEncoder( _
encoder.GetByteCount(chars, 0, chars.Length, True) _
) As Byte
encoder.GetBytes(chars, 0, chars.Length, allCharactersFromEncoder, 0, True)
Console.WriteLine("All characters encoded:")
ShowArray(allCharactersFromEncoder)
' Do not flush state; i.e. maintain state between calls.
Dim bFlushState As Boolean = False
' Encode characters one-by-one.
' By maintaining state, the Encoder will not store extra bytes in the output.
Dim firstcharNoFlush( _
encoder.GetByteCount(chars, 0, 1, bFlushState) _
) As Byte
encoder.GetBytes(chars, 0, 1, firstcharNoFlush, 0, bFlushState)
Console.WriteLine("First character:")
ShowArray(firstcharNoFlush)
Dim secondcharNoFlush( _
encoder.GetByteCount(chars, 1, 1, bFlushState) _
) As Byte
encoder.GetBytes(chars, 1, 1, secondcharNoFlush, 0, bFlushState)
Console.WriteLine("Second character:")
ShowArray(secondcharNoFlush)
Dim thirdcharNoFlush( _
encoder.GetByteCount(chars, 2, 1, bFlushState) _
) As Byte
encoder.GetBytes(chars, 2, 1, thirdcharNoFlush, 0, bFlushState)
Console.WriteLine("Third character:")
ShowArray(thirdcharNoFlush)
' Must flush state on last call to GetBytes().
bFlushState = True
Dim fourthcharNoFlush( _
encoder.GetByteCount(chars, 3, 1, bFlushState) _
) As Byte
encoder.GetBytes(chars, 3, 1, fourthcharNoFlush, 0, bFlushState)
Console.WriteLine("Fourth character:")
ShowArray(fourthcharNoFlush)
End Sub
Public Shared Sub ShowArray(theArray As Array)
Dim o As Object
For Each o In theArray
Console.Write("[{0}]", o)
Next o
Console.WriteLine(ControlChars.NewLine)
End Sub
End Class
'This code example produces the following output.
'
'Using Encoding
'--------------
'All characters encoded:
'[43][65][67][77][65][74][81][79][103][65][54][77][45]
'
'First character:
'[43][65][67][77][45]
'
'Second character:
'[43][65][67][85][45]
'
'Third character:
'[43][65][54][65][45]
'
'Fourth character:
'[43][65][54][77][45]
'
'Using Encoder
'-------------
'All characters encoded:
'[43][65][67][77][65][74][81][79][103][65][54][77][45][0]
'
'First character:
'[43][65][67][0]
'
'Second character:
'[77][65][74][0]
'
'Third character:
'[81][79][103][0]
'
'Fourth character:
'[65][54][77][45][0]
'
설명
클래스 구현의 인스턴스를 Encoder 가져오려면 애플리케이션 구현의 메서드를 GetEncoderEncoding 사용 해야 합니다.
메서드는 GetByteCount 유니코드 문자 집합을 인코딩하는 결과 바이트 수를 결정하고 메서드는 GetBytes 실제 인코딩을 수행합니다. 클래스에서 사용할 수 있는 이러한 두 메서드의 여러 버전이 Encoder 있습니다. 자세한 내용은 Encoding.GetBytes를 참조하세요.
개체는 Encoder 블록에 걸쳐 있는 문자 시퀀스를 올바르게 인코딩할 GetBytes 수 있도록 후속 호출 또는 Convert 메서드 간에 상태 정보를 유지 관리합니다.
Encoder 또한 데이터 블록의 끝에 후행 문자를 유지하고 다음 인코딩 작업에서 후행 문자를 사용합니다. 예를 들어 데이터 블록은 일치하지 않는 높은 서로게이트로 끝날 수 있으며 일치하는 하위 서로게이트는 다음 데이터 블록에 있을 수 있습니다. 따라서 GetDecoderGetEncoder 이러한 작업은 전체 데이터 스트림 대신 데이터 블록을 처리하는 경우가 많기 때문에 네트워크 전송 및 파일 작업에 유용합니다.
메모
애플리케이션이 데이터 스트림으로 완료되면 매개 변수 true 를 적절한 메서드 호출로 설정 flush 하여 상태 정보가 플러시되는지 확인해야 합니다. 예외가 발생하거나 애플리케이션이 스트림을 전환하는 경우 개체의 Encoder 내부 상태를 지우기 위해 호출 Reset 해야 합니다.
구현자 참고
애플리케이션이 이 클래스에서 상속되는 경우 모든 멤버를 재정의해야 합니다.
생성자
| Name | Description |
|---|---|
| Encoder() |
Encoder 클래스의 새 인스턴스를 초기화합니다. |
속성
| Name | Description |
|---|---|
| Fallback |
현재 Encoder 개체의 개체를 EncoderFallback 가져오거나 설정합니다. |
| FallbackBuffer |
EncoderFallbackBuffer 현재 Encoder 개체와 연결된 개체를 가져옵니다. |
메서드
| Name | Description |
|---|---|
| Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean) |
유니코드 문자 배열을 인코딩된 바이트 시퀀스로 변환하고 결과를 바이트 배열에 저장합니다. |
| Convert(Char*, Int32, Byte*, Int32, Boolean, Int32, Int32, Boolean) |
유니코드 문자의 버퍼를 인코딩된 바이트 시퀀스로 변환하고 결과를 다른 버퍼에 저장합니다. |
| Convert(ReadOnlySpan<Char>, Span<Byte>, Boolean, Int32, Int32, Boolean) |
유니코드 문자 범위를 인코딩된 바이트 시퀀스로 변환하고 결과를 다른 버퍼에 저장합니다. |
| Equals(Object) |
지정한 개체와 현재 개체가 같은지 여부를 확인합니다. (다음에서 상속됨 Object) |
| GetByteCount(Char[], Int32, Int32, Boolean) |
파생 클래스에서 재정의되는 경우 지정된 문자 배열의 문자 집합을 인코딩하여 생성된 바이트 수를 계산합니다. 매개 변수는 계산 후 인코더의 내부 상태를 지울지 여부를 나타냅니다. |
| GetByteCount(Char*, Int32, Boolean) |
파생 클래스에서 재정의되는 경우 지정된 문자 포인터에서 시작하는 문자 집합을 인코딩하여 생성되는 바이트 수를 계산합니다. 매개 변수는 계산 후 인코더의 내부 상태를 지울지 여부를 나타냅니다. |
| GetByteCount(ReadOnlySpan<Char>, Boolean) |
파생 클래스에서 재정의되는 경우 'chars' 범위의 문자 집합을 인코딩하여 생성된 바이트 수를 계산합니다. 매개 변수는 계산 후 인코더의 내부 상태를 지울지 여부를 나타냅니다. |
| GetBytes(Char[], Int32, Int32, Byte[], Int32, Boolean) |
파생 클래스에서 재정의되는 경우 지정된 문자 배열의 문자 집합과 내부 버퍼의 모든 문자를 지정된 바이트 배열로 인코딩합니다. 매개 변수는 변환 후 인코더의 내부 상태를 지울지 여부를 나타냅니다. |
| GetBytes(Char*, Int32, Byte*, Int32, Boolean) |
파생 클래스에서 재정의되는 경우 지정된 문자 포인터에서 시작하는 문자 집합과 내부 버퍼의 모든 문자를 지정된 바이트 포인터에서 시작하여 저장되는 바이트 시퀀스로 인코딩합니다. 매개 변수는 변환 후 인코더의 내부 상태를 지울지 여부를 나타냅니다. |
| GetBytes(ReadOnlySpan<Char>, Span<Byte>, Boolean) |
파생 클래스에서 재정의되는 경우 입력 문자 범위의 문자 집합과 내부 버퍼의 모든 문자를 입력 바이트 범위에 저장된 바이트 시퀀스로 인코딩합니다. 매개 변수는 변환 후 인코더의 내부 상태를 지울지 여부를 나타냅니다. |
| GetHashCode() |
기본 해시 함수로 작동합니다. (다음에서 상속됨 Object) |
| GetType() |
현재 인스턴스의 Type 가져옵니다. (다음에서 상속됨 Object) |
| MemberwiseClone() |
현재 Object단순 복사본을 만듭니다. (다음에서 상속됨 Object) |
| Reset() |
파생 클래스에서 재정의되는 경우 인코더를 초기 상태로 다시 설정합니다. |
| ToString() |
현재 개체를 나타내는 문자열을 반환합니다. (다음에서 상속됨 Object) |