Example #1
0
 public void EncodeKeyValue(string key, IDictionary <string, Bincoding> value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseDictionaryValue(value)));
 }
Example #2
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.");
            }
        }
Example #3
0
 public void EncodeKeyValue(string key, ICollection <Bincoding> value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseListValue(value)));
 }
Example #4
0
 public void EncodeKeyValue(string key, IWriteStream[] value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseBinaryListValue(value)));
 }
Example #5
0
 public void EncodeKeyValue(string key, IWriteStream value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Example #6
0
 public void EncodeKeyValue(string key, string value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value, false)));
 }
Example #7
0
 public void EncodeKeyValue(string key, byte[] value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Example #8
0
 public void EncodeKeyValue(string key, DateTime value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Example #9
0
 public void EncodeKeyValue(string key, Bincoding value)
 {
     Encode(Bincoding.ParseKeyValue(key, value));
 }
Example #10
0
 public void EncodeKeyValue(KeyValuePair <string, Bincoding> value)
 {
     Encode(Bincoding.ParseKeyValue(value));
 }
Example #11
0
 public static Bincoding ParseKeyValue <T>(KeyValuePair <string, T> value)
 {
     return(Bincoding.ParseKeyValue(new KeyValuePair <string, Bincoding>(value.Key, Bincoding.ParseValue(value.Value))));
 }