Example #1
0
        /// <summary>
        /// Get Encoding by name (e.g. gb2312)
        /// </summary>
        /// <param name="name">encoding name</param>
        /// <returns>DBCSEncoding</returns>
        public static DBCSEncoding GetDBCSEncoding(string name)
        {
            name = name.ToLower();
            DBCSEncoding encoding = new DBCSEncoding {
                _webName = name
            };

            if (_cache.ContainsKey(name))
            {
                var tuple = _cache[name];
                encoding._dbcsToUnicode = tuple.Item1;
                encoding._unicodeToDbcs = tuple.Item2;
                return(encoding);
            }

            var dbcsToUnicode = new char[0x10000];
            var unicodeToDbcs = new ushort[0x10000];

            /*
             * According to many feedbacks, add this automatic function for finding resource in revision 1.0.0.1.
             * We suggest that use the old method as below if you understand how to embed the resource.
             * Please make sure the *.bin file was correctly embedded if throw an exception at here.
             */
            //using (Stream stream = typeof(DBCSEncoding).Assembly.GetManifestResourceStream(typeof(DBCSEncoding).Namespace + "." + name + ".bin"))
            using (Stream stream = typeof(DBCSEncoding).GetTypeInfo()
                                   .Assembly
                                   .GetManifestResourceStream(typeof(DBCSEncoding).GetTypeInfo()
                                                              .Assembly
                                                              .GetManifestResourceNames()
                                                              .Single(s => s.EndsWith("." + name + ".bin"))))
                using (BinaryReader reader = new BinaryReader(stream))
                {
                    for (int i = 0; i < 0xffff; i++)
                    {
                        ushort u = reader.ReadUInt16();
                        unicodeToDbcs[i] = u;
                    }
                    for (int i = 0; i < 0xffff; i++)
                    {
                        ushort u = reader.ReadUInt16();
                        dbcsToUnicode[i] = (char)u;
                    }
                }

            _cache[name]            = new Tuple <char[], ushort[]>(dbcsToUnicode, unicodeToDbcs);
            encoding._dbcsToUnicode = dbcsToUnicode;
            encoding._unicodeToDbcs = unicodeToDbcs;
            return(encoding);
        }
Example #2
0
 public DBCSDecoder(DBCSEncoding encoding)
 {
     _encoding = encoding;
 }