Ejemplo n.º 1
0
        public override Dictionary <TKey, TValue> ConvertFromInternal(object value)
        {
            if (value is byte[])
            {
                var components = new Dictionary <TKey, TValue>();

                var keyTypeHint   = typeof(TKey);
                var valueTypeHint = typeof(TValue);

                using (var bytes = new MemoryStream((byte[])value))
                {
                    // number of key / value pairs
                    var numEntriesBytes = new byte[2];
                    if (bytes.Read(numEntriesBytes, 0, 2) <= 0)
                    {
                        return(components);
                    }

                    var nEntries = BitConverter.ToUInt16(numEntriesBytes, 0);
                    for (var i = 0; i < nEntries; i++)
                    {
                        //get the length of the key
                        var keyLengthBytes = new byte[2];
                        bytes.Read(keyLengthBytes, 0, 2);
                        var keyLength = BitConverter.ToUInt16(keyLengthBytes, 0);

                        //read the content of the key into a buffer
                        var keyBuffer = new byte[keyLength];
                        bytes.Read(keyBuffer, 0, keyLength);
                        var entryKey = CassandraObject.GetCassandraObjectFromDatabaseByteArray(keyBuffer, keyTypeHint);

                        //get the length of the value
                        var valueLengthBytes = new byte[2];
                        bytes.Read(valueLengthBytes, 0, 2);
                        var valueLength = BitConverter.ToUInt16(valueLengthBytes, 0);

                        //read the content of the key into a buffer
                        var valueBuffer = new byte[valueLength];
                        bytes.Read(valueBuffer, 0, valueLength);
                        var entryValue = CassandraObject.GetCassandraObjectFromDatabaseByteArray(valueBuffer, valueTypeHint);

                        components.Add((TKey)entryKey, (TValue)entryValue);
                    }
                }

                return(components);
            }

            if (value.GetType().GetInterfaces().Contains(typeof(IEnumerable <object>)))
            {
                return(new Dictionary <TKey, TValue>(((IEnumerable <object>)value).Cast <KeyValuePair <TKey, TValue> >().ToDictionary(k => k.Key, v => v.Value)));
            }

            if (value.GetType().GetInterfaces().Contains(typeof(IEnumerable <KeyValuePair <TKey, TValue> >)))
            {
                return(new Dictionary <TKey, TValue>(((IEnumerable <KeyValuePair <TKey, TValue> >)value).ToDictionary(k => k.Key, v => v.Value)));
            }

            return(null);
        }
Ejemplo n.º 2
0
        public override Dictionary <TKey, TValue> FromBigEndian(byte[] value)
        {
            var components = new Dictionary <TKey, TValue>();

            var keyTypeHint   = typeof(TKey);
            var valueTypeHint = typeof(TValue);

            if (value == null)
            {
                return(components);
            }
            using (var bytes = new MemoryStream((byte[])value))
            {
                // number of key / value pairs
                var numEntriesBytes = new byte[2];
                if (bytes.Read(numEntriesBytes, 0, 2) <= 0)
                {
                    return(components);
                }

                var nEntries = BitConverter.ToUInt16(ConvertEndian(numEntriesBytes), 0);
                for (var i = 0; i < nEntries; i++)
                {
                    //get the length of the key
                    var keyLengthBytes = new byte[2];
                    bytes.Read(keyLengthBytes, 0, 2);
                    var keyLength = BitConverter.ToUInt16(ConvertEndian(keyLengthBytes), 0);

                    //read the content of the key into a buffer
                    var keyBuffer = new byte[keyLength];
                    bytes.Read(keyBuffer, 0, keyLength);
                    var entryKey = CassandraObject.GetCassandraObjectFromDatabaseByteArray(keyBuffer, keyTypeHint);

                    //get the length of the value
                    var valueLengthBytes = new byte[2];
                    bytes.Read(valueLengthBytes, 0, 2);
                    var valueLength = BitConverter.ToUInt16(ConvertEndian(valueLengthBytes), 0);

                    //read the content of the key into a buffer
                    var valueBuffer = new byte[valueLength];
                    bytes.Read(valueBuffer, 0, valueLength);
                    var entryValue = CassandraObject.GetCassandraObjectFromDatabaseByteArray(valueBuffer, valueTypeHint);

                    components.Add((TKey)entryKey, (TValue)entryValue);
                }
            }

            return(components);
        }
Ejemplo n.º 3
0
        public override List <T> ConvertFromInternal(object value)
        {
            if (value is byte[])
            {
                var components = new List <T>();

                var typeHint = typeof(T);

                using (var bytes = new MemoryStream((byte[])value))
                {
                    // number of elements
                    var numElementsBytes = new byte[2];
                    if (bytes.Read(numElementsBytes, 0, 2) <= 0)
                    {
                        return(components);
                    }

                    var nElements = BitConverter.ToUInt16(numElementsBytes, 0);
                    for (var i = 0; i < nElements; i++)
                    {
                        //get the length of this element
                        var elementLengthBytes = new byte[2];
                        bytes.Read(elementLengthBytes, 0, 2);
                        var elementLength = BitConverter.ToUInt16(elementLengthBytes, 0);

                        //read the content of the element into a buffer
                        var buffer = new byte[elementLength];
                        bytes.Read(buffer, 0, elementLength);
                        var component = CassandraObject.GetCassandraObjectFromDatabaseByteArray(buffer, typeHint);
                        components.Add((T)component);
                    }
                }

                return(components);
            }

            if (value.GetType().GetInterfaces().Contains(typeof(IEnumerable <object>)))
            {
                return(new List <T>(((IEnumerable <object>)value).Cast <T>()));
            }

            if (value.GetType().GetInterfaces().Contains(typeof(IEnumerable <T>)))
            {
                return(new List <T>((IEnumerable <T>)value));
            }

            return(null);
        }
Ejemplo n.º 4
0
        public override List <T> FromBigEndian(byte[] value)
        {
            var components = new List <T>();

            var typeHint = typeof(T);

            if (value == null)
            {
                return(components);
            }
            using (var bytes = new MemoryStream(value))
            {
                // number of elements
                var numElementsBytes = new byte[2];
                if (bytes.Read(numElementsBytes, 0, 2) <= 0)
                {
                    return(components);
                }

                var nElements = BitConverter.ToUInt16(ConvertEndian(numElementsBytes), 0);
                for (var i = 0; i < nElements; i++)
                {
                    //get the length of this element
                    var elementLengthBytes = new byte[2];
                    bytes.Read(elementLengthBytes, 0, 2);
                    var elementLength = BitConverter.ToUInt16(ConvertEndian(elementLengthBytes), 0);

                    //read the content of the element into a buffer
                    var buffer = new byte[elementLength];
                    bytes.Read(buffer, 0, elementLength);
                    var component = CassandraObject.GetCassandraObjectFromDatabaseByteArray(buffer, typeHint);
                    components.Add((T)component);
                }
            }

            return(components);
        }