Example #1
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 #2
0
        public void Encode(Bincoding value)
        {
            _s.WriteByte((byte)value.Type);

            switch (value.Type)
            {
            case BincodingType.NULL:
                break;

            case BincodingType.SHORTSTRING:
                _s.WriteByte(Convert.ToByte(value.Value.Length));
                _s.Write(value.Value, 0, value.Value.Length);
                break;

            case BincodingType.BINARY:
            case BincodingType.STRING:
                WriteLength(_s, value.Value.Length);
                _s.Write(value.Value, 0, value.Value.Length);
                break;

            case BincodingType.STREAM:
                Stream stream = value.GetValueStream();

                WriteLength(_s, Convert.ToInt32(stream.Length - stream.Position));
                stream.CopyTo(_s);
                break;

            case BincodingType.LIST:
                ICollection <Bincoding> list = value.GetList();

                WriteLength(_s, list.Count);

                foreach (Bincoding item in list)
                {
                    Encode(item);
                }

                break;

            case BincodingType.KEY_VALUE_PAIR:
                KeyValuePair <string, Bincoding> keyValue = value.GetKeyValuePair();

                byte[] keyBuffer = Encoding.UTF8.GetBytes(keyValue.Key);
                _s.WriteByte(Convert.ToByte(keyBuffer.Length));
                _s.Write(keyBuffer, 0, keyBuffer.Length);

                Encode(keyValue.Value);

                break;

            case BincodingType.DICTIONARY:
                IDictionary <string, Bincoding> dictionary = value.GetDictionary();

                WriteLength(_s, dictionary.Count);

                foreach (KeyValuePair <string, Bincoding> item in dictionary)
                {
                    EncodeKeyValue(item);
                }

                break;

            default:
                _s.Write(value.Value, 0, value.Value.Length);
                break;
            }
        }