jellyfin-server/Emby.Common.Implementations/TextEncoding/TextEncoding.cs

44 lines
1.2 KiB
C#
Raw Normal View History

2016-10-30 07:11:37 +00:00
using System.Text;
2016-11-01 04:07:12 +00:00
using MediaBrowser.Model.IO;
2016-11-08 18:44:23 +00:00
using MediaBrowser.Model.Text;
2016-10-30 07:11:37 +00:00
2016-11-03 22:34:16 +00:00
namespace Emby.Common.Implementations.TextEncoding
2016-10-30 07:11:37 +00:00
{
2016-11-08 18:44:23 +00:00
public class TextEncoding : ITextEncoding
2016-10-30 07:11:37 +00:00
{
2016-11-01 04:07:12 +00:00
private readonly IFileSystem _fileSystem;
public TextEncoding(IFileSystem fileSystem)
{
_fileSystem = fileSystem;
}
2016-11-08 18:44:23 +00:00
public Encoding GetASCIIEncoding()
2016-10-30 07:11:37 +00:00
{
2016-11-08 18:44:23 +00:00
return Encoding.ASCII;
2016-10-30 07:11:37 +00:00
}
2016-11-01 04:07:12 +00:00
public Encoding GetFileEncoding(string srcFile)
{
// *** Detect byte order mark if any - otherwise assume default
var buffer = new byte[5];
using (var file = _fileSystem.OpenRead(srcFile))
{
file.Read(buffer, 0, 5);
}
if (buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf)
return Encoding.UTF8;
if (buffer[0] == 0xfe && buffer[1] == 0xff)
return Encoding.Unicode;
if (buffer[0] == 0 && buffer[1] == 0 && buffer[2] == 0xfe && buffer[3] == 0xff)
return Encoding.UTF32;
if (buffer[0] == 0x2b && buffer[1] == 0x2f && buffer[2] == 0x76)
return Encoding.UTF7;
2016-11-03 22:34:16 +00:00
return null;
2016-11-01 04:07:12 +00:00
}
2016-10-30 07:11:37 +00:00
}
}