csharptest | 16015a9 | 2010-11-05 14:49:32 -0500 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2008 Google Inc. All rights reserved. |
| 4 | // http://github.com/jskeet/dotnet-protobufs/ |
| 5 | // Original C++/Java/Python code: |
| 6 | // http://code.google.com/p/protobuf/ |
| 7 | // |
| 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions are |
| 10 | // met: |
| 11 | // |
| 12 | // * Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // * Redistributions in binary form must reproduce the above |
| 15 | // copyright notice, this list of conditions and the following disclaimer |
| 16 | // in the documentation and/or other materials provided with the |
| 17 | // distribution. |
| 18 | // * Neither the name of Google Inc. nor the names of its |
| 19 | // contributors may be used to endorse or promote products derived from |
| 20 | // this software without specific prior written permission. |
| 21 | // |
| 22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | #endregion |
| 34 | |
| 35 | using System.Collections; |
| 36 | using System.Collections.Generic; |
| 37 | using System.IO; |
| 38 | |
| 39 | namespace Google.ProtocolBuffers { |
| 40 | /// <summary> |
| 41 | /// Implementation of the non-generic IMessage interface as far as possible. |
| 42 | /// </summary> |
| 43 | public abstract class AbstractMessageLite<TMessage, TBuilder> : IMessageLite<TMessage, TBuilder> |
| 44 | where TMessage : AbstractMessageLite<TMessage, TBuilder> |
| 45 | where TBuilder : AbstractBuilderLite<TMessage, TBuilder> { |
| 46 | |
| 47 | |
| 48 | public abstract TBuilder CreateBuilderForType(); |
| 49 | |
| 50 | public abstract TBuilder ToBuilder(); |
| 51 | |
| 52 | public abstract TMessage DefaultInstanceForType { get; } |
| 53 | |
| 54 | public abstract bool IsInitialized { get; } |
| 55 | |
| 56 | public abstract void WriteTo(CodedOutputStream output); |
| 57 | |
| 58 | public abstract int SerializedSize { get; } |
| 59 | |
| 60 | //public override bool Equals(object other) { |
| 61 | //} |
| 62 | |
| 63 | //public override int GetHashCode() { |
| 64 | //} |
| 65 | |
| 66 | #region IMessageLite<TMessage,TBuilder> Members |
| 67 | |
| 68 | /// <summary> |
| 69 | /// Serializes the message to a ByteString. This is a trivial wrapper |
| 70 | /// around WriteTo(CodedOutputStream). |
| 71 | /// </summary> |
| 72 | public ByteString ToByteString() { |
| 73 | ByteString.CodedBuilder output = new ByteString.CodedBuilder(SerializedSize); |
| 74 | WriteTo(output.CodedOutput); |
| 75 | return output.Build(); |
| 76 | } |
| 77 | |
| 78 | /// <summary> |
| 79 | /// Serializes the message to a byte array. This is a trivial wrapper |
| 80 | /// around WriteTo(CodedOutputStream). |
| 81 | /// </summary> |
| 82 | public byte[] ToByteArray() { |
| 83 | byte[] result = new byte[SerializedSize]; |
| 84 | CodedOutputStream output = CodedOutputStream.CreateInstance(result); |
| 85 | WriteTo(output); |
| 86 | output.CheckNoSpaceLeft(); |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | /// <summary> |
| 91 | /// Serializes the message and writes it to the given stream. |
| 92 | /// This is just a wrapper around WriteTo(CodedOutputStream). This |
| 93 | /// does not flush or close the stream. |
| 94 | /// </summary> |
| 95 | /// <param name="output"></param> |
| 96 | public void WriteTo(Stream output) { |
| 97 | CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); |
| 98 | WriteTo(codedOutput); |
| 99 | codedOutput.Flush(); |
| 100 | } |
| 101 | |
| 102 | /// <summary> |
| 103 | /// Like WriteTo(Stream) but writes the size of the message as a varint before |
| 104 | /// writing the data. This allows more data to be written to the stream after the |
| 105 | /// message without the need to delimit the message data yourself. Use |
| 106 | /// IBuilder.MergeDelimitedFrom(Stream) or the static method |
| 107 | /// YourMessageType.ParseDelimitedFrom(Stream) to parse messages written by this method. |
| 108 | /// </summary> |
| 109 | /// <param name="output"></param> |
| 110 | public void WriteDelimitedTo(Stream output) { |
| 111 | CodedOutputStream codedOutput = CodedOutputStream.CreateInstance(output); |
| 112 | codedOutput.WriteRawVarint32((uint)SerializedSize); |
| 113 | WriteTo(codedOutput); |
| 114 | codedOutput.Flush(); |
| 115 | } |
| 116 | |
| 117 | IBuilderLite IMessageLite.WeakCreateBuilderForType() { |
| 118 | return CreateBuilderForType(); |
| 119 | } |
| 120 | |
| 121 | IBuilderLite IMessageLite.WeakToBuilder() { |
| 122 | return ToBuilder(); |
| 123 | } |
| 124 | |
| 125 | IMessageLite IMessageLite.WeakDefaultInstanceForType { |
| 126 | get { return DefaultInstanceForType; } |
| 127 | } |
| 128 | |
| 129 | #endregion |
| 130 | } |
| 131 | } |