Пример #1
0
        public static Bincoding ParseListValue <T>(ICollection <T> value)
        {
            List <Bincoding> list = new List <Bincoding>(value.Count);

            foreach (T item in value)
            {
                list.Add(Bincoding.ParseValue <T>(item));
            }

            return(Bincoding.ParseListValue(list));
        }
Пример #2
0
        public static Bincoding ParseBinaryListValue(IWriteStream[] value)
        {
            List <Bincoding> value2 = new List <Bincoding>(value.Length);

            foreach (IWriteStream item in value)
            {
                value2.Add(Bincoding.ParseValue(item));
            }

            return(new Bincoding(value2));
        }
Пример #3
0
        public static Bincoding ParseDictionaryValue <T>(IDictionary <string, T> value)
        {
            IDictionary <string, Bincoding> dict = new Dictionary <string, Bincoding>(value.Count);

            foreach (KeyValuePair <string, T> item in value)
            {
                dict.Add(item.Key, Bincoding.ParseValue <T>(item.Value));
            }

            return(Bincoding.ParseDictionaryValue(dict));
        }
Пример #4
0
 public void EncodeKeyValue(string key, IWriteStream value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Пример #5
0
 public void EncodeKeyValue(string key, byte[] value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Пример #6
0
 public void EncodeKeyValue(string key, DateTime value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Пример #7
0
 public void Encode(Stream value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Пример #8
0
 public void Encode(string value, bool shortString = false)
 {
     Encode(Bincoding.ParseValue(value, shortString));
 }
Пример #9
0
 public void EncodeBinary(IWriteStream value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Пример #10
0
 public void Encode(DateTime value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Пример #11
0
 public void Encode(ulong value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Пример #12
0
 public void Encode(ushort value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Пример #13
0
 public void Encode(bool value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Пример #14
0
 public static Bincoding ParseKeyValue <T>(KeyValuePair <string, T> value)
 {
     return(Bincoding.ParseKeyValue(new KeyValuePair <string, Bincoding>(value.Key, Bincoding.ParseValue(value.Value))));
 }
Пример #15
0
 public void EncodeKeyValue(string key, string value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value, false)));
 }
Пример #16
0
 public void Encode(byte[] value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Пример #17
0
        public Bincoding DecodeNext()
        {
            if (_lastStream != null)
            {
                if ((_lastStream.Length - _lastStream.Position) > 0)
                {
                    _lastStream.CopyTo(Stream.Null, 4096);
                }

                _lastStream = null;
            }

            int intType = _s.ReadByte();

            if (intType < 0)
            {
                return(null);
            }

            BincodingType type = (BincodingType)intType;

            switch (type)
            {
            case BincodingType.NULL:
                return(new Bincoding(type, null));

            case BincodingType.SHORTSTRING:
            {
                int len = _s.ReadByte();
                if (len < 0)
                {
                    throw new EndOfStreamException();
                }

                byte[] value = new byte[len];
                _s.ReadBytes(value, 0, value.Length);

                return(new Bincoding(type, value));
            }

            case BincodingType.BOOLEAN:
            case BincodingType.BYTE:
            {
                int value = _s.ReadByte();
                if (value < 0)
                {
                    throw new EndOfStreamException();
                }

                return(new Bincoding(type, new byte[] { (byte)value }));
            }

            case BincodingType.SHORT:
            case BincodingType.USHORT:
            {
                byte[] value = new byte[2];
                _s.ReadBytes(value, 0, 2);

                return(new Bincoding(type, value));
            }

            case BincodingType.INTEGER:
            case BincodingType.UINTEGER:
            {
                byte[] value = new byte[4];
                _s.ReadBytes(value, 0, 4);

                return(new Bincoding(type, value));
            }

            case BincodingType.LONG:
            case BincodingType.ULONG:
            case BincodingType.DATETIME:
            {
                byte[] value = new byte[8];
                _s.ReadBytes(value, 0, 8);

                return(new Bincoding(type, value));
            }

            case BincodingType.BINARY:
            case BincodingType.STRING:
            {
                int count = ReadLength(_s);

                byte[] value = new byte[count];
                _s.ReadBytes(value, 0, count);

                return(new Bincoding(type, value));
            }

            case BincodingType.STREAM:
            {
                int count = ReadLength(_s);

                _lastStream = new OffsetStream(_s, _s.Position, count, true, false);

                return(Bincoding.ParseValue(_lastStream));
            }

            case BincodingType.LIST:
            {
                int count = ReadLength(_s);

                List <Bincoding> list = new List <Bincoding>(count);

                for (int i = 0; i < count; i++)
                {
                    list.Add(DecodeNext());
                }

                return(Bincoding.ParseListValue(list));
            }

            case BincodingType.KEY_VALUE_PAIR:
            {
                int keyLen = _s.ReadByte();
                if (keyLen < 0)
                {
                    throw new EndOfStreamException();
                }

                byte[] keyBuffer = new byte[keyLen];
                _s.ReadBytes(keyBuffer, 0, keyLen);

                string    key   = Encoding.UTF8.GetString(keyBuffer, 0, keyLen);
                Bincoding value = DecodeNext();

                return(Bincoding.ParseKeyValue(new KeyValuePair <string, Bincoding>(key, value)));
            }

            case BincodingType.DICTIONARY:
            {
                int count = ReadLength(_s);
                Dictionary <string, Bincoding> dictionary = new Dictionary <string, Bincoding>(count);

                for (int i = 0; i < count; i++)
                {
                    Bincoding entry = DecodeNext();
                    KeyValuePair <string, Bincoding> pair = entry.GetKeyValuePair();

                    dictionary.Add(pair.Key, pair.Value);
                }

                return(Bincoding.ParseDictionaryValue(dictionary));
            }

            default:
                throw new InvalidDataException("Invalid bincoding type encountered while decoding data.");
            }
        }
Пример #18
0
        public static Bincoding ParseValue <T>(T value)
        {
            if (value is Bincoding)
            {
                return((Bincoding)(object)value);
            }
            else if (value is bool)
            {
                return(Bincoding.ParseValue((bool)(object)value));
            }
            else if (value is byte)
            {
                return(Bincoding.ParseValue((byte)(object)value));
            }
            else if (value is short)
            {
                return(Bincoding.ParseValue((short)(object)value));
            }
            else if (value is int)
            {
                return(Bincoding.ParseValue((int)(object)value));
            }
            else if (value is long)
            {
                return(Bincoding.ParseValue((long)(object)value));
            }
            else if (value is ushort)
            {
                return(Bincoding.ParseValue((ushort)(object)value));
            }
            else if (value is uint)
            {
                return(Bincoding.ParseValue((uint)(object)value));
            }
            else if (value is ulong)
            {
                return(Bincoding.ParseValue((ulong)(object)value));
            }
            else if (value is byte[])
            {
                return(Bincoding.ParseValue((byte[])(object)value));
            }
            else if (value is string)
            {
                return(Bincoding.ParseValue((string)(object)value, false));
            }
            else if (value is Stream)
            {
                return(Bincoding.ParseValue((Stream)(object)value));
            }
            else if (value is DateTime)
            {
                return(Bincoding.ParseValue((DateTime)(object)value));
            }
            else if (value is object)
            {
                if (value == null)
                {
                    return(Bincoding.GetNullValue());
                }

                throw new IOException("Type not supported.");
            }
            else
            {
                throw new IOException("Type not supported.");
            }
        }