本文整理汇总了C#中Google.Protobuf.CodedInputStream类的典型用法代码示例。如果您正苦于以下问题:C# CodedInputStream类的具体用法?C# CodedInputStream怎么用?C# CodedInputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodedInputStream类属于Google.Protobuf命名空间,在下文中一共展示了CodedInputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AssertReadVarint
/// <summary>
/// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64()
/// </summary>
private static void AssertReadVarint(byte[] data, ulong value)
{
CodedInputStream input = new CodedInputStream(data);
Assert.AreEqual((uint) value, input.ReadRawVarint32());
input = new CodedInputStream(data);
Assert.AreEqual(value, input.ReadRawVarint64());
Assert.IsTrue(input.IsAtEnd);
// Try different block sizes.
for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
{
input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize));
Assert.AreEqual((uint) value, input.ReadRawVarint32());
input = new CodedInputStream(new SmallBlockInputStream(data, bufferSize));
Assert.AreEqual(value, input.ReadRawVarint64());
Assert.IsTrue(input.IsAtEnd);
}
// Try reading directly from a MemoryStream. We want to verify that it
// doesn't read past the end of the input, so write an extra byte - this
// lets us test the position at the end.
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(data, 0, data.Length);
memoryStream.WriteByte(0);
memoryStream.Position = 0;
Assert.AreEqual((uint) value, CodedInputStream.ReadRawVarint32(memoryStream));
Assert.AreEqual(data.Length, memoryStream.Position);
}
示例2: MergeFrom
/// <summary>
/// Merges data from the given stream into an existing message.
/// </summary>
/// <param name="message">The message to merge the data into.</param>
/// <param name="input">Stream containing the data to merge, which must be protobuf-encoded binary data.</param>
public static void MergeFrom(this IMessage message, Stream input)
{
ProtoPreconditions.CheckNotNull(message, "message");
ProtoPreconditions.CheckNotNull(input, "input");
CodedInputStream codedInput = new CodedInputStream(input);
message.MergeFrom(codedInput);
codedInput.CheckReadEndOfStreamTag();
}
示例3: MergeFrom
/// <summary>
/// Merges data from the given stream into an existing message.
/// </summary>
/// <param name="message">The message to merge the data into.</param>
/// <param name="input">Stream containing the data to merge, which must be protobuf-encoded binary data.</param>
public static void MergeFrom(this IMessage message, Stream input)
{
Preconditions.CheckNotNull(message, "message");
Preconditions.CheckNotNull(input, "input");
CodedInputStream codedInput = new CodedInputStream(input);
message.MergeFrom(codedInput);
codedInput.CheckLastTagWas(0);
}
示例4: ByteStringToValueType
public void ByteStringToValueType ()
{
// From Google's protobuf documentation, varint encoding example:
// 300 = 1010 1100 0000 0010 = 0xAC 0x02
byte[] bytes = { 0xAC, 0x02 };
var codedStream = new CodedInputStream (bytes);
uint value = codedStream.ReadUInt32 ();
Assert.AreEqual (300, value);
}
示例5: Decode
/// <summary>
/// Decode a value of the given type.
/// Should not be called directly. This interface is used by service client stubs.
/// </summary>
public static object Decode(ByteString value, Type type, Connection client)
{
var stream = new CodedInputStream (value.ToByteArray ());
if (type == typeof(double))
return stream.ReadDouble ();
else if (type == typeof(float))
return stream.ReadFloat ();
else if (type == typeof(int))
return stream.ReadInt32 ();
else if (type == typeof(long))
return stream.ReadInt64 ();
else if (type == typeof(uint))
return stream.ReadUInt32 ();
else if (type == typeof(ulong))
return stream.ReadUInt64 ();
else if (type == typeof(bool))
return stream.ReadBool ();
else if (type == typeof(string))
return stream.ReadString ();
else if (type == typeof(byte[]))
return stream.ReadBytes ().ToByteArray ();
else if (type.IsEnum)
return stream.ReadInt32 ();
else if (typeof(RemoteObject).IsAssignableFrom (type)) {
if (client == null)
throw new ArgumentException ("Client not passed when decoding remote object");
var id = stream.ReadUInt64 ();
if (id == 0)
return null;
return (RemoteObject)Activator.CreateInstance (type, client, id);
} else if (typeof(IMessage).IsAssignableFrom (type)) {
IMessage message = (IMessage)Activator.CreateInstance (type);
message.MergeFrom (stream);
return message;
} else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof(IList<>))
return DecodeList (value, type, client);
else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof(IDictionary<,>))
return DecodeDictionary (value, type, client);
else if (type.IsGenericType && type.GetGenericTypeDefinition () == typeof(ISet<>))
return DecodeSet (value, type, client);
else if (type.IsGenericType &&
(type.GetGenericTypeDefinition () == typeof(Tuple<>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,,>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,,,>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,,,,>) ||
type.GetGenericTypeDefinition () == typeof(Tuple<,,,,,>)))
return DecodeTuple (value, type, client); // TODO: ugly handing of tuple types
throw new ArgumentException (type + " is not a serializable type");
}
示例6: Read
protected override int Read (ref Request request, byte[] data, int offset, int length)
{
try {
var codedStream = new CodedInputStream (data, offset, length);
// Get the protobuf message size
int size = (int)codedStream.ReadUInt32 ();
int totalSize = (int)codedStream.Position + size;
// Check if enough data is available, if not then delay the decoding
if (length < totalSize)
return 0;
// Decode the request
request = Schema.KRPC.Request.Parser.ParseFrom (codedStream).ToMessage ();
return totalSize;
} catch (InvalidProtocolBufferException e) {
throw new MalformedRequestException (e.Message);
}
}
示例7: ReadMaliciouslyLargeBlob
public void ReadMaliciouslyLargeBlob()
{
MemoryStream ms = new MemoryStream();
CodedOutputStream output = new CodedOutputStream(ms);
uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
output.WriteRawVarint32(tag);
output.WriteRawVarint32(0x7FFFFFFF);
output.WriteRawBytes(new byte[32]); // Pad with a few random bytes.
output.Flush();
ms.Position = 0;
CodedInputStream input = new CodedInputStream(ms);
Assert.AreEqual(tag, input.ReadTag());
Assert.Throws<InvalidProtocolBufferException>(() => input.ReadBytes());
}
示例8: ReadInvalidUtf8
public void ReadInvalidUtf8()
{
MemoryStream ms = new MemoryStream();
CodedOutputStream output = new CodedOutputStream(ms);
uint tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited);
output.WriteRawVarint32(tag);
output.WriteRawVarint32(1);
output.WriteRawBytes(new byte[] {0x80});
output.Flush();
ms.Position = 0;
CodedInputStream input = new CodedInputStream(ms);
Assert.AreEqual(tag, input.ReadTag());
string text = input.ReadString();
Assert.AreEqual('\ufffd', text[0]);
}
示例9: ReadValue
public override void ReadValue(CodedInputStream stream)
{
}
示例10: Main
internal void Main()
{
client = new TcpClient ();
client.Connect (address, port);
stream = client.GetStream ();
stream.Write (Encoder.streamHelloMessage, 0, Encoder.streamHelloMessage.Length);
stream.Write (clientIdentifier, 0, clientIdentifier.Length);
var recvOkMessage = new byte [Encoder.okMessage.Length];
stream.Read (recvOkMessage, 0, Encoder.okMessage.Length);
if (recvOkMessage.Equals (Encoder.okMessage))
throw new Exception ("Invalid hello message received from stream server. " +
"Got " + Encoder.ToHexString (recvOkMessage));
this.codedStream = new CodedInputStream (stream);
try {
while (true) {
var message = new StreamMessage ();
codedStream.ReadMessage (message);
foreach (var response in message.Responses)
manager.Update (response.Id, response.Response);
}
} catch (IOException) {
// Exit when the connection closes
}
}
示例11: Dispose_DisposesUnderlyingStream
public void Dispose_DisposesUnderlyingStream()
{
var memoryStream = new MemoryStream();
Assert.IsTrue(memoryStream.CanRead);
using (var cis = new CodedInputStream(memoryStream))
{
}
Assert.IsFalse(memoryStream.CanRead); // Disposed
}
示例12: AssertReadVarintFailure
/// <summary>
/// Parses the given bytes using ReadRawVarint32() and ReadRawVarint64() and
/// expects them to fail with an InvalidProtocolBufferException whose
/// description matches the given one.
/// </summary>
private static void AssertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data)
{
CodedInputStream input = new CodedInputStream(data);
var exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint32());
Assert.AreEqual(expected.Message, exception.Message);
input = new CodedInputStream(data);
exception = Assert.Throws<InvalidProtocolBufferException>(() => input.ReadRawVarint64());
Assert.AreEqual(expected.Message, exception.Message);
// Make sure we get the same error when reading directly from a Stream.
exception = Assert.Throws<InvalidProtocolBufferException>(() => CodedInputStream.ReadRawVarint32(new MemoryStream(data)));
Assert.AreEqual(expected.Message, exception.Message);
}
示例13: TestSlowPathAvoidance
public void TestSlowPathAvoidance()
{
using (var ms = new MemoryStream())
{
CodedOutputStream output = new CodedOutputStream(ms);
output.WriteTag(1, WireFormat.WireType.LengthDelimited);
output.WriteBytes(ByteString.CopyFrom(new byte[100]));
output.WriteTag(2, WireFormat.WireType.LengthDelimited);
output.WriteBytes(ByteString.CopyFrom(new byte[100]));
output.Flush();
ms.Position = 0;
CodedInputStream input = new CodedInputStream(ms, new byte[ms.Length / 2], 0, 0);
uint tag = input.ReadTag();
Assert.AreEqual(1, WireFormat.GetTagFieldNumber(tag));
Assert.AreEqual(100, input.ReadBytes().Length);
tag = input.ReadTag();
Assert.AreEqual(2, WireFormat.GetTagFieldNumber(tag));
Assert.AreEqual(100, input.ReadBytes().Length);
}
}
示例14: Tag0Throws
public void Tag0Throws()
{
var input = new CodedInputStream(new byte[] { 0 });
Assert.Throws<InvalidProtocolBufferException>(() => input.ReadTag());
}
示例15: SkipGroup
public void SkipGroup()
{
// Create an output stream with a group in:
// Field 1: string "field 1"
// Field 2: group containing:
// Field 1: fixed int32 value 100
// Field 2: string "ignore me"
// Field 3: nested group containing
// Field 1: fixed int64 value 1000
// Field 3: string "field 3"
var stream = new MemoryStream();
var output = new CodedOutputStream(stream);
output.WriteTag(1, WireFormat.WireType.LengthDelimited);
output.WriteString("field 1");
// The outer group...
output.WriteTag(2, WireFormat.WireType.StartGroup);
output.WriteTag(1, WireFormat.WireType.Fixed32);
output.WriteFixed32(100);
output.WriteTag(2, WireFormat.WireType.LengthDelimited);
output.WriteString("ignore me");
// The nested group...
output.WriteTag(3, WireFormat.WireType.StartGroup);
output.WriteTag(1, WireFormat.WireType.Fixed64);
output.WriteFixed64(1000);
// Note: Not sure the field number is relevant for end group...
output.WriteTag(3, WireFormat.WireType.EndGroup);
// End the outer group
output.WriteTag(2, WireFormat.WireType.EndGroup);
output.WriteTag(3, WireFormat.WireType.LengthDelimited);
output.WriteString("field 3");
output.Flush();
stream.Position = 0;
// Now act like a generated client
var input = new CodedInputStream(stream);
Assert.AreEqual(WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited), input.ReadTag());
Assert.AreEqual("field 1", input.ReadString());
Assert.AreEqual(WireFormat.MakeTag(2, WireFormat.WireType.StartGroup), input.ReadTag());
input.SkipLastField(); // Should consume the whole group, including the nested one.
Assert.AreEqual(WireFormat.MakeTag(3, WireFormat.WireType.LengthDelimited), input.ReadTag());
Assert.AreEqual("field 3", input.ReadString());
}