/// <summary> /// 编码函数,返回字节流 /// </summary> /// <param name="source">待编码的B编码节点</param> /// <returns>已编码的字节流</returns> public static byte[] ByteArrayEncode(BEncodedNode source) { MemoryStream result = new MemoryStream(); source.Encode(result); return(result.ToArray()); }
/// <summary> /// 解码函数 /// </summary> /// <param name="source">待解码的字节数组</param> /// <returns>返回已解码的节点</returns> public static BEncodedNode Decode(byte[] source) { //初始化变量 int position = 0; ///当待解码字节数组的长度为零,抛出异常 if (source.Length == 0) { throw new BEncodingException(ExBytesLengthZero); } BEncodedNode result = Decode(source, ref position); if (position != source.Length) { throw new BEncodingException(ExBytesLengthTooLong); } return(result); }
/// <summary> /// 编码函数,返回字符串 /// </summary> /// <param name="source">待编码的Handler对象</param> /// <returns>已编码的字符串</returns> public static string StringEncode(BEncodedNode source) { return(Encoding.GetString(ByteArrayEncode(source))); }