Decoder.Convert 메서드

정의

인코딩된 바이트 시퀀스를 문자열 또는 문자 배열로 변환합니다.

오버로드

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

인코딩된 바이트 범위를 UTF-16 인코딩 문자로 변환하고, 그 결과를 다른 범위 버퍼에 저장합니다.

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

인코딩된 바이트 버퍼를 UTF-16으로 인코딩된 문자로 변환하고 그 결과를 다른 버퍼에 저장합니다.

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

인코딩된 바이트 배열을 UTF-16으로 인코딩된 문자로 변환하고 그 결과를 문자 배열에 저장합니다.

설명

Decoder개체는 호출 사이에 상태를 저장합니다. Convert 애플리케이션 데이터의 스트림을 사용 하 여 작업이 완료 되는 경우 설정 해야 합니다 flush 매개 변수를 true 상태 정보를 플러시 되도록 해야 합니다. 이 설정을 사용하는 경우 디코더는 데이터 블록 끝에 있는 잘못된 바이트를 무시하고 내부 버퍼를 지웁니다. 서로게이트 쌍의 high 서로게이트와 같이 논리 단위의 일부인 나머지 처리된 데이터는 현재 대체 설정에 따라 변환됩니다.

Convert메서드는 파일 또는 스트림에서 읽은 데이터와 같이 임의 입력 양을 디코딩하는 루프에 사용되도록 설계되었습니다. 디코딩 작업의 출력을 고정 크기 버퍼에 저장합니다. GetChars 는 출력 버퍼가 충분히 크지 않은 경우 예외를 throw하지만, Convert 출력 배열에서 2자 이상을 허용하는 경우 가능한 한 많은 공간을 채우고 쓴 바이트 및 문자를 반환합니다. 자세한 Encoding.GetChars 내용은 을 참조하세요.

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

인코딩된 바이트 범위를 UTF-16 인코딩 문자로 변환하고, 그 결과를 다른 범위 버퍼에 저장합니다.

public:
 virtual void Convert(ReadOnlySpan<System::Byte> bytes, Span<char> chars, bool flush, [Runtime::InteropServices::Out] int % bytesUsed, [Runtime::InteropServices::Out] int % charsUsed, [Runtime::InteropServices::Out] bool % completed);
public virtual void Convert (ReadOnlySpan<byte> bytes, Span<char> chars, bool flush, out int bytesUsed, out int charsUsed, out bool completed);
abstract member Convert : ReadOnlySpan<byte> * Span<char> * bool * int * int * bool -> unit
override this.Convert : ReadOnlySpan<byte> * Span<char> * bool * int * int * bool -> unit
Public Overridable Sub Convert (bytes As ReadOnlySpan(Of Byte), chars As Span(Of Char), flush As Boolean, ByRef bytesUsed As Integer, ByRef charsUsed As Integer, ByRef completed As Boolean)

매개 변수

bytes
ReadOnlySpan<Byte>

변환할 시퀀스를 포함하는 읽기 전용 바이트 범위입니다.

chars
Span<Char>

변환된 문자를 저장하는 범위입니다.

flush
Boolean

변환할 추가 데이터가 없음을 나타내려면 true이고, 그렇지 않으면 false입니다.

bytesUsed
Int32

이 메서드가 반환되는 경우 변환에서 생성된 바이트 수를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

charsUsed
Int32

이 메서드가 반환되는 경우 변환에 사용된 chars의 문자 수를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

completed
Boolean

이 메서드를 반환할 경우, 지정된 모든 문자가 변환되었으면 true을 포함하고 그렇지 않으면 false를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

설명

