/// <summary> /// Seek past the value for the previously read key. /// </summary> public static void SkipKey(Stream stream, Key key) { switch (key.WireType) { case Wire.Fixed32: stream.Seek(4, SeekOrigin.Current); return; case Wire.Fixed64: stream.Seek(8, SeekOrigin.Current); return; case Wire.LengthDelimited: stream.Seek(ProtocolParser.ReadUInt32(stream), SeekOrigin.Current); return; case Wire.Varint: ProtocolParser.ReadSkipVarInt(stream); return; default: throw new NotImplementedException("Unknown wire type: " + key.WireType); } }
/// <summary> /// Read the value for an unknown key as bytes. /// Used to preserve unknown keys during deserialization. /// Requires the message option preserveunknown=true. /// </summary> public static byte[] ReadValueBytes(Stream stream, Key key) { byte[] b; int offset = 0; switch (key.WireType) { case Wire.Fixed32: b = new byte[4]; while (offset < 4) offset += stream.Read(b, offset, 4 - offset); return b; case Wire.Fixed64: b = new byte[8]; while (offset < 8) offset += stream.Read(b, offset, 8 - offset); return b; case Wire.LengthDelimited: //Read and include length in value buffer uint length = ProtocolParser.ReadUInt32(stream); using (var ms = new MemoryStream()) { //TODO: pass b directly to MemoryStream constructor or skip usage of it completely ProtocolParser.WriteUInt32(ms, length); b = new byte[length + ms.Length]; ms.ToArray().CopyTo(b, 0); offset = (int)ms.Length; } //Read data into buffer while (offset < b.Length) offset += stream.Read(b, offset, b.Length - offset); return b; case Wire.Varint: return ProtocolParser.ReadVarIntBytes(stream); default: throw new NotImplementedException("Unknown wire type: " + key.WireType); } }
public static void WriteKey(Stream stream, Key key) { uint n = (key.Field << 3) | ((uint)key.WireType); WriteUInt32(stream, n); }
public KeyValue(Key key, byte[] value) { this.Key = key; this.Value = value; }
public static void WriteKey(Stream stream, Key key) { uint n = (key.Field << 3) | ((uint)key.WireType); WriteUInt32(stream, n); }
public KeyValue(Key key, byte[] value) { this.Key = key; this.Value = value; }
/// <summary> /// Seek past the value for the previously read key. /// </summary> public static void SkipKey(byte[] buffer, ref int offset, Key key) { switch (key.WireType) { case Wire.Fixed32: offset += 4; return; case Wire.Fixed64: offset += 8; return; case Wire.LengthDelimited: offset += (int)ProtocolParser.ReadUInt32(buffer, ref offset); return; case Wire.Varint: ProtocolParser.ReadSkipVarInt(buffer, ref offset); return; default: throw new NotImplementedException("Unknown wire type: " + key.WireType); } }
/// <summary> /// Read the value for an unknown key as bytes. /// Used to preserve unknown keys during deserialization. /// Requires the message option preserveunknown=true. /// </summary> public static byte[] ReadValueBytes(byte[] buffer, ref int offset, Key key) { byte[] b; switch (key.WireType) { case Wire.Fixed32: b = new byte[4]; Array.Copy(buffer, offset, b, 0, 4); offset += 4; return b; case Wire.Fixed64: b = new byte[8]; Array.Copy(buffer, offset, b, 0, 8); offset += 8; return b; case Wire.LengthDelimited: //Read and include length in value buffer uint length = ProtocolParser.ReadUInt32(buffer, ref offset); using (var ms = new MemoryStream()) { //TODO: pass b directly to MemoryStream constructor or skip usage of it completely ProtocolParser.WriteUInt32(ms, length); b = new byte[length + ms.Length]; ms.ToArray().CopyTo(b, 0); Array.Copy(buffer, offset, b, (int)ms.Length, b.Length - (int)ms.Length); offset += b.Length - (int) ms.Length; return b; } case Wire.Varint: return ProtocolParser.ReadVarIntBytes(buffer, ref offset); default: throw new NotImplementedException("Unknown wire type: " + key.WireType); } }