Exemple #1
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));
        }
Exemple #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));
        }
Exemple #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));
        }
Exemple #4
0
 public void EncodeDictionary <T>(IDictionary <string, T> value)
 {
     Encode(Bincoding.ParseDictionaryValue <T>(value));
 }
Exemple #5
0
 public void EncodeKeyValue(string key, IDictionary <string, Bincoding> value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseDictionaryValue(value)));
 }
Exemple #6
0
 public void EncodeKeyValue(string key, ICollection <Bincoding> value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseListValue(value)));
 }
Exemple #7
0
 public void EncodeKeyValue(string key, IWriteStream value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Exemple #8
0
 public void EncodeKeyValue(string key, DateTime value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Exemple #9
0
 public void EncodeKeyValue(KeyValuePair <string, Bincoding> value)
 {
     Encode(Bincoding.ParseKeyValue(value));
 }
Exemple #10
0
 public void Encode(bool value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Exemple #11
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;
            }
        }
Exemple #12
0
 public static Bincoding ParseKeyValue(string key, Bincoding value)
 {
     return(new Bincoding(new KeyValuePair <string, Bincoding>(key, value)));
 }
Exemple #13
0
 public static Bincoding ParseKeyValue <T>(KeyValuePair <string, T> value)
 {
     return(Bincoding.ParseKeyValue(new KeyValuePair <string, Bincoding>(value.Key, Bincoding.ParseValue(value.Value))));
 }
Exemple #14
0
 public void EncodeList <T>(ICollection <T> value)
 {
     Encode(Bincoding.ParseListValue(value));
 }
Exemple #15
0
 public void Encode(ushort value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Exemple #16
0
 public void EncodeBinaryList(IWriteStream[] value)
 {
     Encode(Bincoding.ParseBinaryListValue(value));
 }
Exemple #17
0
 public void Encode(ulong value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Exemple #18
0
 public void EncodeKeyValue(string key, Bincoding value)
 {
     Encode(Bincoding.ParseKeyValue(key, value));
 }
Exemple #19
0
 public void Encode(DateTime value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Exemple #20
0
 public void EncodeKeyValue(string key, byte[] value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value)));
 }
Exemple #21
0
 public void Encode(byte[] value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Exemple #22
0
 public void EncodeKeyValue(string key, string value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseValue(value, false)));
 }
Exemple #23
0
 public void EncodeBinary(IWriteStream value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Exemple #24
0
 public void EncodeKeyValue(string key, IWriteStream[] value)
 {
     Encode(Bincoding.ParseKeyValue(key, Bincoding.ParseBinaryListValue(value)));
 }
Exemple #25
0
 public void Encode(string value, bool shortString = false)
 {
     Encode(Bincoding.ParseValue(value, shortString));
 }
Exemple #26
0
 public void EncodeDictionary(IDictionary <string, Bincoding> value)
 {
     Encode(Bincoding.ParseDictionaryValue(value));
 }
Exemple #27
0
 public void Encode(Stream value)
 {
     Encode(Bincoding.ParseValue(value));
 }
Exemple #28
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.");
            }
        }
Exemple #29
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.");
            }
        }