completed출력 매개 변수는 입력 바이트 범위의 모든 데이터가 변환되어 chars 범위에 저장되었는지 여부를 나타냅니다. 이 매개 변수는 false 입력 바이트 범위에 포함된 바이트 수를 문자 범위의 문자 수를 초과하지 않고 변환할 수 없는 경우 로 설정됩니다. 이 경우 애플리케이션 출력 버퍼의 내용을 사용 해야 하거나 새 출력 버퍼를 제공 증가 합니다 bytes 매개 변수에 의해 지정 된 바이트 수로는 bytesUsed 매개 변수를 호출는 Convert 메서드를 다시 나머지 입력을 처리 합니다.

completed매개 false bytesUsed 변수와 바이트 범위 길이가 같더라도 매개 변수를 로 설정할 수도 있습니다. 이 상황은 범위에 저장되지 않은 개체에 여전히 데이터가 있는 경우에 Decoder bytes 발생합니다.

적용 대상

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

인코딩된 바이트 버퍼를 UTF-16으로 인코딩된 문자로 변환하고 그 결과를 다른 버퍼에 저장합니다.

public:
 virtual void Convert(System::Byte* bytes, int byteCount, char* chars, int charCount, bool flush, [Runtime::InteropServices::Out] int % bytesUsed, [Runtime::InteropServices::Out] int % charsUsed, [Runtime::InteropServices::Out] bool % completed);
[System.CLSCompliant(false)]
public virtual void Convert (byte* bytes, int byteCount, char* chars, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed);
public virtual void Convert (byte* bytes, int byteCount, char* chars, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Convert (byte* bytes, int byteCount, char* chars, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed);
[System.CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(false)]
[System.Security.SecurityCritical]
public virtual void Convert (byte* bytes, int byteCount, char* chars, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed);
[<System.CLSCompliant(false)>]
abstract member Convert : nativeptr<byte> * int * nativeptr<char> * int * bool * int * int * bool -> unit
override this.Convert : nativeptr<byte> * int * nativeptr<char> * int * bool * int * int * bool -> unit
abstract member Convert : nativeptr<byte> * int * nativeptr<char> * int * bool * int * int * bool -> unit
override this.Convert : nativeptr<byte> * int * nativeptr<char> * int * bool * int * int * bool -> unit
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member Convert : nativeptr<byte> * int * nativeptr<char> * int * bool * int * int * bool -> unit
override this.Convert : nativeptr<byte> * int * nativeptr<char> * int * bool * int * int * bool -> unit
[<System.CLSCompliant(false)>]
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Security.SecurityCritical>]
abstract member Convert : nativeptr<byte> * int * nativeptr<char> * int * bool * int * int * bool -> unit
override this.Convert : nativeptr<byte> * int * nativeptr<char> * int * bool * int * int * bool -> unit

매개 변수

bytes
Byte*

변환할 바이트 시퀀스를 포함하는 버퍼의 주소입니다.

byteCount
Int32

변환할 bytes의 바이트 수입니다.

chars
Char*

변환된 문자를 저장할 버퍼의 주소입니다.

charCount
Int32

변환에서 사용할 chars의 최대 문자 수입니다.

flush
Boolean

변환할 추가 데이터가 없음을 나타내려면 true이고, 그렇지 않으면 false입니다.

bytesUsed
Int32

이 메서드가 반환되는 경우 변환에서 생성된 바이트 수를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

charsUsed
Int32

이 메서드가 반환되는 경우 변환에 사용된 chars의 문자 수를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

completed
Boolean

이 메서드가 반환되는 경우 byteCount로 지정된 모든 문자가 변환되었으면 true를 포함하고, 그렇지 않으면 false를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

특성

예외

chars 또는 bytesnull(Nothing)입니다.

charCount 또는 byteCount가 0보다 작습니다.

출력 버퍼가 너무 작아서 변환된 입력을 포함할 수 없습니다. 서로게이트 문자 쌍을 1개 이상 수용할 수 있도록 출력 버퍼의 크기는 2자 이상이어야 합니다.

대체가 발생했습니다(자세한 내용은 .NET의 문자 인코딩 참조).

FallbackDecoderExceptionFallback로 설정됩니다.

