コード例 #1
0
        public static void WriteBytes(BufferSegment value, MsgPackStream stream)
        {
            if (value.Array == null)
            {
                stream.WriteUInt8(FormatCode.Nil);
                return;
            }

            var length = value.Length;

            if (length <= byte.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Bin8);
                stream.WriteUInt8(unchecked ((byte)length));
            }
            else if (length <= ushort.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Bin16);
                stream.WriteUInt16(unchecked ((ushort)length));
            }
            else if (length < uint.MaxValue)
            {
                stream.WriteUInt8(FormatCode.Bin32);
                stream.WriteUInt32(unchecked ((uint)length));
            }
            else
            {
                throw new MsgPackException("Max bin length exceeded");
            }

            stream.WriteBytes(value);
        }
コード例 #2
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);
        }
コード例 #3
0
        public static void WriteUInt16(ushort value, MsgPackStream stream)
        {
            if (value <= byte.MaxValue)
            {
                WriteUInt8(unchecked ((byte)value), stream);
                return;
            }

            stream.WriteUInt8(FormatCode.UInt16);
            stream.WriteUInt16(value);
        }
コード例 #4
0
 public static void WriteMapHeader(uint length, MsgPackStream stream)
 {
     if (length <= FormatRange.MaxFixMapCount)
     {
         stream.WriteUInt8(unchecked ((byte)(FormatCode.MinFixMap | length)));
     }
     else if (length <= ushort.MaxValue)
     {
         stream.WriteUInt8(FormatCode.Map16);
         stream.WriteUInt16(unchecked ((ushort)length));
     }
     else
     {
         stream.WriteUInt8(FormatCode.Map32);
         stream.WriteUInt32(length);
     }
 }
コード例 #5
0
        public static void WriteExtensionHeader(ExtensionHeader header, MsgPackStream stream)
        {
            switch (header.Length)
            {
            case 1: stream.WriteUInt8(FormatCode.FixExt1); break;

            case 2: stream.WriteUInt8(FormatCode.FixExt2); break;

            case 4: stream.WriteUInt8(FormatCode.FixExt4); break;

            case 8: stream.WriteUInt8(FormatCode.FixExt8); break;

            case 16: stream.WriteUInt8(FormatCode.FixExt16); break;

            default:
                if (header.Length <= byte.MaxValue)
                {
                    stream.WriteUInt8(FormatCode.Ext8);
                    stream.WriteUInt8(unchecked ((byte)header.Length));
                }
                else if (header.Length <= ushort.MaxValue)
                {
                    stream.WriteUInt8(FormatCode.Ext16);
                    stream.WriteUInt16(unchecked ((ushort)header.Length));
                }
                else
                {
                    stream.WriteUInt8(FormatCode.Ext32);
                    stream.WriteUInt32(header.Length);
                }

                break;
            }

            stream.WriteInt8(header.TypeCode);
        }