Пример #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 TestUnsignedMinMax()
        {
            var stream = new MsgPackStream();

            stream.WriteUInt8(byte.MinValue);
            stream.WriteUInt8(byte.MaxValue);
            stream.WriteUInt16(ushort.MinValue);
            stream.WriteUInt16(ushort.MaxValue);
            stream.WriteUInt32(uint.MinValue);
            stream.WriteUInt32(uint.MaxValue);
            stream.WriteUInt64(ulong.MinValue);
            stream.WriteUInt64(ulong.MaxValue);

            stream.Position = 0;
            Assert.AreEqual(byte.MinValue, stream.ReadUInt8());
            Assert.AreEqual(byte.MaxValue, stream.ReadUInt8());
            Assert.AreEqual(ushort.MinValue, stream.ReadUInt16());
            Assert.AreEqual(ushort.MaxValue, stream.ReadUInt16());
            Assert.AreEqual(uint.MinValue, stream.ReadUInt32());
            Assert.AreEqual(uint.MaxValue, stream.ReadUInt32());
            Assert.AreEqual(ulong.MinValue, stream.ReadUInt64());
            Assert.AreEqual(ulong.MaxValue, stream.ReadUInt64());
        }
Пример #4
0
        public static ulong ReadUInt64(MsgPackStream stream)
        {
            var code = stream.ReadUInt8();

            if (FormatMask.Match(code) == FormatMask.PositiveFixInt)
            {
                return(code);
            }

            switch (code)
            {
            case FormatCode.UInt8: return(stream.ReadUInt8());

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

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

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

            default: throw new MsgPackCodeException(code);
            }
        }
Пример #5
0
        public void TestUnsigned()
        {
            var stream = new MsgPackStream();

            stream.WriteUInt8(255);
            Assert.AreEqual(stream.Position, 1);

            stream.WriteUInt16(12431);
            Assert.AreEqual(stream.Position, 3);

            stream.WriteUInt32(761);
            Assert.AreEqual(stream.Position, 7);

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

            stream.Position = 0;
            Assert.AreEqual(255, stream.ReadUInt8());
            Assert.AreEqual(12431, stream.ReadUInt16());
            Assert.AreEqual(761, stream.ReadUInt32());
            Assert.AreEqual(64, stream.ReadUInt64());
        }