설명

completed출력 매개 변수는 입력 버퍼의 모든 데이터가 변환되어 출력 버퍼에 저장되었는지 여부를 나타냅니다. 매개 변수로 false 지정된 바이트 수를 매개 변수로 지정된 문자 수를 초과하지 않고 변환할 수 없는 경우 이 byteCount 매개 변수는 로 charCount 설정됩니다. 이 경우 애플리케이션 출력 버퍼의 내용을 사용 해야 하거나 새 출력 버퍼를 제공 증가 합니다 bytes 매개 변수에 의해 지정 된 바이트 수로는 bytesUsed 매개 변수를 호출는 Convert 메서드를 다시 나머지 입력을 처리 합니다.

completed및 매개 변수가 false bytesUsed 같더라도 매개 변수를 로 설정할 수도 byteCount 있습니다. 이 상황은 Decoder 버퍼에 저장되지 않은 개체에 여전히 데이터가 있는 경우에 bytes 발생합니다.

적용 대상

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

인코딩된 바이트 배열을 UTF-16으로 인코딩된 문자로 변환하고 그 결과를 문자 배열에 저장합니다.

public:
 virtual void Convert(cli::array <System::Byte> ^ bytes, int byteIndex, int byteCount, cli::array <char> ^ chars, int charIndex, int charCount, bool flush, [Runtime::InteropServices::Out] int % bytesUsed, [Runtime::InteropServices::Out] int % charsUsed, [Runtime::InteropServices::Out] bool % completed);
public virtual void Convert (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed);
[System.Runtime.InteropServices.ComVisible(false)]
public virtual void Convert (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed);
abstract member Convert : byte[] * int * int * char[] * int * int * bool * int * int * bool -> unit
override this.Convert : byte[] * int * int * char[] * int * int * bool * int * int * bool -> unit
[<System.Runtime.InteropServices.ComVisible(false)>]
abstract member Convert : byte[] * int * int * char[] * int * int * bool * int * int * bool -> unit
override this.Convert : byte[] * int * int * char[] * int * int * bool * int * int * bool -> unit
Public Overridable Sub Convert (bytes As Byte(), byteIndex As Integer, byteCount As Integer, chars As Char(), charIndex As Integer, charCount As Integer, flush As Boolean, ByRef bytesUsed As Integer, ByRef charsUsed As Integer, ByRef completed As Boolean)

매개 변수

bytes
Byte[]

변환할 바이트 배열입니다.

byteIndex
Int32

변환할 bytes의 첫 번째 요소입니다.

byteCount
Int32

bytes의 요소 중에서 변환할 요소의 수입니다.

chars
Char[]

변환된 문자를 저장할 배열입니다.

charIndex
Int32

데이터가 저장되는 chars의 첫 번째 요소입니다.

charCount
Int32

변환에서 사용할 chars의 최대 요소 수입니다.

flush
Boolean

변환할 추가 데이터가 없음을 나타내려면 true이고, 그러지 않으면 false입니다.

bytesUsed
Int32

이 메서드가 반환되는 경우 변환에 사용된 바이트 수를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

charsUsed
Int32

이 메서드가 반환되는 경우 변환으로 생성된 chars의 문자 수를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

completed
Boolean

이 메서드가 반환되는 경우 byteCount로 지정된 모든 문자가 변환되었으면 true를 포함하고, 그렇지 않으면 false를 포함합니다. 이 매개 변수는 초기화되지 않은 상태로 전달됩니다.

특성

예외

chars 또는 bytesnull(Nothing)입니다.

charIndex, charCount, byteIndex 또는 byteCount가 0보다 작습니다.

또는

chars - charIndex의 길이가 charCount보다 작습니다.

또는

bytes - byteIndex의 길이가 byteCount보다 작습니다.

출력 버퍼가 너무 작아서 변환된 입력을 포함할 수 없습니다. 서로게이트 문자 쌍을 1개 이상 수용할 수 있도록 출력 버퍼의 크기는 2자 이상이어야 합니다.

