UTF8Encoding コンストラクター

定義

UTF8Encoding クラスの新しいインスタンスを初期化します。

オーバーロード

UTF8Encoding()

UTF8Encoding クラスの新しいインスタンスを初期化します。

UTF8Encoding(Boolean)

UTF8Encoding クラスの新しいインスタンスを初期化します。 Unicode バイト順マークを付加するかどうかを指定するパラメーター。

UTF8Encoding(Boolean, Boolean)

UTF8Encoding クラスの新しいインスタンスを初期化します。 パラメーターでは、Unicode バイト順マークを付加するかどうか、および無効なエンコードが検出されたときに例外をスローするかどうかを指定します。

UTF8Encoding()

ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs

UTF8Encoding クラスの新しいインスタンスを初期化します。

public:
 UTF8Encoding();
public UTF8Encoding ();
Public Sub New ()

次の例では、新 UTF8Encoding しいインスタンスを作成し、その名前を表示します。

using namespace System;
using namespace System::Text;
int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   String^ encodingName = utf8->EncodingName;
   Console::WriteLine( "Encoding name: {0}", encodingName );
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        String encodingName = utf8.EncodingName;
        Console.WriteLine("Encoding name: " + encodingName);
    }
}
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim encodingName As String = utf8.EncodingName
        Console.WriteLine("Encoding name: " & encodingName)
    End Sub
End Class

注釈

このコンストラクターは、Unicode バイトオーダー マークを提供せず、無効なエンコードが検出されたときに例外をスローしないインスタンスを作成します。

注意事項

セキュリティ上の理由から、パラメーターを使用してコンストラクター throwOnInvalidBytes を呼び出し、その値を に true設定することで、エラー検出を有効にすることをお勧めします。

こちらもご覧ください

適用対象

UTF8Encoding(Boolean)

ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs

UTF8Encoding クラスの新しいインスタンスを初期化します。 Unicode バイト順マークを付加するかどうかを指定するパラメーター。

public:
 UTF8Encoding(bool encoderShouldEmitUTF8Identifier);
public UTF8Encoding (bool encoderShouldEmitUTF8Identifier);
new System.Text.UTF8Encoding : bool -> System.Text.UTF8Encoding
Public Sub New (encoderShouldEmitUTF8Identifier As Boolean)

パラメーター

encoderShouldEmitUTF8Identifier
Boolean

GetPreamble() メソッドが Unicode バイト順マークを返すよう指定する場合は true、それ以外の場合は false

次の例では、新 UTF8Encoding しいインスタンスを作成し、 メソッドによって GetPreamble Unicode バイトオーダー マーク プレフィックスを出力する必要があることを指定します。 次に、 メソッドは GetPreamble Unicode バイト順マーク プレフィックスを返します。

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();
}

int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   UTF8Encoding^ utf8EmitBOM = gcnew UTF8Encoding( true );
   Console::WriteLine( "utf8 preamble:" );
   ShowArray( utf8->GetPreamble() );
   Console::WriteLine( "utf8EmitBOM:" );
   ShowArray( utf8EmitBOM->GetPreamble() );
}
using System;
using System.Text;

class UTF8EncodingExample {
    public static void Main() {
        UTF8Encoding utf8 = new UTF8Encoding();
        UTF8Encoding utf8EmitBOM = new UTF8Encoding(true);

        Console.WriteLine("utf8 preamble:");
        ShowArray(utf8.GetPreamble());

        Console.WriteLine("utf8EmitBOM:");
        ShowArray(utf8EmitBOM.GetPreamble());
    }

    public static void ShowArray(Array theArray) {
        foreach (Object o in theArray) {
            Console.Write("[{0}]", o);
        }
        Console.WriteLine();
    }
}
Imports System.Text

Class UTF8EncodingExample
    
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim utf8EmitBOM As New UTF8Encoding(True)
        
        Console.WriteLine("utf8 preamble:")
        ShowArray(utf8.GetPreamble())
        
        Console.WriteLine("utf8EmitBOM:")
        ShowArray(utf8EmitBOM.GetPreamble())
    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()
    End Sub
End Class

注釈

このコンストラクターは、無効なエンコードが検出されたときに例外をスローしないインスタンスを作成します。

注意事項

セキュリティ上の理由から、パラメーターを含む throwOnInvalidBytes コンストラクターを呼び出し、その値を に設定することで、エラー検出を true有効にする必要があります。

パラメーターは encoderShouldEmitUTF8Identifier 、 メソッドの操作を GetPreamble 制御します。 の場合 true、メソッドは UNICODE バイトオーダー マーク (BOM) を UTF-8 形式で含むバイト配列を返します。 の場合 falseは、長さ 0 のバイト配列を返します。 ただし、 を にtrue設定encoderShouldEmitUTF8IdentifierしてもGetBytes、メソッドはバイト配列の先頭に BOM のプレフィックスを付けることはなく、GetByteCountまた、メソッドが BOM のバイト数をバイト数に含めることはありません。

こちらもご覧ください

適用対象

UTF8Encoding(Boolean, Boolean)

ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs
ソース:
UTF8Encoding.cs

UTF8Encoding クラスの新しいインスタンスを初期化します。 パラメーターでは、Unicode バイト順マークを付加するかどうか、および無効なエンコードが検出されたときに例外をスローするかどうかを指定します。

public:
 UTF8Encoding(bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes);
public UTF8Encoding (bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes);
new System.Text.UTF8Encoding : bool * bool -> System.Text.UTF8Encoding
Public Sub New (encoderShouldEmitUTF8Identifier As Boolean, throwOnInvalidBytes As Boolean)

