Example #1
0
 internal static bool TryDecode(BinaryReader reader, Encoding encoding, out BEncodedList bencodedList)
 {
     try
     {
         bencodedList = Decode(reader, encoding);
     }
     catch (Exception e)
     {
         bencodedList = null;
         return false;
     }
     return true;
 }
Example #2
0
 public static bool TryDecode(Stream value, Encoding encoding, out BEncodedList bencodedList)
 {
     try
     {
         bencodedList = Decode(value, encoding);
     }
     catch (Exception e)
     {
         bencodedList = null;
         return false;
     }
     return true;
 }
Example #3
0
        internal static BEncodedList Decode(BinaryReader reader, Encoding encoding)
        {
            if (encoding == null) throw new ArgumentNullException("encoding");
            if (!reader.BaseStream.CanSeek) throw new NotSupportedException("Cannot decode non-seekable streams");

            IBEncodedValue value;
            BEncodedList list = new BEncodedList(encoding);
            char peekChar = (char)reader.PeekChar();

            if (peekChar == BEncodingSettings.ListStart)
            {
                reader.ReadChar();
                peekChar = (char)reader.PeekChar();

                while (peekChar != BEncodingSettings.ListEnd)
                {
                    try
                    {
                        switch (peekChar)
                        {
                            case BEncodingSettings.DictionaryStart:
                                value = BEncodedDictionary.Decode(reader, (Encoding)encoding.Clone());
                                break;
                            case BEncodingSettings.ListStart:
                                value = BEncodedList.Decode(reader, (Encoding)encoding.Clone());
                                break;
                            case BEncodingSettings.IntegerStart:
                                value = BEncodedInteger.Decode(reader);
                                break;
                            default:
                                if (Array.IndexOf(BEncodingSettings.NumericMask, peekChar) != -1)
                                {
                                    value = BEncodedString.Decode(reader, (Encoding)encoding.Clone());
                                }
                                else
                                {
                                    throw BEncodedFormatDecodeException.CreateTraced("Expected integer value", reader.BaseStream);
                                }
                                break;
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }

                    list.Add(value);
                    peekChar = (char)reader.PeekChar();
                }
                reader.ReadChar();
                return list;
            }
            else
            {
                throw BEncodedFormatDecodeException.CreateTraced("Expected list start token", reader.BaseStream);
            }
        }
Example #4
0
 public static bool TryDecode(byte[] value, out BEncodedList bencodedList)
 {
     try
     {
         bencodedList = Decode(value, BEncodingSettings.DefaultEncoding);
     }
     catch (Exception e)
     {
         bencodedList = null;
         return false;
     }
     return true;
 }