대체가 발생했습니다(자세한 내용은 .NET의 문자 인코딩 참조).

FallbackDecoderExceptionFallback로 설정됩니다.

예제

다음 예제에서는 Convert 메서드를 사용하여 UTF-16 문자의 파일을 UTF-8로 변환합니다. 그런 다음 Convert 메서드를 사용하여 UTF-8 문자를 다시 UTF-16 문자로 변환합니다.

// This code example demonstrates the Encoder.Convert() and Decoder.Convert methods.
// This example uses files for input and output, but any source that can be expressed
// as a stream can be used instead.

    using System;
    using System.Text;
    using System.IO;

    public class Sample
    {
    static void Main(string[] args)
        {
// Create a large file of UTF-16 encoded Unicode characters. The file is named Example.txt,
// and is used as input to the Encoder.Convert() method.

            CreateTestFile("Example.txt");

// Using an input file of UTF-16 encoded characters named Example.txt, create an output file
// of UTF-8 encoded bytes named UTF8.txt.

            EncoderConvert("Example.txt", "UTF8.txt", Encoding.UTF8);

// Using an input file of UTF-8 encoded bytes named UTF8.txt, create an output file
// of UTF-16 encoded characters named UTF16.txt.

            DecoderConvert("UTF8.txt", "UTF16.txt", Encoding.UTF8);
        }

// --------------------------------------------------------------------------------------------
// Use the Encoder.Convert() method to convert a file of characters to a file of encoded bytes.
// --------------------------------------------------------------------------------------------
        static void EncoderConvert(String inputFileName, String outputFileName, Encoding enc)
        {
// Convert an input file of characters to an output file of encoded bytes.
// StreamWriter could convert the input file for us, but we'll perform the conversion
// ourselves.

            FileStream fs = new FileStream(outputFileName, FileMode.Create);
            BinaryWriter outputFile = new BinaryWriter(fs);

// StreamReader will detect Unicode encoding from the Byte Order Mark that heads the input file.
            StreamReader inputFile = new StreamReader(inputFileName);

// Get an Encoder.
            Encoder encoder = enc.GetEncoder();

// Guarantee the output buffer large enough to convert a few characters.
            int UseBufferSize = 64;
            if (UseBufferSize < enc.GetMaxByteCount(10))
                    UseBufferSize = enc.GetMaxByteCount(10);
            byte[] bytes = new byte[UseBufferSize];

// Intentionally make the input character buffer larger than the output byte buffer so the
// conversion loop executes more than one cycle.

            char[] chars = new char[UseBufferSize * 4];
            int charsRead;
            do
            {
// Read at most the number of characters that will fit in the input buffer. The return
// value is the actual number of characters read, or zero if no characters remain.
                charsRead = inputFile.Read(chars, 0, UseBufferSize * 4);

                bool completed = false;
                int charIndex = 0;
                int charsUsed;
                int bytesUsed;

                while (!completed)
                {
// If this is the last input data, flush the encoder's internal buffer and state.

                    bool flush = (charsRead == 0);
                    encoder.Convert(chars, charIndex, charsRead - charIndex,
                                    bytes, 0, UseBufferSize, flush,
                                    out charsUsed, out bytesUsed, out completed);

// The conversion produced the number of bytes indicated by bytesUsed. Write that number
// of bytes to the output file.
                    outputFile.Write(bytes, 0, bytesUsed);

// Increment charIndex to the next block of characters in the input buffer, if any, to convert.
                    charIndex += charsUsed;
                }
            }
            while(charsRead != 0);

            outputFile.Close();
            fs.Close();
            inputFile.Close();
        }

// --------------------------------------------------------------------------------------------
// Use the Decoder.Convert() method to convert a file of encoded bytes to a file of characters.
// --------------------------------------------------------------------------------------------
        static void DecoderConvert(String inputFileName, String outputFileName, Encoding enc)
        {
// Convert an input file of of encoded bytes to an output file characters.
// StreamWriter could convert the input file for us, but we'll perform the conversion
// ourselves.

            StreamWriter outputFile = new StreamWriter(outputFileName, false, Encoding.Unicode);

// Read the input as a binary file so we can detect the Byte Order Mark.
            FileStream fs = new FileStream(inputFileName, FileMode.Open);
            BinaryReader inputFile = new BinaryReader(fs);

// Get a Decoder.
            Decoder decoder = enc.GetDecoder();

// Guarantee the output buffer large enough to convert a few characters.
            int UseBufferSize = 64;
            if (UseBufferSize < enc.GetMaxCharCount(10))
                    UseBufferSize = enc.GetMaxCharCount(10);
            char[] chars = new char[UseBufferSize];

// Intentionally make the input byte buffer larger than the output character buffer so the
// conversion loop executes more than one cycle.

            byte[] bytes = new byte[UseBufferSize * 4];
            int bytesRead;
            do
            {
// Read at most the number of bytes that will fit in the input buffer. The
// return value is the actual number of bytes read, or zero if no bytes remain.

                bytesRead = inputFile.Read(bytes, 0, UseBufferSize * 4);

                bool completed = false;
                int byteIndex = 0;
                int bytesUsed;
                int charsUsed;

                while (!completed)
                {
// If this is the last input data, flush the decoder's internal buffer and state.

                    bool flush = (bytesRead == 0);
                    decoder.Convert(bytes, byteIndex, bytesRead - byteIndex,
                                    chars, 0, UseBufferSize, flush,
                                    out bytesUsed, out charsUsed, out completed);

// The conversion produced the number of characters indicated by charsUsed. Write that number
// of characters to the output file.

                    outputFile.Write(chars, 0, charsUsed);

// Increment byteIndex to the next block of bytes in the input buffer, if any, to convert.
                    byteIndex += bytesUsed;
                }
            }
            while(bytesRead != 0);

            outputFile.Close();
            fs.Close();
            inputFile.Close();
        }

// --------------------------------------------------------------------------------------------
// Create a large file of UTF-16 encoded Unicode characters.
// --------------------------------------------------------------------------------------------
        static void CreateTestFile(String FileName)
        {
// StreamWriter defaults to UTF-8 encoding so explicitly specify Unicode, that is,
// UTF-16, encoding.
            StreamWriter file = new StreamWriter(FileName, false, Encoding.Unicode);

// Write a line of text 100 times.
            for (int i = 0; i < 100; i++)
            {
                file.WriteLine("This is an example input file used by the convert example.");
            }

// Write Unicode characters from U+0000 to, but not including, the surrogate character range.
            for (char c = (char)0; c < (char)0xD800; c++)
            {
                file.Write(c);
            }
            file.Close();
        }
    }

