public void Int32Test()
        {
            Int32       expected = Int32.MaxValue - 32;
            SuperStream target   = new SuperStream(Endianess.Little);

            target.WriteInt32(expected);
            target.Position = 0;
            var actual = target.ReadInt32();

            Assert.AreEqual(expected, actual);
        }
Exemple #2
0
        public override int GetHashCode()
        {
            // Handle special cases
            if (this.Bytes == null)
            {
                return(0);
            }
            if (this.Bytes.Length == 0)
            {
                return(0);
            }
            if (this.Bytes.Length == 1)
            {
                return(this.Bytes[0]);
            }
            if (this.Bytes.Length == 2)
            {
                return(BitConverter.ToInt16(this.Bytes, 0));
            }
            if (this.Bytes.Length == 3)
            {
                return(BitConverter.ToInt16(this.Bytes, 0) + this.Bytes[2]);
            }
            if (this.Bytes.Length == 4)
            {
                return(BitConverter.ToInt32(this.Bytes, 0));
            }

            // Handle normal cases.
            int[] integers = new int[this.Bytes.Length / 4];
            var   ms       = new SuperStream(new MemoryStream(this.Bytes), BitConverter.IsLittleEndian ? Endianess.Little : Endianess.Big);

            for (int i = 0; i < integers.Length; i++)
            {
                integers[i] = ms.ReadInt32();
            }
            int hash = integers[0];

            for (int i = 1; i < integers.Length; i++)
            {
                hash ^= integers[i];
            }
            return(hash);
        }
        private bool GetValue(Type dataType, SuperStream stream, out object value)
        {
            if (dataType == typeof(Byte))
            {
                value = (byte)stream.ReadByte();
                return(true);
            }

            if (dataType == typeof(Int64))
            {
                value = stream.ReadInt64();
                return(true);
            }
            if (dataType == typeof(UInt64))
            {
                value = stream.ReadUInt64();
                return(true);
            }

            if (dataType == typeof(Int32))
            {
                value = stream.ReadInt32();
                return(true);
            }
            if (dataType == typeof(UInt32))
            {
                value = stream.ReadUInt32();
                return(true);
            }

            if (dataType == typeof(Int16))
            {
                value = stream.ReadInt16();
                return(true);
            }
            if (dataType == typeof(UInt16))
            {
                value = stream.ReadUInt16();
                return(true);
            }

            if (dataType == typeof(Single))
            {
                value = stream.ReadSingle();
                return(true);
            }

            if (dataType == typeof(Double))
            {
                value = stream.ReadDouble();
                return(true);
            }

            if (dataType == typeof(bool))
            {
                var val = stream.ReadUInt32();
                if (val == 0)
                {
                    value = false;
                    return(true);
                }
                else if (val == 1)
                {
                    value = true;
                    return(true);
                }
                else
                {
                    throw new Exception("Parsing type bool: Expected value to be either 0 or 1, but it was " + val.ToString());
                }
            }

            if (dataType.IsEnum)
            {
                object readValue;
                // Read the enums underlying type
                if (!this.GetValue(Enum.GetUnderlyingType(dataType), stream, out readValue))
                {
                    value = null;
                    return(false);
                }
                // Parse enum using the read value.
                value = Enum.ToObject(dataType, readValue);
                return(true);
            }

            // Test if type has StreamDataAttribute on properties.
            // This allows nesting of StreamData-aware task.DataTypes
            var props = StreamDataInfo.GetProperties(dataType);

            if (props.Length == 0)
            {
                value = null;
                return(false);
            }

            value = StreamData.Create(dataType, stream);
            if (value == null)
            {
                return(false);
            }
            return(true);
        }