Encoder 클래스

정의

문자 집합을 바이트 시퀀스로 변환합니다.

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 namespace System;
using namespace System::Text;
using namespace System::Collections;
void ShowArray( Array^ theArray )
{
   IEnumerator^ myEnum = theArray->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ o = safe_cast<Object^>(myEnum->Current);
      Console::Write( "[{0}]", o );
   }

   Console::WriteLine( "\n" );
}

int main()
{
   
   // The characters to encode.
   
   // Pi
   // Sigma
   array<Char>^chars = {L'\u03a0',L'\u03a3',L'\u03a6',L'\u03a9'};
   
   // Encode characters using an Encoding Object*.
   Encoding^ encoding = Encoding::UTF7;
   Console::WriteLine( "Using Encoding\n--------------" );
   
   // Encode complete array for comparison.
   array<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.
   array<Byte>^firstchar = encoding->GetBytes( chars, 0, 1 );
   Console::WriteLine( "First character:" );
   ShowArray( firstchar );
   array<Byte>^secondchar = encoding->GetBytes( chars, 1, 1 );
   Console::WriteLine( "Second character:" );
   ShowArray( secondchar );
   array<Byte>^thirdchar = encoding->GetBytes( chars, 2, 1 );
   Console::WriteLine( "Third character:" );
   ShowArray( thirdchar );
   array<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.
   array<Byte>^allCharactersFromEncoder = gcnew array<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.
   array<Byte>^firstcharNoFlush = gcnew array<Byte>(encoder->GetByteCount( chars, 0, 1, bFlushState ));
   encoder->GetBytes( chars, 0, 1, firstcharNoFlush, 0, bFlushState );
   Console::WriteLine( "First character:" );
   ShowArray( firstcharNoFlush );
   array<Byte>^secondcharNoFlush = gcnew array<Byte>(encoder->GetByteCount( chars, 1, 1, bFlushState ));
   encoder->GetBytes( chars, 1, 1, secondcharNoFlush, 0, bFlushState );
   Console::WriteLine( "Second character:" );
   ShowArray( secondcharNoFlush );
   array<Byte>^thirdcharNoFlush = gcnew array<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;
   array<Byte>^fourthcharNoFlush = gcnew array<Byte>(encoder->GetByteCount( chars, 3, 1, bFlushState ));
   encoder->GetBytes( chars, 3, 1, fourthcharNoFlush, 0, bFlushState );
   Console::WriteLine( "Fourth character:" );
   ShowArray( fourthcharNoFlush );
}

/* This code example produces the following output.

Using Encoding
--------------
All characters encoded:
[43][65][54][65][68][111][119][79][109][65][54][107][45]

First character:
[43][65][54][65][45]

Second character:
[43][65][54][77][45]

Third character:
[43][65][54][89][45]

Fourth character:
[43][65][54][107][45]

Using Encoder
-------------
All characters encoded:
[43][65][54][65][68][111][119][79][109][65][54][107][45]

First character:
[43][65][54]

Second character:
[65][68][111]

Third character:
[119][79][109]

Fourth character:
[65][54][107][45]


*/
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 의 instance 가져오려면 애플리케이션 구현의 메서드를 GetEncoderEncoding 사용 해야 합니다.

GetByteCount메서드는 유니코드 문자 집합을 인코딩할 바이트 수를 결정 하 고 GetBytes 메서드는 실제 인코딩을 수행 합니다. 클래스에서 사용할 수 있는 이러한 두 메서드의 여러 버전이 Encoder 있습니다. 자세한 내용은 Encoding.GetBytes를 참조하세요.

개체는 Encoder 블록에 걸쳐 있는 문자 시퀀스를 올바르게 인코딩할 GetBytes 수 있도록 또는 Convert 메서드에 대한 연속 호출 간에 상태 정보를 유지 관리합니다. Encoder또한는 데이터 블록 끝에 후행 문자를 보존 하 고 다음 인코딩 작업에서 후행 문자를 사용 합니다. 예를 들어, 데이터 블록이 일치 하지 않는 상위 서로게이트를 종료 하 고 일치 하는 하위 서로게이트는 다음 데이터 블록에 있을 수 있습니다. 따라서 GetDecoderGetEncoder 는 네트워크 전송 및 파일 작업에 유용 합니다. 이러한 작업은 대개 전체 데이터 스트림 대신 데이터 블록을 처리 하기 때문입니다.

참고

