MessageExtensions.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using System.IO;
  33. namespace Google.Protobuf
  34. {
  35. /// <summary>
  36. /// Extension methods on <see cref="IMessage"/> and <see cref="IMessage{T}"/>.
  37. /// </summary>
  38. public static class MessageExtensions
  39. {
  40. public static void MergeFrom(this IMessage message, byte[] data)
  41. {
  42. Preconditions.CheckNotNull(message, "message");
  43. Preconditions.CheckNotNull(data, "data");
  44. CodedInputStream input = CodedInputStream.CreateInstance(data);
  45. message.MergeFrom(input);
  46. input.CheckLastTagWas(0);
  47. }
  48. public static void MergeFrom(this IMessage message, ByteString data)
  49. {
  50. Preconditions.CheckNotNull(message, "message");
  51. Preconditions.CheckNotNull(data, "data");
  52. CodedInputStream input = data.CreateCodedInput();
  53. message.MergeFrom(input);
  54. input.CheckLastTagWas(0);
  55. }
  56. public static void MergeFrom(this IMessage message, Stream input)
  57. {
  58. Preconditions.CheckNotNull(message, "message");
  59. Preconditions.CheckNotNull(input, "input");
  60. CodedInputStream codedInput = CodedInputStream.CreateInstance(input);
  61. message.MergeFrom(codedInput);
  62. codedInput.CheckLastTagWas(0);
  63. }
  64. public static void MergeDelimitedFrom(this IMessage message, Stream input)
  65. {
  66. Preconditions.CheckNotNull(message, "message");
  67. Preconditions.CheckNotNull(input, "input");
  68. int size = (int) CodedInputStream.ReadRawVarint32(input);
  69. Stream limitedStream = new LimitedInputStream(input, size);
  70. message.MergeFrom(limitedStream);
  71. }
  72. public static byte[] ToByteArray(this IMessage message)
  73. {
  74. Preconditions.CheckNotNull(message, "message");
  75. byte[] result = new byte[message.CalculateSize()];
  76. CodedOutputStream output = CodedOutputStream.CreateInstance(result);
  77. message.WriteTo(output);
  78. output.CheckNoSpaceLeft();
  79. return result;
  80. }
  81. public static void WriteTo(this IMessage message, Stream output)
  82. {
  83. Preconditions.CheckNotNull(message, "message");
  84. Preconditions.CheckNotNull(output, "output");
  85. CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
  86. message.WriteTo(codedOutput);
  87. codedOutput.Flush();
  88. }
  89. public static void WriteDelimitedTo(this IMessage message, Stream output)
  90. {
  91. Preconditions.CheckNotNull(message, "message");
  92. Preconditions.CheckNotNull(output, "output");
  93. CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output);
  94. codedOutput.WriteRawVarint32((uint)message.CalculateSize());
  95. message.WriteTo(codedOutput);
  96. codedOutput.Flush();
  97. }
  98. public static ByteString ToByteString(this IMessage message)
  99. {
  100. Preconditions.CheckNotNull(message, "message");
  101. return ByteString.AttachBytes(message.ToByteArray());
  102. }
  103. }
  104. }