< Summary

Information
Class: DirectSight.Common.FileHelper
Assembly DirectSight
File(s): /home/runner/work/DirectSight/DirectSight/DirectSight/Common/FileHelper.cs
Line coverage
93%
Covered lines: 46
Uncovered lines: 3
Coverable lines: 49
Total lines: 104
Line coverage: 93.8%
Branch coverage
83%
Covered branches: 25
Total branches: 30
Branch coverage: 83.3%
Method coverage

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
GetEncoding(...)83.33%6693.33%
GetEncodingByBOM(...)81.81%222290%
GetEncodingByParsing(...)100%22100%

File(s)

/home/runner/work/DirectSight/DirectSight/DirectSight/Common/FileHelper.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3using System.Text;
 4
 5namespace DirectSight.Common;
 6
 7/// <summary>
 8/// Helper class to detect encoding of files.
 9/// </summary>
 10public class FileHelper
 11{
 12    /// <summary>
 13    /// Determines a text file's encoding by analyzing its byte order mark (BOM) and if not found try parsing into difer
 14    /// Defaults to UTF8 when detection of the text file's endianness fails.
 15    /// </summary>
 16    /// <param name="filename">The text file to analyze.</param>
 17    /// <returns>The detected encoding.</returns>
 18    public static Encoding GetEncoding(string filename)
 7119    {
 7120        var encodingByBOM = GetEncodingByBOM(filename);
 7121        if (encodingByBOM != null)
 322        {
 323            return encodingByBOM;
 24        }
 25
 26        // BOM not found, so try to parse characters into several encodings
 6827        var encodingByParsingUTF8 = GetEncodingByParsing(filename, Encoding.UTF8);
 6828        if (encodingByParsingUTF8 != null)
 6729        {
 6730            return encodingByParsingUTF8;
 31        }
 32
 133        var encodingByParsingLatin1 = GetEncodingByParsing(filename, Encoding.GetEncoding("iso-8859-1"));
 134        if (encodingByParsingLatin1 != null)
 135        {
 136            return encodingByParsingLatin1;
 37        }
 38
 039        return Encoding.UTF8;
 7140    }
 41
 42    /// <summary>
 43    /// Determines a text file's encoding by analyzing its byte order mark (BOM).
 44    /// </summary>
 45    /// <param name="filename">The text file to analyze.</param>
 46    /// <returns>The detected encoding.</returns>
 47    private static Encoding GetEncodingByBOM(string filename)
 7148    {
 49        // Read the BOM
 7150        var byteOrderMark = new byte[4];
 7151        using (var file = new FileStream(filename, FileMode.Open, FileAccess.Read))
 7152        {
 7153            file.Read(byteOrderMark, 0, 4);
 7154        }
 55
 7156        if (byteOrderMark[0] == 0xef && byteOrderMark[1] == 0xbb && byteOrderMark[2] == 0xbf)
 157        {
 158            return Encoding.UTF8;
 59        }
 60
 7061        if (byteOrderMark[0] == 0xff && byteOrderMark[1] == 0xfe)
 162        {
 63            // UTF-16LE
 164            return Encoding.Unicode;
 65        }
 66
 6967        if (byteOrderMark[0] == 0xfe && byteOrderMark[1] == 0xff)
 168        {
 69            // UTF-16BE
 170            return Encoding.BigEndianUnicode;
 71        }
 72
 6873        if (byteOrderMark[0] == 0 && byteOrderMark[1] == 0 && byteOrderMark[2] == 0xfe && byteOrderMark[3] == 0xff)
 074        {
 075            return Encoding.UTF32;
 76        }
 77
 78        // no BOM found
 6879        return null;
 7180    }
 81
 82    private static Encoding GetEncodingByParsing(string filename, Encoding encoding)
 6983    {
 6984        var encodingVerifier = Encoding.GetEncoding(encoding.BodyName, new EncoderExceptionFallback(), new DecoderExcept
 85
 86        try
 6987        {
 6988            using (var textReader = new StreamReader(filename, encodingVerifier, detectEncodingFromByteOrderMarks: true)
 6989            {
 379190                while (!textReader.EndOfStream)
 372291                {
 372292                    textReader.ReadLine();   // in order to increment the stream position
 372293                }
 94
 95                // all text parsed ok
 6896                return textReader.CurrentEncoding;
 97            }
 98        }
 199        catch (Exception)
 1100        {
 1101            return null;
 102        }
 69103    }
 104}