애플리케이션 상태 정보를 설정 하 여 플러시 되도록 확인 해야 하는 데이터의 스트림을 사용 하 여 완료 되 면 합니다 flush 매개 변수를 true 적절 한 메서드 호출에서 합니다. 예외가 발생 하거나 애플리케이션 스트림 전환 하는 경우 호출 해야 하는 경우 Reset 의 내부 상태를 지울지를 Encoder 개체입니다.

구현자 참고

애플리케이션이 클래스에서 상속 하는 경우에 모든 멤버를 재정의 해야 합니다.

생성자

Encoder()

Encoder 클래스의 새 인스턴스를 초기화합니다.

속성

Fallback

현재 EncoderFallback 개체에 대한 Encoder 개체를 가져오거나 설정합니다.

FallbackBuffer

현재 EncoderFallbackBuffer 개체와 관련된 Encoder 개체를 가져옵니다.

메서드

Convert(Char*, Int32, Byte*, Int32, Boolean, Int32, Int32, Boolean)

유니코드 문자 버퍼를 인코딩된 바이트 시퀀스로 변환하고 결과를 다른 버퍼에 저장합니다.

Convert(Char[], Int32, Int32, Byte[], Int32, Int32, Boolean, Int32, Int32, Boolean)

유니코드 문자 배열을 인코딩된 바이트 시퀀스로 변환하고 결과를 바이트 배열에 저장합니다.

Convert(ReadOnlySpan<Char>, Span<Byte>, Boolean, Int32, Int32, Boolean)

유니코드 문자 범위를 인코딩된 바이트 시퀀스로 변환하고, 다른 버퍼에 그 결과를 저장합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetByteCount(Char*, Int32, Boolean)

파생 클래스에서 재정의되면 지정한 문자 포인터에서 시작하는 문자 집합을 인코딩하여 생성되는 바이트 수를 계산합니다. 매개 변수는 계산 후 인코더의 내부 상태를 지울지 여부를 나타냅니다.

GetByteCount(Char[], Int32, Int32, Boolean)

파생 클래스에서 재정의되면 지정한 문자 배열의 문자 집합을 인코딩하여 생성되는 바이트 수를 계산합니다. 매개 변수는 계산 후 인코더의 내부 상태를 지울지 여부를 나타냅니다.

GetByteCount(ReadOnlySpan<Char>, Boolean)

파생 클래스에서 재정의할 경우, ‘문자’ 범위의 문자 집합을 인코딩하여 생성되는 바이트 수를 계산합니다. 매개 변수는 계산 후 인코더의 내부 상태를 지울지 여부를 나타냅니다.

GetBytes(Char*, Int32, Byte*, Int32, Boolean)

파생 클래스에서 재정의될 때 지정한 문자 포인터에서 시작하는 문자 집합 및 내부 버퍼의 모든 문자를 지정한 바이트 포인터에서 시작하여 저장되는 바이트 시퀀스로 인코딩합니다. 매개 변수는 변환 후 인코더의 내부 상태를 지울지 여부를 나타냅니다.

GetBytes(Char[], Int32, Int32, Byte[], Int32, Boolean)

파생 클래스에서 재정의될 때 지정한 문자 배열의 문자 집합 및 내부 버퍼의 모든 문자를 지정한 바이트 배열로 인코딩합니다. 매개 변수는 변환 후 인코더의 내부 상태를 지울지 여부를 나타냅니다.

GetBytes(ReadOnlySpan<Char>, Span<Byte>, Boolean)

파생 클래스에서 재정의할 경우, 입력 문자 범위의 문자 집합과 내부 버퍼의 모든 문자를 입력 바이트 범위에 저장되는 바이트 시퀀스로 인코딩합니다. 매개 변수는 변환 후 인코더의 내부 상태를 지울지 여부를 나타냅니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
Reset()

파생 클래스에서 재정의될 때 인코더를 다시 초기 상태로 설정합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

확장 메서드

Convert(Encoder, ReadOnlySequence<Char>, IBufferWriter<Byte>, Boolean, Int64, Boolean)

ReadOnlySequence<T>을 인코딩된 바이트로 변환하고 결과를 writer에 씁니다.

Convert(Encoder, ReadOnlySpan<Char>, IBufferWriter<Byte>, Boolean, Int64, Boolean)

encoder를 사용하여 ReadOnlySpan<T>을 바이트로 변환하고 결과를 writer에 씁니다.

적용 대상

추가 정보