private int GetBomOffset(byte[] buffer) { byte[] bom = encoding.GetPreamble(); bool usesBom = true; for (int i = 0; i < bom.Length; ++i) { if (bom[i] != buffer[i]) { usesBom = false; } } return usesBom ? bom.Length : 0; }