DetectEncoding() public method

public DetectEncoding ( byte buffer, int size ) : EncodingEnum
buffer byte
size int
return EncodingEnum
Beispiel #1
0
        /// <summary>
        /// Returns the encoding of the input file
        /// </summary>
        /// <param name="srcFile"></param>
        /// <returns></returns>
        public static Encoding GetFileEncoding(string srcFile)
        {
            Encoding encoding = Encoding.Default;

            if (string.IsNullOrEmpty(srcFile) || !File.Exists(srcFile))
            {
                return(encoding);
            }

            // Read in the file in binary
            byte[] buffer;
            try {
                using (var file = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                    using (var memoryStream = new MemoryStream()) {
                        file.CopyTo(memoryStream);
                        buffer = memoryStream.ToArray();
                    }
                }
            } catch (Exception) {
                return(encoding);
            }

            // Detect encoding
            var          textDetect = new TextEncodingDetect();
            EncodingEnum textEnc    = textDetect.DetectEncoding(buffer, buffer.Length);

            switch (textEnc)
            {
            case EncodingEnum.Ascii:
                // ASCII (chars in the 0-127 range)
                encoding = Encoding.ASCII;
                break;

            case EncodingEnum.Ansi:
                // ANSI (chars in the range 0-255 range)
                encoding = Encoding.GetEncoding(1252);
                break;

            case EncodingEnum.Utf8Bom:
            case EncodingEnum.Utf8Nobom:
                // UTF-8
                encoding = Encoding.UTF8;
                break;

            case EncodingEnum.Utf16LeBom:
            case EncodingEnum.Utf16LeNobom:
                // UTF-16 Little Endian
                encoding = Encoding.Unicode;
                break;

            case EncodingEnum.Utf16BeBom:
            case EncodingEnum.Utf16BeNobom:
                // UTF-16 Big Endian
                encoding = Encoding.BigEndianUnicode;
                break;
            }

            return(encoding);
        }
Beispiel #2
0
        /// <summary>
        /// Returns the encoding of the input file
        /// </summary>
        /// <param name="srcFile"></param>
        /// <returns></returns>
        public static Encoding GetFileEncoding(string srcFile)
        {
            Encoding encoding = Encoding.Default;

            if (string.IsNullOrEmpty(srcFile) || !File.Exists(srcFile)) {
                return encoding;
            }

            // Read in the file in binary
            byte[] buffer;
            try {
                using (var file = new FileStream(srcFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) {
                    using (var memoryStream = new MemoryStream()) {
                        file.CopyTo(memoryStream);
                        buffer = memoryStream.ToArray();
                    }
                }
            } catch (Exception) {
                return encoding;
            }

            // Detect encoding
            var textDetect = new TextEncodingDetect();
            EncodingEnum textEnc = textDetect.DetectEncoding(buffer, buffer.Length);

            switch (textEnc) {
                case EncodingEnum.Ascii:
                    // ASCII (chars in the 0-127 range)
                    encoding = Encoding.ASCII;
                    break;
                case EncodingEnum.Ansi:
                    // ANSI (chars in the range 0-255 range)
                    encoding = Encoding.GetEncoding(1252);
                    break;
                case EncodingEnum.Utf8Bom:
                case EncodingEnum.Utf8Nobom:
                    // UTF-8
                    encoding = Encoding.UTF8;
                    break;
                case EncodingEnum.Utf16LeBom:
                case EncodingEnum.Utf16LeNobom:
                    // UTF-16 Little Endian
                    encoding = Encoding.Unicode;
                    break;
                case EncodingEnum.Utf16BeBom:
                case EncodingEnum.Utf16BeNobom:
                    // UTF-16 Big Endian
                    encoding = Encoding.BigEndianUnicode;
                    break;
            }

            return encoding;
        }