Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public static void WriteUInt32(uint value, MsgPackStream stream)
        {
            if (value <= ushort.MaxValue)
            {
                WriteUInt16(unchecked ((ushort)value), stream);
                return;
            }

            stream.WriteUInt8(FormatCode.UInt32);
            stream.WriteUInt32(value);
        }
Exemplo n.º 3
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);
     }
 }
Exemplo n.º 4
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);
        }