public void GetVarInt64SizeWorks() { Assert.Equal(1, VarIntHelper.GetVarInt64Size(0)); for (var i = 1; i < 10; i++) { Assert.Equal(i, VarIntHelper.GetVarInt64Size((1UL << (7 * i)) - 1)); Assert.Equal(i + 1, VarIntHelper.GetVarInt64Size(1UL << (7 * i))); } }
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; } } }
void IOutputStream.WriteBytes(byte[] value) { if (value != null && value.Length != 0) { var size = value.Length; _size += VarIntHelper.GetVarInt32Size((uint)size) + size; } else { _size++; } }
void IOutputStream.WriteString(string value) { if (!string.IsNullOrEmpty(value)) { var size = FastBufferWriter.GetUtf8StringSize(value); _size += VarIntHelper.GetVarInt32Size((uint)size) + size; } else { _size++; } }
public void SkipVarIntOk() { var buf = Buf(VarIntHelper.GetVarInt32Bytes(889863845U)); buf.SkipVarInt(); Assert.True(buf.End); buf = Buf(VarIntHelper.GetVarInt64Bytes(ulong.MaxValue)); buf.SkipVarInt(); Assert.True(buf.End); Assert.Throws <InvalidDataException>(() => buf.SkipVarInt()); buf = Buf(VarIntHelper.GetVarInt64Bytes(ulong.MaxValue).TakeWhile(x => x > 128).ToArray()); Assert.Throws <InvalidDataException>(() => buf.SkipVarInt()); buf = Buf(Enumerable.Range(0, 20).Select(x => (byte)128).ToArray()); Assert.Throws <InvalidDataException>(() => buf.SkipVarInt()); }
public void GetVarInt64BytesSeemsOk() { var b0 = VarIntHelper.GetVarInt64Bytes(0U); Assert.Equal(1, b0.Length); Assert.Equal(0, b0[0]); for (var i = 1; i < 10; i++) { var breakingPoint = VarIntHelper.GetVarInt64Bytes((1UL << (7 * i)) - 1); Assert.Equal(i, breakingPoint.Length); Assert.True(breakingPoint.Take(i - 1).All(x => x == 255)); Assert.Equal(127, breakingPoint.Last()); var next = VarIntHelper.GetVarInt64Bytes(1UL << (7 * i)); Assert.Equal(i + 1, next.Length); Assert.True(next.Take(i).All(x => x == 128)); Assert.Equal(1, next.Last()); } }
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); }
public void WriteUInt64(ulong value) { _size += VarIntHelper.GetVarInt64Size(value); }
public void WriteUInt32(uint value) { _size += VarIntHelper.GetVarInt32Size(value); }
public void WriteInt64(long value) { _size += VarIntHelper.GetVarInt64Size((ulong)value); }
public void WriteInt32(int value) { _size += value >= 0 ? VarIntHelper.GetVarInt32Size((uint)value) : 10; }
void IOutputStream.WriteEnum(int value) { _size += value >= 0 ? VarIntHelper.GetVarInt32Size((uint)value) : 10; }
public void WriteSInt64(long value) { _size += VarIntHelper.GetVarInt64Size(ProtoOutputStream.EncodeZigZag64(value)); }
public void WriteSInt32(int value) { _size += VarIntHelper.GetVarInt32Size(ProtoOutputStream.EncodeZigZag32(value)); }