Exemple #1
0
        public BincodingDecoder(Stream s, string format)
        {
            if (format.Length != 2)
            {
                throw new ArgumentException("Argument 'format' must be of 2 characters.");
            }

            byte[] buffer = new byte[2];
            OffsetStream.StreamRead(s, buffer, 0, 2);

            if (format != Encoding.ASCII.GetString(buffer))
            {
                throw new InvalidDataException("Unable to decode: invalid data format.");
            }

            _s      = s;
            _format = format;

            int version = s.ReadByte();

            if (version < 0)
            {
                throw new EndOfStreamException();
            }

            _version = (byte)version;
        }
        public BinaryNumber(Stream s)
        {
            int length = s.ReadByte();

            if (length < 0)
            {
                throw new EndOfStreamException();
            }

            _number = new byte[length];
            OffsetStream.StreamRead(s, _number, 0, length);
        }
        public Bincoding DecodeNext()
        {
            if (_lastStream != null)
            {
                if ((_lastStream.Length - _lastStream.Position) > 0)
                {
                    OffsetStream.StreamCopy(_lastStream, new NullStream());
                }

                _lastStream = null;
            }

            BincodingType type = (BincodingType)_s.ReadByte();

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

            case BincodingType.BOOLEAN:
            case BincodingType.BYTE:
            {
                byte value = (byte)_s.ReadByte();
                return(new Bincoding(type, new byte[] { value }));
            }

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

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

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

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

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

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

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

                byte[] value = new byte[count];
                OffsetStream.StreamRead(_s, 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.GetValue(_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.GetValue(list));
            }

            case BincodingType.KEY_VALUE_PAIR:
            {
                int    keyLen    = _s.ReadByte();
                byte[] keyBuffer = new byte[keyLen];
                OffsetStream.StreamRead(_s, keyBuffer, 0, keyLen);

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

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

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

                Dictionary <string, Bincoding> dictionary = new Dictionary <string, Bincoding>(count);
                int    keyLen;
                byte[] keyBuffer = new byte[255];

                for (int i = 0; i < count; i++)
                {
                    keyLen = _s.ReadByte();
                    OffsetStream.StreamRead(_s, keyBuffer, 0, keyLen);

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

                    dictionary.Add(key, value);
                }

                return(Bincoding.GetValue(dictionary));
            }

            default:
                throw new InvalidDataException("Invalid bincoding type encountered while decoding data.");
            }
        }