パラメーター

encoderShouldEmitUTF8Identifier
Boolean

GetPreamble() メソッドが Unicode バイト順マークを返すよう指定する場合は true、それ以外の場合は false

throwOnInvalidBytes
Boolean

無効なエンコードが検出されたときに例外をスローする場合は true、それ以外の場合は false

次の例では、新しい UTF8Encoding インスタンスを作成し、無効なエンコードが GetPreamble 検出されたときにメソッドが Unicode バイトオーダー マーク プレフィックスを出力しないように指定し、例外をスローする必要があります。 このコンストラクターの動作は、無効なエンコードが検出されたときに例外をスローしない既定 UTF8Encoding() のコンストラクターと比較されます。 2 つの UTF8Encoding インスタンスは、無効な文字シーケンスである 2 つの上位サロゲート (U+D801 と U+D802) を含む文字配列をエンコードします。高いサロゲートの後には常に低いサロゲートが続く必要があります。

using namespace System;
using namespace System::Text;

void ShowArray(Array^ theArray)
{
   for each (Byte b in theArray) {
      Console::Write( "{0:X2} ", b);
   }
   Console::WriteLine();
}

int main()
{
   UTF8Encoding^ utf8 = gcnew UTF8Encoding;
   UTF8Encoding^ utf8ThrowException = gcnew UTF8Encoding(false,true);
   
   // This array contains two high surrogates in a row (\uD801, \uD802).
   array<Char>^chars = {'a','b','c',L'\xD801',L'\xD802','d'};
   
   // The following method call will not throw an exception.
   array<Byte>^bytes = utf8->GetBytes( chars );
   ShowArray( bytes );
   Console::WriteLine();
   
   try {
      
      // The following method call will throw an exception.
      bytes = utf8ThrowException->GetBytes( chars );
   }
   catch (EncoderFallbackException^ e ) {
            Console::WriteLine("{0} exception\nMessage:\n{1}",
                               e->GetType()->Name, e->Message);
   }

}
using System;
using System.Text;

class Example
{
    public static void Main()
    {
        UTF8Encoding utf8 = new UTF8Encoding();
        UTF8Encoding utf8ThrowException = new UTF8Encoding(false, true);

        // Create an array with two high surrogates in a row (\uD801, \uD802).
        Char[] chars = new Char[] {'a', 'b', 'c', '\uD801', '\uD802', 'd'};

        // The following method call will not throw an exception.
        Byte[] bytes = utf8.GetBytes(chars);
        ShowArray(bytes);
        Console.WriteLine();

        try {
            // The following method call will throw an exception.
            bytes = utf8ThrowException.GetBytes(chars);
            ShowArray(bytes);
        }
        catch (EncoderFallbackException e) {
            Console.WriteLine("{0} exception\nMessage:\n{1}",
                              e.GetType().Name, e.Message);
        }
    }

    public static void ShowArray(Array theArray) {
        foreach (Object o in theArray)
            Console.Write("{0:X2} ", o);

        Console.WriteLine();
    }
}
// The example displays the following output:
//    61 62 63 EF BF BD EF BF BD 64
//
//    EncoderFallbackException exception
//    Message:
//    Unable to translate Unicode character \uD801 at index 3 to specified code page.
Imports System.Text

Class Example
    Public Shared Sub Main()
        Dim utf8 As New UTF8Encoding()
        Dim utf8ThrowException As New UTF8Encoding(False, True)
        
        ' Create an array with two high surrogates in a row (\uD801, \uD802).
        Dim chars() As Char = {"a"c, "b"c, "c"c, ChrW(&hD801), ChrW(&hD802), "d"c}
        
        ' The following method call will not throw an exception.
        Dim bytes As Byte() = utf8.GetBytes(chars)
        ShowArray(bytes)
        Console.WriteLine()
        
        Try
            ' The following method call will throw an exception.
            bytes = utf8ThrowException.GetBytes(chars)
            ShowArray(bytes)
        Catch e As EncoderFallbackException
            Console.WriteLine("{0} exception{2}Message:{2}{1}",
                              e.GetType().Name, e.Message, vbCrLf)
        End Try
    End Sub
    
    
    Public Shared Sub ShowArray(theArray As Array)
        For Each o In theArray
            Console.Write("{0:X2} ", o)
        Next
        Console.WriteLine()
    End Sub
End Class
' The example displays the following output:
'    61 62 63 EF BF BD EF BF BD 64
'
'    EncoderFallbackException exception
'    Message:
'    Unable to translate Unicode character \uD801 at index 3 to specified code page.

注釈

パラメーターは encoderShouldEmitUTF8Identifier 、 メソッドの操作を GetPreamble 制御します。 の場合 true、メソッドは UNICODE バイトオーダー マーク (BOM) を UTF-8 形式で含むバイト配列を返します。 の場合 falseは、長さ 0 のバイト配列を返します。 ただし、 を にtrue設定encoderShouldEmitUTF8IdentifierしてもGetBytes、メソッドはバイト配列の先頭に BOM のプレフィックスを付けることはなく、GetByteCountまた、メソッドが BOM のバイト数をバイト数に含めることはありません。

が の場合throwOnInvalidBytes、無効なバイト シーケンスを検出するメソッドは例外をSystem.ArgumentExceptionスローtrueします。 それ以外の場合、メソッドは例外をスローせず、無効なシーケンスは無視されます。

注意事項

セキュリティ上の理由から、パラメーターを含むコンストラクターを呼び出し、そのパラメーターを throwOnInvalidBytes に設定することで、エラー検出を true有効にする必要があります。

こちらもご覧ください

適用対象