/// <summary> /// Handler字典类的解码函数 /// </summary> /// <param name="source">待解码的字节数组</param> /// <param name="position">字节数组的解码位置</param> /// <returns>解码的字节数组长度</returns> public override int Decode(byte[] source, ref int position) { //保存初始位置 int start = position; //跳过字符'[' position++; try { //当遇到']'(ASCII码为93),解析结束 while (source[position] != 93) { //解析字符串 BytesNode keyNode = new BytesNode(); keyNode.Decode(source, ref position); byte[] key = keyNode.ByteArray; if (key.LongLength == 0) { throw new BEncodingException(ExDictNodeKeyLengthError); } //解析值节点 BEncodedNode valueNode = BEncode.Decode(source, ref position); //']'(ASCII码为93),解析结束 if (valueNode == null) { throw new BEncodingException(ExDictNodeValueIsNull); } //字典添加key和handler dict.Add(keyNode, valueNode); } } //当捕捉IndexOutOfRangeException,抛出BEncodingException catch (IndexOutOfRangeException) { throw new BEncodingException(ExDictNodeLengthError); } //当捕捉ArgumentException,抛出BEncodingException catch (ArgumentException) { throw new BEncodingException(ExDictNodeSameKey); } //跳过字符'e' position++; //返回所解析的数组长度 return(position - start); }
/// <summary> /// </summary> /// <param name="source">待解码的字节数组</param> /// <param name="position">字节数组的解码位置</param> /// <returns>解码的字节数组长度</returns> public override int Decode(byte[] source, ref int position) { //保存初始位置 int start = position; //跳过字符'{' position++; try { //当遇到'}'(ASCII码为125),解析结束 while (source[position] != 125) { BEncodedNode node = BEncode.Decode(source, ref position); //当遇到'}'(ASCII码为93125解析结束 if (node == null) { break; } //列表添加handler items.Add(node); } } //当捕捉IndexOutOfRangeException,抛出BEncodingException catch (IndexOutOfRangeException) { throw new BEncodingException(ExListNodeLengthError); } //跳过字符'e' position++; //返回所解析的数组长度 return(position - start); }