Class ByteOrderMark
Provides static helper methods for detecting, decoding, and removing Unicode byte order marks (BOMs).
public static class ByteOrderMark
- Inheritance
-
ByteOrderMark
Methods
Decode(byte[])
Decodes the byte order mark (BOM) in the specified byte array to its corresponding Encoding.
public static Encoding Decode(byte[] bytes)
Parameters
bytesbyte[]The byte array that contains the BOM to decode.
Returns
Exceptions
- ArgumentNullException
bytesis null.- ArgumentException
bytesdoes not contain a recognizable byte order mark.
DetectEncodingOrDefault(byte[], Encoding)
Detects the encoding of the specified byte array, or returns a fallback encoding if detection fails.
public static Encoding DetectEncodingOrDefault(byte[] input, Encoding fallbackEncoding)
Parameters
inputbyte[]The byte array to inspect.
fallbackEncodingEncodingThe encoding to return when detection fails.
Returns
- Encoding
The detected encoding of
input, orfallbackEncodingif detection fails. IffallbackEncodingis null, DefaultEncoding is returned.
DetectEncodingOrDefault(Stream, Encoding)
Detects the encoding of the specified stream, or returns a fallback encoding if detection fails.
public static Encoding DetectEncodingOrDefault(Stream value, Encoding fallbackEncoding)
Parameters
valueStreamThe stream to inspect.
fallbackEncodingEncodingThe encoding to return when detection fails.
Returns
- Encoding
The detected encoding of
value, orfallbackEncodingif detection fails.
Remove(byte[], Encoding)
Removes the preamble, if present, from the specified byte array.
public static byte[] Remove(byte[] bytes, Encoding encoding)
Parameters
bytesbyte[]The byte array to process.
encodingEncodingThe encoding used to determine which preamble to remove.
Returns
- byte[]
A byte array whose content does not include the detected preamble.
Exceptions
- ArgumentNullException
bytesorencodingis null.
Remove(Stream, Encoding, Action<DisposableOptions>)
Removes the preamble, if present, from the specified stream.
public static Stream Remove(Stream value, Encoding encoding, Action<DisposableOptions> setup = null)
Parameters
valueStreamThe stream to process.
encodingEncodingThe encoding used to determine which preamble to remove.
setupAction<DisposableOptions>The delegate that configures disposable behavior.
Returns
- Stream
A stream whose content does not include the detected preamble.
Exceptions
- ArgumentNullException
valueorencodingis null.
TryDetectEncoding(byte[], out Encoding)
Tries to detect the encoding represented by the byte order mark in the specified byte array.
public static bool TryDetectEncoding(byte[] input, out Encoding result)
Parameters
inputbyte[]The byte array to inspect.
resultEncodingWhen this method returns, contains the detected Encoding if detection succeeds; otherwise, null.
Returns
TryDetectEncoding(Stream, out Encoding)
Tries to detect the encoding represented by the byte order mark in the specified stream.
public static bool TryDetectEncoding(Stream value, out Encoding result)
Parameters
valueStreamThe stream to inspect.
resultEncodingWhen this method returns, contains the detected Encoding if detection succeeds; otherwise, null.
Returns
Remarks
This method reads up to the first four bytes of value and restores the original stream position
before returning. The stream must support seeking.