public static byte[] ToByteArray(this IMessage message) { Preconditions.CheckNotNull(message, "message"); byte[] result = new byte[message.CalculateSize()]; CodedOutputStream output = CodedOutputStream.CreateInstance(result); message.WriteTo(output); output.CheckNoSpaceLeft(); return result; }
/// <summary> /// Writes the length and then data of the given message to a stream. /// </summary> /// <param name="message">The message to write.</param> /// <param name="output">The output stream to write to.</param> public static void WriteDelimitedTo(this IMessage message, Stream output) { ProtoPreconditions.CheckNotNull(message, "message"); ProtoPreconditions.CheckNotNull(output, "output"); CodedOutputStream codedOutput = new CodedOutputStream(output); codedOutput.WriteRawVarint32((uint)message.CalculateSize()); message.WriteTo(codedOutput); codedOutput.Flush(); }
/// <summary> /// </summary> /// <param name="type"> /// </param> /// <param name="asValueType"> /// </param> /// <returns> /// </returns> public static int GetTypeSize(this IType type, bool asValueType = false) { if (asValueType && type.IsPrimitiveType()) { return SystemTypeSizes[type.Name]; } // find index int size; if (!sizeByType.TryGetValue(type, out size)) { IList<MemberLocationInfo> membersLayout; size = type.CalculateSize(out membersLayout); sizeByType[type] = size; membersLayoutByType[type] = membersLayout; } return size; }