/// <summary>
        /// Read a <see cref="System.Boolean"/> from a <see cref="Stream"/>.
        /// </summary>
        public static bool ReadBoolean(this IByteEncoding encoding, Stream stream)
        {
            var u = encoding.ReadUInt64(stream);

            if (u > 1)
            {
                throw new InvalidDataException("Encoded value exceeds Boolean bounds.");
            }
            return(u == 1);
        }
        /// <summary>
        /// Read a <see cref="System.Char"/> from a <see cref="Stream"/>.
        /// </summary>
        public static char ReadChar(this IByteEncoding encoding, Stream stream)
        {
            var u = encoding.ReadUInt64(stream);

            if (u > ushort.MaxValue)
            {
                throw new InvalidDataException("Encoded value exceeds Char bounds.");
            }
            return((char)u);
        }
        /// <summary>
        /// Read a <see cref="System.SByte"/> from a <see cref="Stream"/>.
        /// </summary>
        public static sbyte ReadSByte(this IByteEncoding encoding, Stream stream)
        {
            var u = encoding.ReadUInt64(stream);

            if (u > byte.MaxValue)
            {
                throw new InvalidDataException("Encoded value exceeds SByte bounds.");
            }
            return((sbyte)((sbyte)(u >> 1) ^ (((sbyte)u & 1) * -1)));
        }
        /// <summary>
        /// Read a <see cref="System.Byte"/> from a <see cref="Stream"/>.
        /// </summary>
        public static byte ReadByte(this IByteEncoding encoding, Stream stream)
        {
            var u = encoding.ReadUInt64(stream);

            if (u > byte.MaxValue)
            {
                throw new InvalidDataException("Encoded value exceeds Byte bounds.");
            }
            return((byte)u);
        }
        /// <summary>
        /// Read a <see cref="System.Int16"/> from a <see cref="Stream"/>.
        /// </summary>
        public static short ReadInt16(this IByteEncoding encoding, Stream stream)
        {
            var u = encoding.ReadUInt64(stream);

            if (u > ushort.MaxValue)
            {
                throw new InvalidDataException("Encoded value exceeds Int16 bounds.");
            }
            return((short)((short)(u >> 1) ^ (((short)u & 1) * -1)));
        }
        /// <summary>
        /// Read a <see cref="System.UInt32"/> from a <see cref="Stream"/>.
        /// </summary>
        public static uint ReadUInt32(this IByteEncoding encoding, Stream stream)
        {
            var u = encoding.ReadUInt64(stream);

            if (u > uint.MaxValue)
            {
                throw new InvalidDataException("Encoded value exceeds UInt32 bounds.");
            }
            return((uint)u);
        }
        /// <summary>
        /// Read a <see cref="System.DateTime"/> from a <see cref="Stream"/>.
        /// </summary>
        public static DateTime ReadDateTime(this IByteEncoding encoding, Stream stream)
        {
            var ticks = encoding.ReadInt64(stream);
            var kind  = (DateTimeKind)encoding.ReadUInt64(stream);

            if (!validDataTimeKinds.Contains(kind))
            {
                throw new InvalidDataException("Encoded value is not valid DateTimeKind.");
            }
            return(new DateTime(ticks, kind));
        }
Beispiel #8
0
        private void EncodingByteTest(IByteEncoding encoding)
        {
            var stream = new MemoryStream();
            var values = new List <Byte>();
            var g      = new Generator(encoding.GetType().Name);

            for (int i = 0; i < 1000; i++)
            {
                var v = (Byte)(g.Byte() >> g.Int32(8));
                values.Add(v);
                encoding.Write(stream, v);
            }

            stream.Position = 0;

            for (int i = 0; i < values.Count; i++)
            {
                var v = encoding.ReadByte(stream);
                Assert.AreEqual(values[i], v);
            }

            try
            {
                using (var r = new RandomStream(g))
                {
                    for (int i = 0; i < 500; i++)
                    {
                        encoding.ReadByte(r);
                    }
                }

                Assert.Fail("Expected InvalidDataException");
            }
            catch (Exception e)
            {
                Assert.AreEqual("InvalidDataException", e.GetType().Name);
            }
        }
        /// <summary>
        /// Read a <see cref="System.Int64"/> from a <see cref="Stream"/>.
        /// </summary>
        public static long ReadInt64(this IByteEncoding encoding, Stream stream)
        {
            var u = encoding.ReadUInt64(stream);

            return((long)(u >> 1) ^ (((long)u & 1) * -1L));
        }
 /// <summary>
 /// Write a <see cref="System.DateTime"/> to a <see cref="Stream"/>.
 /// </summary>
 public static void Write(this IByteEncoding encoding, Stream stream, DateTime value)
 {
     encoding.Write(stream, value.Ticks);
     encoding.Write(stream, (ulong)value.Kind);
 }
 /// <summary>
 /// Write a <see cref="System.Boolean"/> to a <see cref="Stream"/>.
 /// </summary>
 public static void Write(this IByteEncoding encoding, Stream stream, bool value)
 {
     encoding.Write(stream, value ? 1UL : 0UL);
 }
 /// <summary>
 /// Write a <see cref="System.SByte"/> to a <see cref="Stream"/>.
 /// </summary>
 public static void Write(this IByteEncoding encoding, Stream stream, sbyte value)
 {
     encoding.Write(stream, (byte)((value >> 7) ^ (value << 1)));
 }
 /// <summary>
 /// Write a <see cref="System.Int16"/> to a <see cref="Stream"/>.
 /// </summary>
 public static void Write(this IByteEncoding encoding, Stream stream, short value)
 {
     encoding.Write(stream, (ushort)((value >> 15) ^ (value << 1)));
 }
 /// <summary>
 /// Write a <see cref="System.Int32"/> to a <see cref="Stream"/>.
 /// </summary>
 public static void Write(this IByteEncoding encoding, Stream stream, int value)
 {
     encoding.Write(stream, (uint)((value >> 31) ^ (value << 1)));
 }
 /// <summary>
 /// Write a <see cref="System.Int64"/> to a <see cref="Stream"/>.
 /// </summary>
 public static void Write(this IByteEncoding encoding, Stream stream, long value)
 {
     encoding.Write(stream, (ulong)((value >> 63) ^ (value << 1)));
 }