Table of Contents

Class ByteOrderMark

Namespace
Cuemon.Text
Assembly
Cuemon.Kernel.dll

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

bytes byte[]

The byte array that contains the BOM to decode.

Returns

Encoding

The Encoding represented by the BOM in bytes.

Exceptions

ArgumentNullException

bytes is null.

ArgumentException

bytes does 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

input byte[]

The byte array to inspect.

fallbackEncoding Encoding

The encoding to return when detection fails.

Returns

Encoding

The detected encoding of input, or fallbackEncoding if detection fails. If fallbackEncoding is 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

value Stream

The stream to inspect.

fallbackEncoding Encoding

The encoding to return when detection fails.

Returns

Encoding

The detected encoding of value, or fallbackEncoding if detection fails.

Remove(byte[], Encoding)

Removes the preamble, if present, from the specified byte array.

public static byte[] Remove(byte[] bytes, Encoding encoding)

Parameters

bytes byte[]

The byte array to process.

encoding Encoding

The encoding used to determine which preamble to remove.

Returns

byte[]

A byte array whose content does not include the detected preamble.

Exceptions

ArgumentNullException

bytes or encoding is 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

value Stream

The stream to process.

encoding Encoding

The encoding used to determine which preamble to remove.

setup Action<DisposableOptions>

The delegate that configures disposable behavior.

Returns

Stream

A stream whose content does not include the detected preamble.

Exceptions

ArgumentNullException

value or encoding is 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

input byte[]

The byte array to inspect.

result Encoding

When this method returns, contains the detected Encoding if detection succeeds; otherwise, null.

Returns

bool

true if an encoding was detected; otherwise, false.

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

value Stream

The stream to inspect.

result Encoding

When this method returns, contains the detected Encoding if detection succeeds; otherwise, null.

Returns

bool

true if an encoding was detected; otherwise, false.

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.