Ejemplo n.º 1
0
        public static void WriteString(string value, MsgPackStream stream)
        {
            if (value == null)
            {
                stream.WriteUInt8(FormatCode.Nil);
                return;
            }

            var length = Encoding.UTF8.GetByteCount(value);

            if (length <= FormatRange.MaxFixStringLength)
            {
                stream.WriteUInt8(unchecked ((byte)(FormatCode.MinFixStr | length)));
            }
            else if (length <= byte.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Str8);
                stream.WriteUInt8(unchecked ((byte)length));
            }
            else if (length <= ushort.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Str16);
                stream.WriteUInt16(unchecked ((ushort)length));
            }
            else
            {
                stream.WriteUInt8(FormatCode.Str32);
                stream.WriteInt32(length);
            }

            stream.WriteString(value);
        }
Ejemplo n.º 2
0
        public static void WriteInt32(int value, MsgPackStream stream)
        {
            if (value >= 0)
            {
                WriteUInt32(unchecked ((uint)value), stream);
                return;
            }

            if (value >= short.MinValue)
            {
                WriteInt16(unchecked ((short)value), stream);
                return;
            }

            stream.WriteUInt8(FormatCode.Int32);
            stream.WriteInt32(value);
        }