< Summary

Information
Class: ValidateLib.Encoding.Bom.BomDetector
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Encoding\Bom\BomDetector.cs
Line coverage
87%
Covered lines: 7
Uncovered lines: 1
Coverable lines: 8
Total lines: 34
Line coverage: 87.5%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBlocks covered Blocks not covered
ContainsUtf8Bom(...)83

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Encoding\Bom\BomDetector.cs

#LineLine coverage
 1namespace ValidateLib.Encoding.Bom
 2{
 3    /// <summary>
 4    /// Checks whether the first few bytes of a file contain a UTF-8 BOM (Byte Order Mark).
 5    /// </summary>
 6    internal class BomDetector
 7    {
 8        /// <summary>
 9        /// Checks if the provided file stream contains a UTF-8 BOM.
 10        /// </summary>
 11        /// <param name="fs">The file stream to check.</param>
 12        /// <returns>True if the file stream contains a UTF-8 BOM, otherwise false.</returns>
 13        public static bool ContainsUtf8Bom(FileStream fs)
 14        {
 115            if (fs.Length < 3)
 016                return false;
 17
 18            // Store the current position in the stream
 119            long originalPosition = fs.Position;
 20
 21            // Read the first three bytes
 122            byte[] bomBytes = new byte[3];
 123            fs.Read(bomBytes, 0, 3);
 24
 25            // Check for UTF-8 BOM (EF BB BF)
 126            bool hasUtf8Bom = bomBytes[0] == 0xEF && bomBytes[1] == 0xBB && bomBytes[2] == 0xBF;
 27
 28            // Reset the position in the stream
 129            fs.Seek(originalPosition, SeekOrigin.Begin);
 30
 131            return hasUtf8Bom;
 32        }
 33    }
 34}