示例#1
0
 public static object DecodeObject(DataIndex data)
 {
     var currentChar = data.GetNext();
     switch (currentChar)
     {
         case control_char_dictionary_start:
             return DecodeDictionary(data);
         case control_char_list_start:
             return DecodeList(data);
         case control_char_long_start:
             return DecodeLong(data);
         default:
             int n;
             // Check for final case: a number, which represents a length of bytes.
             if (!int.TryParse(Encoding.UTF8.GetString(new[] { currentChar }), out n))
                 throw new BEncodeException("Invalid control character.");
             data.Index--; // This is a special circumstance that requires the "control" character to be parsed.
             return DecodeString(data);
     }
 }
示例#2
0
        private static byte[] DecodeString(DataIndex data)
        {
            var length = (int) DecodeLong(data, control_char_string_split);

            return data.GetBytes(length);
        }
示例#3
0
 private static void DecodeLoop(DataIndex data, byte endByte, Action action)
 {
     while (data.Get() != endByte)
     {
         action();
     }
     data.Index++;
 }
示例#4
0
        private static long DecodeLong(DataIndex data, byte endByte = control_char_long_end)
        {
            var numberString = Encoding.UTF8.GetString(data.GetBytesUntil(endByte));

            return Convert.ToInt64(numberString);
        }
示例#5
0
 private static List<object> DecodeList(DataIndex data)
 {
     var list = new List<object>();
     DecodeLoop(data, control_char_list_end, () => list.Add(DecodeObject(data)));
     return list;
 }
示例#6
0
 private static Dictionary<string, object> DecodeDictionary(DataIndex data)
 {
     var dictionary = new Dictionary<string, object>();
     DecodeLoop(data, control_char_dictionary_end, () => dictionary.Add(Encoding.UTF8.GetString(DecodeString(data)), DecodeObject(data)));
     return dictionary;
 }