using System.IO; using System.Text; namespace SabreTools.IO { /// /// Extensions to File functionality /// public static class FileExtensions { /// /// Determines a text file's encoding by analyzing its byte order mark (BOM). /// Defaults to ASCII when detection of the text file's endianness fails. /// /// The text file to analyze. /// The detected encoding. /// http://stackoverflow.com/questions/3825390/effective-way-to-find-any-files-encoding public static Encoding GetEncoding(string filename) { if (!File.Exists(filename)) return Encoding.Default; // Try to open the file try { FileStream file = File.OpenRead(filename); if (file == null) return Encoding.Default; // Read the BOM var bom = new byte[4]; file.Read(bom, 0, 4); file.Dispose(); // Analyze the BOM if (bom[0] == 0x2b && bom[1] == 0x2f && bom[2] == 0x76) return Encoding.UTF7; if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf) return Encoding.UTF8; if (bom[0] == 0xff && bom[1] == 0xfe) return Encoding.Unicode; //UTF-16LE if (bom[0] == 0xfe && bom[1] == 0xff) return Encoding.BigEndianUnicode; //UTF-16BE if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff) return Encoding.UTF32; return Encoding.Default; } catch { return Encoding.Default; } } /// /// Returns if the first byte array starts with the second array /// /// First byte array to compare /// Second byte array to compare /// True if the input arrays should match exactly, false otherwise (default) /// True if the first byte array starts with the second, false otherwise public static bool StartsWith(this byte[] arr1, byte[] arr2, bool exact = false) { // If we have any invalid inputs, we return false if (arr1 == null || arr2 == null || arr1.Length == 0 || arr2.Length == 0 || arr2.Length > arr1.Length || (exact && arr1.Length != arr2.Length)) { return false; } // Otherwise, loop through and see for (int i = 0; i < arr2.Length; i++) { if (arr1[i] != arr2[i]) return false; } return true; } } }