Пример #1
0
        public static float ReadSingle(MsgPackStream stream)
        {
            var code = stream.ReadUInt8();

            switch (FormatMask.Match(code))
            {
            case FormatMask.PositiveFixInt: return(code);

            case FormatMask.NegativeFixInt: return(unchecked ((sbyte)code));
            }

            switch (code)
            {
            case FormatCode.Float32: return(stream.ReadSingle());

            case FormatCode.Int8: return(stream.ReadInt8());

            case FormatCode.UInt8: return(stream.ReadUInt8());

            case FormatCode.Int16: return(stream.ReadInt16());

            case FormatCode.UInt16: return(stream.ReadUInt16());

            case FormatCode.Int32: return(stream.ReadInt32());

            case FormatCode.UInt32: return(stream.ReadUInt32());

            case FormatCode.Int64: return(stream.ReadInt64());

            case FormatCode.UInt64: return(stream.ReadUInt64());

            default: throw new MsgPackCodeException(code);
            }
        }
Пример #2
0
        internal static DateTime Unpack(MsgPackStream stream, uint length)
        {
            long seconds, nanoseconds;

            switch (length)
            {
            case 4:
                nanoseconds = 0;
                seconds     = stream.ReadUInt32();
                break;

            case 8:
                var data64 = stream.ReadUInt64();
                nanoseconds = (long)(data64 >> 34);
                seconds     = (long)(data64 & 0x00000003fffffffful);
                break;

            case 12:
                nanoseconds = stream.ReadUInt32();
                seconds     = stream.ReadInt64();
                break;

            default: throw new MsgPackException("Invalid timestemp size {0}", length);
            }

            return(UnixEpoch.AddSeconds(seconds).AddTicks(nanoseconds / NanosecondsPerTick));
        }
Пример #3
0
        public void TestSignedMinMax()
        {
            var stream = new MsgPackStream();

            stream.WriteInt8(sbyte.MinValue);
            stream.WriteInt8(sbyte.MaxValue);
            stream.WriteInt16(short.MinValue);
            stream.WriteInt16(short.MaxValue);
            stream.WriteInt32(int.MinValue);
            stream.WriteInt32(int.MaxValue);
            stream.WriteInt64(long.MinValue);
            stream.WriteInt64(long.MaxValue);

            stream.Position = 0;
            Assert.AreEqual(sbyte.MinValue, stream.ReadInt8());
            Assert.AreEqual(sbyte.MaxValue, stream.ReadInt8());
            Assert.AreEqual(short.MinValue, stream.ReadInt16());
            Assert.AreEqual(short.MaxValue, stream.ReadInt16());
            Assert.AreEqual(int.MinValue, stream.ReadInt32());
            Assert.AreEqual(int.MaxValue, stream.ReadInt32());
            Assert.AreEqual(long.MinValue, stream.ReadInt64());
            Assert.AreEqual(long.MaxValue, stream.ReadInt64());
        }
Пример #4
0
        public void TestSigned()
        {
            var stream = new MsgPackStream();

            stream.WriteInt8(-11);
            Assert.AreEqual(stream.Position, 1);

            stream.WriteInt16(192);
            Assert.AreEqual(stream.Position, 3);

            stream.WriteInt32(-213);
            Assert.AreEqual(stream.Position, 7);

            stream.WriteInt64(64);
            Assert.AreEqual(stream.Position, 15);

            stream.Position = 0;
            Assert.AreEqual(-11, stream.ReadInt8());
            Assert.AreEqual(192, stream.ReadInt16());
            Assert.AreEqual(-213, stream.ReadInt32());
            Assert.AreEqual(64, stream.ReadInt64());
        }