UTF8Encoding コンストラクター
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
UTF8Encoding クラスの新しいインスタンスを初期化します。
オーバーロード
| 名前 | 説明 |
|---|---|
| UTF8Encoding() |
UTF8Encoding クラスの新しいインスタンスを初期化します。 |
| UTF8Encoding(Boolean) |
UTF8Encoding クラスの新しいインスタンスを初期化します。 パラメーターは、Unicode バイトオーダー マークを指定するかどうかを指定します。 |
| UTF8Encoding(Boolean, Boolean) |
UTF8Encoding クラスの新しいインスタンスを初期化します。 パラメーターは、Unicode バイトオーダー マークを指定するかどうか、および無効なエンコードが検出されたときに例外をスローするかどうかを指定します。 |
UTF8Encoding()
- ソース:
- UTF8Encoding.cs
- ソース:
- UTF8Encoding.cs
- ソース:
- UTF8Encoding.cs
- ソース:
- UTF8Encoding.cs
- ソース:
- UTF8Encoding.cs
UTF8Encoding クラスの新しいインスタンスを初期化します。
public:
UTF8Encoding();
public UTF8Encoding();
Public Sub New ()
例
次の例では、新しい UTF8Encoding インスタンスを作成し、その名前を表示します。
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 バイトオーダー マークを提供せず、無効なエンコードが検出されたときに例外をスローしないインスタンスを作成します。
Caution
セキュリティ上の理由から、 throwOnInvalidBytes パラメーターを使用してコンストラクターを呼び出し、その値を true に設定することで、エラー検出を有効にすることをお勧めします。
こちらもご覧ください
適用対象
UTF8Encoding(Boolean)
- ソース:
- UTF8Encoding.cs
- ソース:
- UTF8Encoding.cs
- ソース:
- 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
true
GetPreamble() メソッドが Unicode バイトオーダー マークを返すように指定する場合は。それ以外の場合はfalse。
例
次の例では、新しい UTF8Encoding インスタンスを作成し、 GetPreamble メソッドによって Unicode バイトオーダー マーク プレフィックスを出力する必要があることを指定します。 その後、 GetPreamble メソッドは Unicode バイトオーダー マーク プレフィックスを返します。
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
注釈
このコンストラクターは、無効なエンコードが検出されたときに例外をスローしないインスタンスを作成します。
Caution
セキュリティ上の理由から、 throwOnInvalidBytes パラメーターを含むコンストラクターを呼び出し、その値を true に設定することで、エラー検出を有効にする必要があります。
encoderShouldEmitUTF8Identifier パラメーターは、GetPreamble メソッドの操作を制御します。
true場合、メソッドは UNICODE バイトオーダー マーク (BOM) を含むバイト配列を UTF-8 形式で返します。
false場合は、長さ 0 のバイト配列を返します。 ただし、 encoderShouldEmitUTF8Identifier を true に設定しても、 GetBytes メソッドはバイト配列の先頭に BOM のプレフィックスを付けることはなく、また、 GetByteCount メソッドがバイト数に BOM のバイト数を含めることはありません。
こちらもご覧ください
適用対象
UTF8Encoding(Boolean, Boolean)
- ソース:
- UTF8Encoding.cs
- ソース:
- UTF8Encoding.cs
- ソース:
- 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
- throwOnInvalidBytes
- Boolean
true 無効なエンコードが検出されたときに例外をスローする場合。それ以外の場合は false。
例
次の例では、新しい UTF8Encoding インスタンスを作成し、 GetPreamble メソッドが Unicode バイトオーダー マーク プレフィックスを出力しないように指定し、無効なエンコードが検出されたときに例外をスローする必要があります。 このコンストラクターの動作は、無効なエンコードが検出されたときに例外をスローしない既定の UTF8Encoding() コンストラクターと比較されます。 2 つの UTF8Encoding インスタンスは、行に 2 つの上位サロゲート (U+D801 と U+D802) を含む文字配列をエンコードします。これは無効な文字シーケンスです。高いサロゲートの後に常に低サロゲートが続く必要があります。
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 のバイト配列を返します。 ただし、 encoderShouldEmitUTF8Identifier を true に設定しても、 GetBytes メソッドはバイト配列の先頭に BOM のプレフィックスを付けることはなく、また、 GetByteCount メソッドがバイト数に BOM のバイト数を含めることはありません。
throwOnInvalidBytesがtrue場合、無効なバイト シーケンスを検出するメソッドはSystem.ArgumentException例外をスローします。 それ以外の場合、メソッドは例外をスローせず、無効なシーケンスは無視されます。
Caution
セキュリティ上の理由から、 throwOnInvalidBytes パラメーターを含むコンストラクターを呼び出し、そのパラメーターを true に設定することで、エラー検出を有効にする必要があります。