Exemple #1
0
        private object DeSerializeValueTypeAndStringArray(Type t, byte[] b)
        {
            char[] chars = Encoding.UTF8.GetChars(b);

            Type elementType = t.GetElementType();



            if (b.Length < 3)
            {
                throw new DeSerializeException("字节流长度过短 ,不是有效的字节流 。 字节流长度 : " + b.Length + " 。");
            }

            if (chars[0] != 'a')
            {
                throw new DeSerializeException("数组字节流第 1 个字符应该是 \"a\" 。");
            }

            if (chars[1] != ' ')
            {
                throw new DeSerializeException("数组字节流第 2 个字符应该是 空格 \" \" 。");
            }


            int i = 2;
            int j;

            int elementCount;


            j = StrUtil.FindForward(chars, i, ' ');


            if (j == -1)
            {
                throw new DeSerializeException("找不到 Array 的 elementCount 项 的 结尾符 空格 \" \" 。 从第 " + i + " 个字符开始寻找 。");
            }

            if (j == i)
            {
                throw new DeSerializeException("Array 的 elementCount 项 应是 数字 。 在第 " + i + " 个字符 。");
            }

            if (!int.TryParse(new string(chars, i, j - 2 + 1), out elementCount))
            {
                throw new DeSerializeException("Array 的 elementCount 项 应是 数字 。 在第 " + i + " 个字符 。");
            }


            i = j + 1;


            Array array = Array.CreateInstance(elementType, elementCount);

            int arrayIndex = 0;

            int length;

            object o;

            string value;

            while (true)
            {
                if (i >= chars.Length)
                {
                    break;
                }

                j = StrUtil.FindForward(chars, i, ' ');

                if (j == -1)
                {
                    throw new DeSerializeException("找不到 Array 的 element 项 的 length 项 的 结尾符 空格 \" \" 。 从第 " + i + "个字符开始寻找 。");
                }

                if (j == i)
                {
                    throw new DeSerializeException("Array 的 element 项 的 length 项 应是 数字 。 在第 " + i + " 个字符 。");
                }

                if (!int.TryParse(new string(chars, i, j - i), out length))
                {
                    throw new DeSerializeException("Array 的 element 项 的 length 项 应是 数字 。 在第 " + i + " 个字符 。");
                }



                value = new string(chars, j + 1, length);

                o = Util.ConvertForDeSerialize(value, elementType);


                array.SetValue(o, arrayIndex++);


                i = j + 1 + length;
            }

            return(array);
        }
Exemple #2
0
        private Dictionary <string, string> GetDataDic(char[] chars, int beginIndex, int endIndex)
        {
            Dictionary <string, string> dataDic = new Dictionary <string, string>();


            if (endIndex - beginIndex < 2)
            {
                throw new DeSerializeException("用于反序列化对象的字节流长度过短 , 不是有效的字节流 。 字节流 beginIndex : " + beginIndex + " endIndex : " + endIndex + " 。");
            }

            if (chars[beginIndex] != 'o')
            {
                throw new DeSerializeException("对象字节流第 1 个字符应该是 \"o\" 。 在第 " + beginIndex + " 个字符 。");
            }


            int i = beginIndex + 1;


            if (chars[i] != ' ')
            {
                throw new DeSerializeException("对象字节流第 2 个字符应该是 空格 \" \" 。 在第 " + i + " 个字符 。");
            }


            i = beginIndex + 2;

            int j;

            int length;

            string name;
            string value;

            while (true)
            {
                if (i > endIndex)
                {
                    break;
                }

                j = StrUtil.FindForward(chars, i, ' ');

                if (j == -1)
                {
                    throw new DeSerializeException("找不到 Property Field 的 name 项 的 length 项 的 结尾符 空格 \" \" 。 从第 " + i + " 个字符开始寻找 。");
                }

                if (j == i)
                {
                    throw new DeSerializeException("Property Field 的 name 项 的 length 项 应是 数字 。 在第 " + i + " 个字符 。");
                }

                if (!int.TryParse(new string(chars, i, j - i), out length))
                {
                    throw new DeSerializeException("Property Field 的 name 项 的 length 项 应是 数字 。 在第 " + i + " 个字符 。");
                }


                name = new string(chars, j + 1, length);


                i = j + 1 + length;



                j = StrUtil.FindForward(chars, i, ' ');


                if (j == -1)
                {
                    throw new DeSerializeException("找不到 Property Field 的 value 项 的 length 项 的 结尾符 空格 \" \" 。 从第 " + i + "个字符开始寻找 。");
                }

                if (j == i)
                {
                    throw new DeSerializeException("Property Field 的 value 项 的 length 项 应是 数字 。 在第 " + i + " 个字符 。");
                }

                if (!int.TryParse(new string(chars, i, j - i), out length))
                {
                    throw new DeSerializeException("Property Field 的 value 项 的 length 项 应是 数字 。 在第 " + i + " 个字符 。");
                }


                value = new string(chars, j + 1, length);


                i = j + 1 + length;


                dataDic.Add(name, value);
            }

            return(dataDic);
        }