Beispiel #1
0
        /// <summary>
        /// 编码函数,返回字节数组
        /// </summary>
        /// <param name="source">待编码的Handler对象</param>
        /// <returns>已编码的字节数组</returns>
        public static byte[] ByteArrayEncode(BEncodedNode source)
        {
            MemoryStream msw = new MemoryStream();

            source.Encode(msw);
            return(msw.ToArray());
        }
Beispiel #2
0
        /// <summary>
        /// 安全解码函数,带错误检测
        /// </summary>
        /// <param name="source">待解码的字节数组</param>
        /// <returns>返回已解码的Handler基类</returns>
        public static BEncodedNode Decode(byte[] source)
        {
            //初始化变量
            int position = 0;

            ///当待解码字节数组的长度为零,抛出异常
            if (source.Length == 0)
            {
                throw new BitTorrentException("待解码的字节数组的长度为零");
            }

            BEncodedNode interpreter = Decode(source, ref position);

            if (position != source.Length)
            {
                throw new BitTorrentException("解码的字节数组长度异常");
            }
            return(interpreter);
        }
Beispiel #3
0
 /// <summary>
 /// 编码函数,返回字符串
 /// </summary>
 /// <param name="source">待编码的Handler对象</param>
 /// <returns>已编码的字符串</returns>
 public static string StringEncode(BEncodedNode source)
 {
     return(Encoding.UTF8.GetString(ByteArrayEncode(source)));
 }