示例#1
0
 public void GetVarInt32SizeWorks()
 {
     Assert.Equal(1, VarIntHelper.GetVarInt32Size(0));
     for (var i = 1; i < 5; i++)
     {
         Assert.Equal(i, VarIntHelper.GetVarInt32Size((1U << (7 * i)) - 1));
         Assert.Equal(i + 1, VarIntHelper.GetVarInt32Size(1U << (7 * i)));
     }
 }
示例#2
0
        public void WriteRepeated <T>(IReadOnlyCollection <T> values, RepeatedType <T> type)
        {
            // here we can check by one byte since wire type is mask and written in first byte
            // acually we can quick check by BaseType and don't store first byte, but
            // to support some strange non-packed primitive types...
            if (ProtoRepeatedInfo <T> .IsPackedRepeatedField(_tagFirstByte))
            {
                // packed
                // fixed type?
                var fixedBaseSize = GetFixedBaseTypeSize(type.BaseType);
                if (fixedBaseSize == 0)
                {
                    // variable field..., use same hack as in WriteMessage
                    var current = _size;
                    var write   = type.WriteElement;

                    var spot = MessageSizes.Reserve();
                    foreach (var value in values)
                    {
                        write(this, value);
                    }
                    var size = _size - current;
                    // remember message size, we will need it
                    _messageSizes.Set(spot, size);

                    _size += VarIntHelper.GetVarInt32Size((uint)size);
                }
                else
                {
                    // we can calculate message size fast and easy, no need to cache
                    var size = fixedBaseSize * values.Count;
                    _size += VarIntHelper.GetVarInt32Size((uint)size) + size;
                }
            }
            else
            {
                // don't cache non-packed sizes, they won't be asked
                // all the tag sizes
                _size += _lastTagSize * (values.Count - 1);
                var fixedBaseSize = GetFixedBaseTypeSize(type.BaseType);
                if (fixedBaseSize == 0)
                {
                    var write = type.WriteElement;
                    // write last tag
                    foreach (var value in values)
                    {
                        write(this, value);
                    }
                }
                else
                {
                    _size += fixedBaseSize * values.Count;
                }
            }
        }
示例#3
0
 void IOutputStream.WriteBytes(byte[] value)
 {
     if (value != null && value.Length != 0)
     {
         var size = value.Length;
         _size += VarIntHelper.GetVarInt32Size((uint)size) + size;
     }
     else
     {
         _size++;
     }
 }
示例#4
0
 void IOutputStream.WriteString(string value)
 {
     if (!string.IsNullOrEmpty(value))
     {
         var size = FastBufferWriter.GetUtf8StringSize(value);
         _size += VarIntHelper.GetVarInt32Size((uint)size) + size;
     }
     else
     {
         _size++;
     }
 }
示例#5
0
        void IOutputStream.WriteMessage(IMessage message)
        {
            // we need to get individual message size here
            // to get message length varint size...
            var current = _size;

            var spot = MessageSizes.Reserve();

            // just write it to this "stream"
            message.WriteTo(this);
            // ok, we have size, order doesn't matter ^^
            var size = _size - current;

            // we can event cache it somewhere
            _messageSizes.Set(spot, size);

            // oh, and add length prefix to the "stream"
            _size += VarIntHelper.GetVarInt32Size((uint)size);
        }
示例#6
0
 public void WriteUInt32(uint value)
 {
     _size += VarIntHelper.GetVarInt32Size(value);
 }
示例#7
0
 public void WriteInt32(int value)
 {
     _size += value >= 0 ? VarIntHelper.GetVarInt32Size((uint)value) : 10;
 }
示例#8
0
 void IOutputStream.WriteEnum(int value)
 {
     _size += value >= 0 ? VarIntHelper.GetVarInt32Size((uint)value) : 10;
 }
示例#9
0
 public void WriteSInt32(int value)
 {
     _size += VarIntHelper.GetVarInt32Size(ProtoOutputStream.EncodeZigZag32(value));
 }