Utf8Helper.cs 752 B

12345678910111213141516171819202122232425262728293031
  1. using System.IO;
  2. using System.Text;
  3. using Abp.IO.Extensions;
  4. namespace Abp.Localization.Dictionaries
  5. {
  6. internal static class Utf8Helper
  7. {
  8. public static string ReadStringFromStream(Stream stream)
  9. {
  10. var bytes = stream.GetAllBytes();
  11. var skipCount = HasBom(bytes) ? 3 : 0;
  12. return Encoding.UTF8.GetString(bytes, skipCount, bytes.Length - skipCount);
  13. }
  14. private static bool HasBom(byte[] bytes)
  15. {
  16. if (bytes.Length < 3)
  17. {
  18. return false;
  19. }
  20. if (!(bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF))
  21. {
  22. return false;
  23. }
  24. return true;
  25. }
  26. }
  27. }