aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/TextEncoding/TextEncoding.cs
diff options
context:
space:
mode:
authorLuke <luke.pulverenti@gmail.com>2016-11-03 19:59:50 -0400
committerGitHub <noreply@github.com>2016-11-03 19:59:50 -0400
commitc53745548ac2130f4cfbbe0d7a2804c36c8ae4eb (patch)
tree6ee298ebb5470c4f3bcbef8d814a0354901469c4 /Emby.Common.Implementations/TextEncoding/TextEncoding.cs
parent338b04a0c58729ec70aed89924ea6bd12422872b (diff)
parent405a5f69c5967b4d919b5fe91396f12cb83e8aa8 (diff)
Merge pull request #2267 from MediaBrowser/dev
Dev
Diffstat (limited to 'Emby.Common.Implementations/TextEncoding/TextEncoding.cs')
-rw-r--r--Emby.Common.Implementations/TextEncoding/TextEncoding.cs48
1 files changed, 48 insertions, 0 deletions
diff --git a/Emby.Common.Implementations/TextEncoding/TextEncoding.cs b/Emby.Common.Implementations/TextEncoding/TextEncoding.cs
new file mode 100644
index 000000000..35b869e43
--- /dev/null
+++ b/Emby.Common.Implementations/TextEncoding/TextEncoding.cs
@@ -0,0 +1,48 @@
+using System.Text;
+using MediaBrowser.Model.IO;
+using MediaBrowser.Model.TextEncoding;
+
+namespace Emby.Common.Implementations.TextEncoding
+{
+ public class TextEncoding : IEncoding
+ {
+ private readonly IFileSystem _fileSystem;
+
+ public TextEncoding(IFileSystem fileSystem)
+ {
+ _fileSystem = fileSystem;
+ }
+
+ public byte[] GetASCIIBytes(string text)
+ {
+ return Encoding.ASCII.GetBytes(text);
+ }
+
+ public string GetASCIIString(byte[] bytes, int startIndex, int length)
+ {
+ return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
+ }
+
+ 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;
+
+ return null;
+ }
+ }
+}