public static byte[] ReadValueBytes(Stream stream, Key key) { int offset = 0; switch (key.WireType) { case Wire.Varint: return(ProtocolParser.ReadVarIntBytes(stream)); case Wire.Fixed64: byte[] buffer1 = new byte[8]; while (offset < 8) { offset += stream.Read(buffer1, offset, 8 - offset); } return(buffer1); case Wire.LengthDelimited: uint val = ProtocolParser.ReadUInt32(stream); byte[] buffer2; using (MemoryStream memoryStream = new MemoryStream()) { ProtocolParser.WriteUInt32((Stream)memoryStream, val); buffer2 = new byte[(long)val + memoryStream.Length]; memoryStream.ToArray().CopyTo((Array)buffer2, 0); offset = (int)memoryStream.Length; } while (offset < buffer2.Length) { offset += stream.Read(buffer2, offset, buffer2.Length - offset); } return(buffer2); case Wire.Fixed32: byte[] buffer3 = new byte[4]; while (offset < 4) { offset += stream.Read(buffer3, offset, 4 - offset); } return(buffer3); default: throw new NotImplementedException("Unknown wire type: " + (object)key.WireType); } }
/// <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); } }
public static void WriteKey(Stream stream, Key key) { uint val = (uint)((Wire)((int)key.Field << 3) | key.WireType); ProtocolParser.WriteUInt32(stream, val); }
public static Key ReadKey(byte firstByte, Stream stream) { return(firstByte < (byte)128 ? new Key((uint)firstByte >> 3, (Wire)((int)firstByte & 7)) : new Key((uint)((int)ProtocolParser.ReadUInt32(stream) << 4 | (int)firstByte >> 3 & 15), (Wire)((int)firstByte & 7))); }
public static void Write(this Stream stream, float val) { ProtocolParser.WriteFloat(stream, val); }
public static void WriteBytes(Stream stream, byte[] val) { ProtocolParser.WriteUInt32(stream, (uint)val.Length); stream.Write(val, 0, val.Length); }
public static void WriteZInt64(Stream stream, long val) { ProtocolParser.WriteUInt64(stream, (ulong)(val << 1 ^ val >> 63)); }
public static void WriteInt64(Stream stream, int val) { ProtocolParser.WriteUInt64(stream, (ulong)val); }
public static void WriteZInt32(Stream stream, int val) { ProtocolParser.WriteUInt32(stream, (uint)(val << 1 ^ val >> 31)); }
public static double ReadDouble(this Stream stream) { return(ProtocolParser.ReadDouble(stream)); }
public static float ReadFloat(this Stream stream) { return(ProtocolParser.ReadFloat(stream)); }
public static int ReadInt(this Stream stream) { return(ProtocolParser.ReadInt32(stream)); }
public static void Write(this Stream stream, byte[] bytes) { ProtocolParser.WriteBytes(stream, bytes); }
public static void Write(this Stream stream, string val) { ProtocolParser.WriteString(stream, val); }
public static void Write(this Stream stream, bool val) { ProtocolParser.WriteBool(stream, val); }
public static void Write(this Stream stream, double val) { ProtocolParser.WriteDouble(stream, val); }
public static int ReadZInt32(Stream stream) { uint num = ProtocolParser.ReadUInt32(stream); return((int)(num >> 1) ^ (int)num << 31 >> 31); }
public static bool ReadBool(this Stream stream) { return(ProtocolParser.ReadBool(stream)); }
public static int ReadInt64(Stream stream) { return((int)ProtocolParser.ReadUInt64(stream)); }
public static string ReadString(this Stream stream) { return(ProtocolParser.ReadString(stream)); }
public static long ReadZInt64(Stream stream) { ulong num = ProtocolParser.ReadUInt64(stream); return((long)(num >> 1) ^ (long)num << 63 >> 63); }
public static byte[] ReadBytes(this Stream stream) { return(ProtocolParser.ReadBytes(stream)); }
public static void WriteString(Stream stream, string val) { ProtocolParser.WriteBytes(stream, Encoding.UTF8.GetBytes(val)); }
public static string ReadString(Stream stream) { byte[] bytes = ProtocolParser.ReadBytes(stream); return(Encoding.UTF8.GetString(bytes, 0, bytes.Length)); }
public static Key ReadKey(Stream stream) { uint num = ProtocolParser.ReadUInt32(stream); return(new Key(num >> 3, (Wire)((int)num & 7))); }
public static void Write(this Stream stream, int val) { ProtocolParser.WriteInt32(stream, val); }