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); } }
public static ExtensionHeader ReadExtensionHeader(MsgPackStream stream) { var code = stream.ReadUInt8(); uint length; switch (code) { case FormatCode.FixExt1: length = 1u; break; case FormatCode.FixExt2: length = 2u; break; case FormatCode.FixExt4: length = 4u; break; case FormatCode.FixExt8: length = 8u; break; case FormatCode.FixExt16: length = 16u; break; case FormatCode.Ext8: length = stream.ReadUInt8(); break; case FormatCode.Ext16: length = stream.ReadUInt16(); break; case FormatCode.Ext32: length = stream.ReadUInt32(); break; default: throw new MsgPackCodeException(code); } return(new ExtensionHeader(stream.ReadInt8(), length)); }
public static byte ReadUInt8(MsgPackStream stream) { var code = stream.ReadUInt8(); if (FormatMask.Match(code) == FormatMask.PositiveFixInt) { return(code); } switch (code) { case FormatCode.UInt8: return(stream.ReadUInt8()); default: throw new MsgPackCodeException(code); } }
public static BufferSegment ReadBytes(MsgPackStream stream) { var code = stream.ReadUInt8(); switch (code) { case FormatCode.Bin8: return(stream.ReadBytes(stream.ReadUInt8())); case FormatCode.Bin16: return(stream.ReadBytes(stream.ReadUInt16())); case FormatCode.Bin32: return(stream.ReadBytes(stream.ReadUInt32())); case FormatCode.Nil: return(new BufferSegment()); default: throw new MsgPackCodeException(code); } }
public static sbyte ReadInt8(MsgPackStream stream) { var code = stream.ReadUInt8(); switch (FormatMask.Match(code)) { case FormatMask.PositiveFixInt: case FormatMask.NegativeFixInt: return(unchecked ((sbyte)code)); } switch (code) { case FormatCode.Int8: return(stream.ReadInt8()); case FormatCode.UInt8: return(checked ((sbyte)stream.ReadUInt8())); default: throw new MsgPackCodeException(code); } }
public static BufferSegment ReadUtf8(MsgPackStream stream) { var code = stream.ReadUInt8(); if (FormatMask.Match(code) == FormatMask.FixString) { return(stream.ReadBytes(code & 0x1Fu)); } switch (code) { case FormatCode.Str8: return(stream.ReadBytes(stream.ReadUInt8())); case FormatCode.Str16: return(stream.ReadBytes(stream.ReadUInt16())); case FormatCode.Str32: return(stream.ReadBytes(stream.ReadUInt32())); case FormatCode.Nil: return(new BufferSegment()); default: throw new MsgPackCodeException(code); } }
public static bool ReadBool(MsgPackStream stream) { var code = stream.ReadUInt8(); switch (code) { case FormatCode.False: return(false); case FormatCode.True: return(true); default: throw new MsgPackCodeException(code); } }
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()); }
public static uint ReadMapHeader(MsgPackStream stream) { var code = stream.ReadUInt8(); if (FormatMask.Match(code) == FormatMask.FixMap) { return(code & 0xfu); } switch (code) { case FormatCode.Map16: return(stream.ReadUInt16()); case FormatCode.Map32: return(stream.ReadUInt32()); default: throw new MsgPackCodeException(code); } }
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()); }
public static void Skip(MsgPackStream stream) { var code = stream.ReadUInt8(); switch (FormatMask.Match(code)) { case FormatMask.PositiveFixInt: return; case FormatMask.NegativeFixInt: return; case FormatMask.FixString: stream.Skip(code & 0x1Fu); return; case FormatMask.FixArray: SkipElements(stream, code & 0xFu); return; case FormatMask.FixMap: SkipElements(stream, 2u * (code & 0xFu)); return; } switch (code) { case FormatCode.Nil: return; case FormatCode.False: return; case FormatCode.True: return; case FormatCode.Int8: stream.Skip(1u); break; case FormatCode.UInt8: stream.Skip(1u); break; case FormatCode.Int16: stream.Skip(2u); break; case FormatCode.UInt16: stream.Skip(2u); break; case FormatCode.Int32: stream.Skip(4u); break; case FormatCode.UInt32: stream.Skip(4u); break; case FormatCode.Int64: stream.Skip(8u); break; case FormatCode.UInt64: stream.Skip(8u); break; case FormatCode.Float32: stream.Skip(4u); break; case FormatCode.Float64: stream.Skip(8u); break; case FormatCode.Str8: stream.Skip(stream.ReadUInt8()); break; case FormatCode.Str16: stream.Skip(stream.ReadUInt16()); break; case FormatCode.Str32: stream.Skip(stream.ReadUInt32()); break; case FormatCode.Bin8: stream.Skip(stream.ReadUInt8()); break; case FormatCode.Bin16: stream.Skip(stream.ReadUInt16()); break; case FormatCode.Bin32: stream.Skip(stream.ReadUInt32()); break; case FormatCode.Array16: SkipElements(stream, stream.ReadUInt16()); break; case FormatCode.Array32: SkipElements(stream, stream.ReadUInt32()); break; case FormatCode.Map16: SkipElements(stream, 2u * stream.ReadUInt16()); break; case FormatCode.Map32: SkipElements(stream, 2u * stream.ReadUInt32()); break; case FormatCode.FixExt1: stream.Skip(2u); break; case FormatCode.FixExt2: stream.Skip(3u); break; case FormatCode.FixExt4: stream.Skip(5u); break; case FormatCode.FixExt8: stream.Skip(9u); break; case FormatCode.FixExt16: stream.Skip(17u); break; case FormatCode.Ext8: stream.Skip(1u + stream.ReadUInt8()); break; case FormatCode.Ext16: stream.Skip(1u + stream.ReadUInt16()); break; case FormatCode.Ext32: stream.Skip(1u + stream.ReadUInt32()); break; default: throw new MsgPackCodeException(code); } }