/*
This code example produces the following results:

(Execute the -dir- console window command and examine the files created.)

Example.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).
UTF8.txt, which contains 169,712 UTF-8 encoded bytes.
UTF16.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).

(Execute the -comp- console window command and compare the two Unicode files.)

>comp example.txt utf16.txt /L
Comparing example.txt and utf16.txt...
Files compare OK

(The two files are equal.)

*/
' This code example demonstrates the Encoder.Convert() and Decoder.Convert methods. 
' This example uses files for input and output, but any source that can be expressed
' as a stream can be used instead.

Imports System.Text
Imports System.IO

Public Class Sample
    Shared Sub Main(ByVal args() As String) 
        ' Create a large file of UTF-16 encoded Unicode characters. The file is named Example.txt, 
        ' and is used as input to the Encoder.Convert() method. 
        CreateTestFile("Example.txt")
        
        
        ' Using an input file of UTF-16 encoded characters named Example.txt, create an output file 
        ' of UTF-8 encoded bytes named UTF8.txt.
        EncoderConvert("Example.txt", "UTF8.txt", Encoding.UTF8)
        
        ' Using an input file of UTF-8 encoded bytes named UTF8.txt, create an output file 
        ' of UTF-16 encoded characters named UTF16.txt.
        DecoderConvert("UTF8.txt", "UTF16.txt", Encoding.UTF8)
    
    End Sub
    
    ' --------------------------------------------------------------------------------------------
    ' Use the Encoder.Convert() method to convert a file of characters to a file of encoded bytes.
    ' --------------------------------------------------------------------------------------------
    Shared Sub EncoderConvert(ByVal inputFileName As String, ByVal outputFileName As String, ByVal enc As Encoding) 
        ' Convert an input file of characters to an output file of encoded bytes.
        ' StreamWriter could convert the input file for us, but we'll perform the conversion 
        ' ourselves.
        Dim fs As New FileStream(outputFileName, FileMode.Create)
        Dim outputFile As New BinaryWriter(fs)
        
        ' StreamReader will detect Unicode encoding from the Byte Order Mark that heads the input file.
        Dim inputFile As New StreamReader(inputFileName)
        
        ' Get an Encoder.
        Dim encoder As Encoder = enc.GetEncoder()
        
        ' Guarantee the output buffer large enough to convert a few characters.
        Dim UseBufferSize As Integer = 64
        If UseBufferSize < enc.GetMaxByteCount(10) Then
            UseBufferSize = enc.GetMaxByteCount(10)
        End If
        Dim bytes(UseBufferSize) As Byte
        
        ' Intentionally make the input character buffer larger than the output byte buffer so the 
        ' conversion loop executes more than one cycle. 
        Dim chars(UseBufferSize * 4) As Char
        Dim charsRead As Integer
        Do
            ' Read at most the number of characters that will fit in the input buffer. The return 
            ' value is the actual number of characters read, or zero if no characters remain. 
            charsRead = inputFile.Read(chars, 0, UseBufferSize * 4)
            
            Dim completed As Boolean = False
            Dim charIndex As Integer = 0
            Dim charsUsed As Integer
            Dim bytesUsed As Integer
            
            While Not completed
                ' If this is the last input data, flush the encoder's internal buffer and state.
                Dim flush As Boolean = charsRead = 0
                encoder.Convert(chars, charIndex, charsRead - charIndex, bytes, 0, UseBufferSize, flush, charsUsed, bytesUsed, completed)
                
                ' The conversion produced the number of bytes indicated by bytesUsed. Write that number
                ' of bytes to the output file.
                outputFile.Write(bytes, 0, bytesUsed)
                
                ' Increment charIndex to the next block of characters in the input buffer, if any, to convert.
                charIndex += charsUsed
            End While
        Loop While charsRead <> 0
        
        outputFile.Close()
        fs.Close()
        inputFile.Close()
    
    End Sub
    
    ' --------------------------------------------------------------------------------------------
    ' Use the Decoder.Convert() method to convert a file of encoded bytes to a file of characters.
    ' --------------------------------------------------------------------------------------------
    Shared Sub DecoderConvert(ByVal inputFileName As String, ByVal outputFileName As String, ByVal enc As Encoding) 
        ' Convert an input file of of encoded bytes to an output file characters.
        ' StreamWriter could convert the input file for us, but we'll perform the conversion 
        ' ourselves.
        Dim outputFile As New StreamWriter(outputFileName, False, Encoding.Unicode)
        
        ' Read the input as a binary file so we can detect the Byte Order Mark.
        Dim fs As New FileStream(inputFileName, FileMode.Open)
        Dim inputFile As New BinaryReader(fs)
        
        ' Get a Decoder.
        Dim decoder As Decoder = enc.GetDecoder()
        
        ' Guarantee the output buffer large enough to convert a few characters.
        Dim UseBufferSize As Integer = 64
        If UseBufferSize < enc.GetMaxCharCount(10) Then
            UseBufferSize = enc.GetMaxCharCount(10)
        End If
        Dim chars(UseBufferSize) As Char
        
        ' Intentionally make the input byte buffer larger than the output character buffer so the 
        ' conversion loop executes more than one cycle. 
        Dim bytes(UseBufferSize * 4) As Byte
        Dim bytesRead As Integer
        Do
            ' Read at most the number of bytes that will fit in the input buffer. The 
            ' return value is the actual number of bytes read, or zero if no bytes remain. 
            bytesRead = inputFile.Read(bytes, 0, UseBufferSize * 4)
            
            Dim completed As Boolean = False
            Dim byteIndex As Integer = 0
            Dim bytesUsed As Integer
            Dim charsUsed As Integer
            
            While Not completed
                ' If this is the last input data, flush the decoder's internal buffer and state.
                Dim flush As Boolean = bytesRead = 0
                decoder.Convert(bytes, byteIndex, bytesRead - byteIndex, chars, 0, UseBufferSize, flush, bytesUsed, charsUsed, completed)
                
                ' The conversion produced the number of characters indicated by charsUsed. Write that number
                ' of characters to the output file.
                outputFile.Write(chars, 0, charsUsed)
                
                ' Increment byteIndex to the next block of bytes in the input buffer, if any, to convert.
                byteIndex += bytesUsed
            End While
        Loop While bytesRead <> 0
        
        outputFile.Close()
        fs.Close()
        inputFile.Close()
    
    End Sub
    
    ' --------------------------------------------------------------------------------------------
    ' Create a large file of UTF-16 encoded Unicode characters. 
    ' --------------------------------------------------------------------------------------------
    Shared Sub CreateTestFile(ByVal FileName As String) 
        ' StreamWriter defaults to UTF-8 encoding so explicitly specify Unicode, that is, 
        ' UTF-16, encoding.
        Dim file As New StreamWriter(FileName, False, Encoding.Unicode)
        
        ' Write a line of text 100 times.
        Dim i As Integer
        For i = 0 To 99
            file.WriteLine("This is an example input file used by the convert example.")
        Next i
        
        ' Write Unicode characters from U+0000 to, but not including, the surrogate character range.
        Dim c As Integer
        For c = 0 To &HD800
            file.Write(ChrW(c))
        Next c
        file.Close()
    
    End Sub
