TextEncoding.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Text;
  2. using MediaBrowser.Model.IO;
  3. using MediaBrowser.Model.Text;
  4. namespace Emby.Common.Implementations.TextEncoding
  5. {
  6. public class TextEncoding : ITextEncoding
  7. {
  8. private readonly IFileSystem _fileSystem;
  9. public TextEncoding(IFileSystem fileSystem)
  10. {
  11. _fileSystem = fileSystem;
  12. }
  13. public Encoding GetASCIIEncoding()
  14. {
  15. return Encoding.ASCII;
  16. }
  17. public Encoding GetFileEncoding(string srcFile)
  18. {
  19. // *** Detect byte order mark if any - otherwise assume default
  20. var buffer = new byte[5];
  21. using (var file = _fileSystem.OpenRead(srcFile))
  22. {
  23. file.Read(buffer, 0, 5);
  24. }
  25. if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
  26. return Encoding.UTF8;
  27. if (buffer[0] == 0xfe && buffer[1] == 0xff)
  28. return Encoding.Unicode;
  29. if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
  30. return Encoding.UTF32;
  31. if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76)
  32. return Encoding.UTF7;
  33. return null;
  34. }
  35. }
  36. }