本文整理汇总了C#中Google.Protobuf.CodedOutputStream类的典型用法代码示例。如果您正苦于以下问题:C# CodedOutputStream类的具体用法?C# CodedOutputStream怎么用?C# CodedOutputStream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodedOutputStream类属于Google.Protobuf命名空间,在下文中一共展示了CodedOutputStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Encode
/// <summary>
/// Encode an object of the given type using the protocol buffer encoding scheme.
/// Should not be called directly. This interface is used by service client stubs.
/// </summary>
public static ByteString Encode (object value, Type type)
{
using (var buffer = new MemoryStream ()) {
var stream = new CodedOutputStream (buffer, true);
return EncodeObject (value, type, buffer, stream);
}
}
示例2: EncodeObject
static ByteString EncodeObject (object value, Type type, MemoryStream buffer, CodedOutputStream stream)
{
buffer.SetLength (0);
if (value != null && !type.IsInstanceOfType (value))
throw new ArgumentException ("Value of type " + value.GetType () + " cannot be encoded to type " + type);
if (value == null && !type.IsSubclassOf (typeof(RemoteObject)) && !IsACollectionType (type))
throw new ArgumentException ("null cannot be encoded to type " + type);
if (value == null)
stream.WriteUInt64 (0);
else if (value is Enum)
stream.WriteInt32 ((int)value);
else {
switch (Type.GetTypeCode (type)) {
case TypeCode.Int32:
stream.WriteInt32 ((int)value);
break;
case TypeCode.Int64:
stream.WriteInt64 ((long)value);
break;
case TypeCode.UInt32:
stream.WriteUInt32 ((uint)value);
break;
case TypeCode.UInt64:
stream.WriteUInt64 ((ulong)value);
break;
case TypeCode.Single:
stream.WriteFloat ((float)value);
break;
case TypeCode.Double:
stream.WriteDouble ((double)value);
break;
case TypeCode.Boolean:
stream.WriteBool ((bool)value);
break;
case TypeCode.String:
stream.WriteString ((string)value);
break;
default:
if (type.Equals (typeof(byte[])))
stream.WriteBytes (ByteString.CopyFrom ((byte[])value));
else if (IsAClassType (type))
stream.WriteUInt64 (((RemoteObject)value).id);
else if (IsAMessageType (type))
((IMessage)value).WriteTo (buffer);
else if (IsAListType (type))
WriteList (value, type, buffer);
else if (IsADictionaryType (type))
WriteDictionary (value, type, buffer);
else if (IsASetType (type))
WriteSet (value, type, buffer);
else if (IsATupleType (type))
WriteTuple (value, type, buffer);
else
throw new ArgumentException (type + " is not a serializable type");
break;
}
}
stream.Flush ();
return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length);
}
示例3: ProtoToByteArray
public static byte[] ProtoToByteArray(IMessage message)
{
int size = message.CalculateSize();
byte[] buffer = new byte[size];
CodedOutputStream output = new CodedOutputStream(buffer);
message.WriteTo(output);
return buffer;
}
示例4: EncodeObject
static ByteString EncodeObject (object value, MemoryStream buffer, CodedOutputStream stream)
{
buffer.SetLength (0);
if (value == null) {
stream.WriteUInt64 (0);
} else if (value is Enum) {
stream.WriteInt32 ((int)value);
} else {
Type type = value.GetType ();
switch (Type.GetTypeCode (type)) {
case TypeCode.Int32:
stream.WriteInt32 ((int)value);
break;
case TypeCode.Int64:
stream.WriteInt64 ((long)value);
break;
case TypeCode.UInt32:
stream.WriteUInt32 ((uint)value);
break;
case TypeCode.UInt64:
stream.WriteUInt64 ((ulong)value);
break;
case TypeCode.Single:
stream.WriteFloat ((float)value);
break;
case TypeCode.Double:
stream.WriteDouble ((double)value);
break;
case TypeCode.Boolean:
stream.WriteBool ((bool)value);
break;
case TypeCode.String:
stream.WriteString ((string)value);
break;
default:
if (type == typeof(byte[]))
stream.WriteBytes (ByteString.CopyFrom ((byte[])value));
else if (TypeUtils.IsAClassType (type))
stream.WriteUInt64 (ObjectStore.Instance.AddInstance (value));
else if (TypeUtils.IsAMessageType (type))
WriteMessage (value, stream);
else if (TypeUtils.IsAListCollectionType (type))
WriteList (value, stream);
else if (TypeUtils.IsADictionaryCollectionType (type))
WriteDictionary (value, stream);
else if (TypeUtils.IsASetCollectionType (type))
WriteSet (value, stream);
else if (TypeUtils.IsATupleCollectionType (type))
WriteTuple (value, stream);
else
throw new ArgumentException (type + " is not a serializable type");
break;
}
}
stream.Flush ();
return ByteString.CopyFrom (buffer.GetBuffer (), 0, (int)buffer.Length);
}
示例5: ToByteArray
public static byte[] ToByteArray(this IMessage message)
{
Preconditions.CheckNotNull(message, "message");
byte[] result = new byte[message.CalculateSize()];
CodedOutputStream output = new CodedOutputStream(result);
message.WriteTo(output);
output.CheckNoSpaceLeft();
return result;
}
示例6: ValueTypeToByteString
public void ValueTypeToByteString ()
{
// From Google's protobuf documentation, varint encoding example:
// 300 = 1010 1100 0000 0010 = 0xAC 0x02
const int value = 300;
var buffer = new byte [2];
var codedStream = new CodedOutputStream (buffer);
codedStream.WriteUInt32 (value);
Assert.AreEqual ("ac02", buffer.ToHexString ());
}
示例7: ValueTypeToByteString
public void ValueTypeToByteString()
{
// From Google's protobuf documentation, varint encoding example:
// 300 = 1010 1100 0000 0010 = 0xAC 0x02
const int value = 300;
var buffer = new byte [2];
var codedStream = new CodedOutputStream (buffer);
codedStream.WriteUInt32 (value);
string hex = ("0x" + BitConverter.ToString (buffer)).Replace ("-", " 0x");
Assert.AreEqual ("0xAC 0x02", hex);
}
示例8: Dispose_DisposesUnderlyingStream
public void Dispose_DisposesUnderlyingStream()
{
var memoryStream = new MemoryStream();
Assert.IsTrue(memoryStream.CanWrite);
using (var cos = new CodedOutputStream(memoryStream))
{
cos.WriteRawByte(0);
Assert.AreEqual(0, memoryStream.Position); // Not flushed yet
}
Assert.AreEqual(1, memoryStream.ToArray().Length); // Flushed data from CodedOutputStream to MemoryStream
Assert.IsFalse(memoryStream.CanWrite); // Disposed
}
示例9: Dispose_WithLeaveOpen
public void Dispose_WithLeaveOpen()
{
var memoryStream = new MemoryStream();
Assert.IsTrue(memoryStream.CanWrite);
using (var cos = new CodedOutputStream(memoryStream, true))
{
cos.WriteRawByte(0);
Assert.AreEqual(0, memoryStream.Position); // Not flushed yet
}
Assert.AreEqual(1, memoryStream.Position); // Flushed data from CodedOutputStream to MemoryStream
Assert.IsTrue(memoryStream.CanWrite); // We left the stream open
}
示例10: StringToByteBuffer
private byte[] StringToByteBuffer(string str)
{
int t = CodedOutputStream.ComputeTagSize(9);
int s = CodedOutputStream.ComputeStringSize(str);
s += t;
byte[] bytes = new byte[s];
CodedOutputStream cos = new CodedOutputStream(bytes);
cos.WriteTag((9 << 3) + 2);
cos.WriteString(str);
cos.Flush();
return bytes;
}
示例11: AssertWriteVarint
/// <summary>
/// Writes the given value using WriteRawVarint32() and WriteRawVarint64() and
/// checks that the result matches the given bytes
/// </summary>
private static void AssertWriteVarint(byte[] data, ulong value)
{
// Only do 32-bit write if the value fits in 32 bits.
if ((value >> 32) == 0)
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawVarint32((uint) value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
// Also try computing size.
Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint32Size((uint) value));
}
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = new CodedOutputStream(rawOutput);
output.WriteRawVarint64(value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
// Also try computing size.
Assert.AreEqual(data.Length, CodedOutputStream.ComputeRawVarint64Size(value));
}
// Try different buffer sizes.
for (int bufferSize = 1; bufferSize <= 16; bufferSize *= 2)
{
// Only do 32-bit write if the value fits in 32 bits.
if ((value >> 32) == 0)
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output =
new CodedOutputStream(rawOutput, bufferSize);
output.WriteRawVarint32((uint) value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
}
{
MemoryStream rawOutput = new MemoryStream();
CodedOutputStream output = new CodedOutputStream(rawOutput, bufferSize);
output.WriteRawVarint64(value);
output.Flush();
Assert.AreEqual(data, rawOutput.ToArray());
}
}
}
示例12: ObjectToByteBuffer
private byte[] ObjectToByteBuffer(int descriptorId, object obj)
{
IMessage u = (IMessage)obj;
int size = u.CalculateSize();
byte[] bytes = new byte[size];
CodedOutputStream cos = new CodedOutputStream(bytes);
u.WriteTo(cos);
cos.Flush();
WrappedMessage wm = new WrappedMessage();
wm.WrappedMessageBytes = ByteString.CopyFrom(bytes);
wm.WrappedDescriptorId = descriptorId;
byte[] msgBytes = new byte[wm.CalculateSize()];
CodedOutputStream msgCos = new CodedOutputStream(msgBytes);
wm.WriteTo(msgCos);
msgCos.Flush();
return msgBytes;
}
示例13: TestRoundTripNegativeEnums
public void TestRoundTripNegativeEnums()
{
NegativeEnumMessage msg = new NegativeEnumMessage
{
Value = NegativeEnum.MinusOne,
Values = { NegativeEnum.NEGATIVE_ENUM_ZERO, NegativeEnum.MinusOne, NegativeEnum.FiveBelow },
PackedValues = { NegativeEnum.NEGATIVE_ENUM_ZERO, NegativeEnum.MinusOne, NegativeEnum.FiveBelow }
};
Assert.AreEqual(58, msg.CalculateSize());
byte[] bytes = new byte[58];
CodedOutputStream output = new CodedOutputStream(bytes);
msg.WriteTo(output);
Assert.AreEqual(0, output.SpaceLeft);
NegativeEnumMessage copy = NegativeEnumMessage.Parser.ParseFrom(bytes);
Assert.AreEqual(msg, copy);
}
示例14: SimpleProtobufUsage
public void SimpleProtobufUsage ()
{
const string SERVICE = "a";
const string METHOD = "b";
var request = new Request ();
request.Service = SERVICE;
request.Procedure = METHOD;
Assert.AreEqual (METHOD, request.Procedure);
Assert.AreEqual (SERVICE, request.Service);
var buffer = new byte [request.CalculateSize ()];
var stream = new CodedOutputStream (buffer);
request.WriteTo (stream);
Request reqCopy = Request.Parser.ParseFrom (buffer);
Assert.AreEqual (METHOD, reqCopy.Procedure);
Assert.AreEqual (SERVICE, reqCopy.Service);
}
示例15: SetUp
public void SetUp ()
{
// Create a request object and get the binary representation of it
expectedRequest = new Request ("TestService", "ProcedureNoArgsNoReturn");
using (var stream = new MemoryStream ()) {
var codedStream = new CodedOutputStream (stream, true);
codedStream.WriteInt32 (expectedRequest.ToProtobufMessage ().CalculateSize ());
expectedRequest.ToProtobufMessage ().WriteTo (codedStream);
codedStream.Flush ();
requestBytes = stream.ToArray ();
}
// Create a response object and get the binary representation of it
expectedResponse = new Response ();
expectedResponse.Error = "SomeErrorMessage";
expectedResponse.Time = 42;
using (var stream = new MemoryStream ()) {
var codedStream = new CodedOutputStream (stream, true);
codedStream.WriteInt32 (expectedResponse.ToProtobufMessage ().CalculateSize ());
expectedResponse.ToProtobufMessage ().WriteTo (codedStream);
codedStream.Flush ();
responseBytes = stream.ToArray ();
}
}