Exemple #1
0
        public Dictionary <TKey, TValue> ReadDictionary <TKey, TValue> ()
        {
            uint ln = ReadUInt32();

            if (ln > ProtocolInformation.MaxArrayLength)
            {
                throw new ProtocolException("Dict length " + ln + " exceeds maximum allowed " + ProtocolInformation.MaxArrayLength + " bytes");
            }

            var val = new Dictionary <TKey, TValue> ((int)(ln / 8));

            ReadPad(8);

            int endPos = _pos + (int)ln;

            var keyReader   = ReadMethodFactory.CreateReadMethodDelegate <TKey>();
            var valueReader = ReadMethodFactory.CreateReadMethodDelegate <TValue>();

            while (_pos < endPos)
            {
                ReadPad(8);
                TKey   k = keyReader(this);
                TValue v = valueReader(this);
                val.Add(k, v);
            }

            if (_pos != endPos)
            {
                throw new ProtocolException("Read pos " + _pos + " != ep " + endPos);
            }

            return(val);
        }
Exemple #2
0
        public T[] ReadArray <T> ()
        {
            uint ln       = ReadUInt32();
            Type elemType = typeof(T);

            if (ln > ProtocolInformation.MaxArrayLength)
            {
                throw new ProtocolException("Array length " + ln + " exceeds maximum allowed " + ProtocolInformation.MaxArrayLength + " bytes");
            }

            //advance to the alignment of the element
            ReadPad(ProtocolInformation.GetAlignment(elemType));

            if (elemType.GetTypeInfo().IsPrimitive)
            {
                // Fast path for primitive types (except bool which isn't blittable and take another path)
                if (elemType != typeof(bool))
                {
                    return(MarshalArray <T> (ln));
                }
                else
                {
                    return((T[])(Array)MarshalBoolArray(ln));
                }
            }

            var list   = new List <T> ();
            int endPos = _pos + (int)ln;

            var elementReader = ReadMethodFactory.CreateReadMethodDelegate <T>();

            while (_pos < endPos)
            {
                list.Add(elementReader(this));
            }

            if (_pos != endPos)
            {
                throw new ProtocolException("Read pos " + _pos + " != ep " + endPos);
            }

            return(list.ToArray());
        }