End Class

'
'This code example produces the following results:
'
'(Execute the -dir- console window command and examine the files created.)
'
'Example.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).
'UTF8.txt, which contains 169,712 UTF-8 encoded bytes.
'UTF16.txt, which contains 122,594 bytes (61,297 UTF-16 encoded characters).
'
'(Execute the -comp- console window command and compare the two Unicode files.)
'
'>comp example.txt utf16.txt /L
'Comparing example.txt and utf16.txt...
'Files compare OK
'
'(The two files are equal.)
'

설명

completed출력 매개 변수는 입력 버퍼의 모든 데이터가 변환되어 출력 버퍼에 저장되었는지 여부를 나타냅니다. 매개 변수로 false 지정된 바이트 수를 매개 변수로 지정된 문자 수를 초과하지 않고 변환할 수 없는 경우 이 byteCount 매개 변수는 로 charCount 설정됩니다. 이 경우 애플리케이션 출력 버퍼의 내용을 사용 해야 하거나 새 출력 버퍼를 제공 증가 합니다 bytes 매개 변수에 의해 지정 된 바이트 수로는 bytesUsed 매개 변수를 호출는 Convert 메서드를 다시 나머지 입력을 처리 합니다.

completed및 매개 변수가 false bytesUsed 같더라도 매개 변수를 로 설정할 수도 byteCount 있습니다. 이 상황은 Decoder 버퍼에 저장되지 않은 개체에 여전히 데이터가 있는 경우에 bytes 발생합니다.

적용 대상