Пример #1
0
        public static Bincoding GetValue(IWriteStream[] value)
        {
            List <Bincoding> value2 = new List <Bincoding>(value.Length);

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

            return(new Bincoding(value2));
        }
Пример #2
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));
        }
Пример #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 Encode(byte[] value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #5
0
 public void Encode(DateTime value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #6
0
 public void Encode(string key, List <Bincoding> value)
 {
     Encode(Bincoding.GetValue(key, Bincoding.GetValue(value)));
 }
Пример #7
0
 public void Encode(Dictionary <string, Bincoding> value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #8
0
 public void EncodeKeyValue(string key, string value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value, false)));
 }
Пример #9
0
 public void Encode(string key, DateTime value)
 {
     Encode(Bincoding.GetValue(key, Bincoding.GetValue(value)));
 }
Пример #10
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.");
            }
        }
Пример #11
0
 public void Encode(List <Bincoding> value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #12
0
 public void EncodeDictionary(IDictionary <string, Bincoding> value)
 {
     Encode(Bincoding.ParseDictionaryValue(value));
 }
Пример #13
0
 public void EncodeDictionary <T>(IDictionary <string, T> value)
 {
     Encode(Bincoding.ParseDictionaryValue <T>(value));
 }
Пример #14
0
 public void EncodeKeyValue(string key, IDictionary <string, Bincoding> value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseDictionaryValue(value)));
 }
Пример #15
0
 public void EncodeKeyValue(string key, IWriteStream[] value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseBinaryListValue(value)));
 }
Пример #16
0
 public void EncodeKeyValue(string key, ICollection <Bincoding> value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseListValue(value)));
 }
Пример #17
0
 public void Encode(string value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #18
0
 public void Encode(KeyValuePair <string, Bincoding> value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #19
0
 public void Encode(Stream value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #20
0
 public void EncodeKeyValue(string key, IWriteStream value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Пример #21
0
 public void Encode(IWriteStream[] value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #22
0
 public static Bincoding GetValue(string key, Bincoding value)
 {
     return(new Bincoding(new KeyValuePair <string, Bincoding>(key, value)));
 }
Пример #23
0
 public void Encode(string key, ulong value)
 {
     Encode(Bincoding.GetValue(key, Bincoding.GetValue(value)));
 }
Пример #24
0
        public void Encode(Bincoding value)
        {
            _s.WriteByte((byte)value.Type);

            switch (value.Type)
            {
            case BincodingType.NULL:
                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));
                OffsetStream.StreamCopy(stream, _s);
                break;

            case BincodingType.LIST:
                List <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:
                Dictionary <string, Bincoding> dictionary = value.GetDictionary();

                WriteLength(_s, dictionary.Count);

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

                break;

            default:
                _s.Write(value.Value, 0, value.Value.Length);
                break;
            }
        }
Пример #25
0
 public void Encode(string key, byte[] value)
 {
     Encode(Bincoding.GetValue(key, Bincoding.GetValue(value)));
 }
Пример #26
0
 public void Encode(bool value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #27
0
 public void Encode(string key, IWriteStream[] value)
 {
     Encode(Bincoding.GetValue(key, Bincoding.GetValue(value)));
 }
Пример #28
0
 public void Encode(short value)
 {
     Encode(Bincoding.GetValue(value));
 }
Пример #29
0
        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.");
            }
        }
Пример #30
0
 public void Encode(uint value)
 {
     Encode(Bincoding.GetValue(value));
 }