1 | #region Copyright notice and license
|
---|
2 |
|
---|
3 | // Protocol Buffers - Google's data interchange format
|
---|
4 | // Copyright 2008 Google Inc. All rights reserved.
|
---|
5 | // http://github.com/jskeet/dotnet-protobufs/
|
---|
6 | // Original C++/Java/Python code:
|
---|
7 | // http://code.google.com/p/protobuf/
|
---|
8 | //
|
---|
9 | // Redistribution and use in source and binary forms, with or without
|
---|
10 | // modification, are permitted provided that the following conditions are
|
---|
11 | // met:
|
---|
12 | //
|
---|
13 | // * Redistributions of source code must retain the above copyright
|
---|
14 | // notice, this list of conditions and the following disclaimer.
|
---|
15 | // * Redistributions in binary form must reproduce the above
|
---|
16 | // copyright notice, this list of conditions and the following disclaimer
|
---|
17 | // in the documentation and/or other materials provided with the
|
---|
18 | // distribution.
|
---|
19 | // * Neither the name of Google Inc. nor the names of its
|
---|
20 | // contributors may be used to endorse or promote products derived from
|
---|
21 | // this software without specific prior written permission.
|
---|
22 | //
|
---|
23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
34 |
|
---|
35 | #endregion
|
---|
36 |
|
---|
37 | using System;
|
---|
38 | using System.IO;
|
---|
39 |
|
---|
40 | namespace Google.ProtocolBuffers
|
---|
41 | {
|
---|
42 | /// <summary>
|
---|
43 | /// Implementation of the non-generic IMessage interface as far as possible.
|
---|
44 | /// </summary>
|
---|
45 | public abstract partial class AbstractBuilderLite<TMessage, TBuilder> : IBuilderLite<TMessage, TBuilder>
|
---|
46 | where TMessage : AbstractMessageLite<TMessage, TBuilder>
|
---|
47 | where TBuilder : AbstractBuilderLite<TMessage, TBuilder>
|
---|
48 | {
|
---|
49 | protected abstract TBuilder ThisBuilder { get; }
|
---|
50 |
|
---|
51 | public abstract bool IsInitialized { get; }
|
---|
52 |
|
---|
53 | public abstract TBuilder Clear();
|
---|
54 |
|
---|
55 | public abstract TBuilder Clone();
|
---|
56 |
|
---|
57 | public abstract TMessage Build();
|
---|
58 |
|
---|
59 | public abstract TMessage BuildPartial();
|
---|
60 |
|
---|
61 | public abstract TBuilder MergeFrom(IMessageLite other);
|
---|
62 |
|
---|
63 | public abstract TBuilder MergeFrom(ICodedInputStream input, ExtensionRegistry extensionRegistry);
|
---|
64 |
|
---|
65 | public abstract TMessage DefaultInstanceForType { get; }
|
---|
66 |
|
---|
67 | #region IBuilderLite<TMessage,TBuilder> Members
|
---|
68 |
|
---|
69 | public virtual TBuilder MergeFrom(ICodedInputStream input)
|
---|
70 | {
|
---|
71 | return MergeFrom(input, ExtensionRegistry.CreateInstance());
|
---|
72 | }
|
---|
73 |
|
---|
74 | public TBuilder MergeDelimitedFrom(Stream input)
|
---|
75 | {
|
---|
76 | return MergeDelimitedFrom(input, ExtensionRegistry.CreateInstance());
|
---|
77 | }
|
---|
78 |
|
---|
79 | public TBuilder MergeDelimitedFrom(Stream input, ExtensionRegistry extensionRegistry)
|
---|
80 | {
|
---|
81 | int size = (int) CodedInputStream.ReadRawVarint32(input);
|
---|
82 | Stream limitedStream = new LimitedInputStream(input, size);
|
---|
83 | return MergeFrom(limitedStream, extensionRegistry);
|
---|
84 | }
|
---|
85 |
|
---|
86 | public TBuilder MergeFrom(ByteString data)
|
---|
87 | {
|
---|
88 | return MergeFrom(data, ExtensionRegistry.CreateInstance());
|
---|
89 | }
|
---|
90 |
|
---|
91 | public TBuilder MergeFrom(ByteString data, ExtensionRegistry extensionRegistry)
|
---|
92 | {
|
---|
93 | CodedInputStream input = data.CreateCodedInput();
|
---|
94 | MergeFrom(input, extensionRegistry);
|
---|
95 | input.CheckLastTagWas(0);
|
---|
96 | return ThisBuilder;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public TBuilder MergeFrom(byte[] data)
|
---|
100 | {
|
---|
101 | CodedInputStream input = CodedInputStream.CreateInstance(data);
|
---|
102 | MergeFrom(input);
|
---|
103 | input.CheckLastTagWas(0);
|
---|
104 | return ThisBuilder;
|
---|
105 | }
|
---|
106 |
|
---|
107 | public TBuilder MergeFrom(byte[] data, ExtensionRegistry extensionRegistry)
|
---|
108 | {
|
---|
109 | CodedInputStream input = CodedInputStream.CreateInstance(data);
|
---|
110 | MergeFrom(input, extensionRegistry);
|
---|
111 | input.CheckLastTagWas(0);
|
---|
112 | return ThisBuilder;
|
---|
113 | }
|
---|
114 |
|
---|
115 | public TBuilder MergeFrom(Stream input)
|
---|
116 | {
|
---|
117 | CodedInputStream codedInput = CodedInputStream.CreateInstance(input);
|
---|
118 | MergeFrom(codedInput);
|
---|
119 | codedInput.CheckLastTagWas(0);
|
---|
120 | return ThisBuilder;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public TBuilder MergeFrom(Stream input, ExtensionRegistry extensionRegistry)
|
---|
124 | {
|
---|
125 | CodedInputStream codedInput = CodedInputStream.CreateInstance(input);
|
---|
126 | MergeFrom(codedInput, extensionRegistry);
|
---|
127 | codedInput.CheckLastTagWas(0);
|
---|
128 | return ThisBuilder;
|
---|
129 | }
|
---|
130 |
|
---|
131 | #endregion
|
---|
132 |
|
---|
133 | #region Explicit definitions
|
---|
134 |
|
---|
135 | IBuilderLite IBuilderLite.WeakClear()
|
---|
136 | {
|
---|
137 | return Clear();
|
---|
138 | }
|
---|
139 |
|
---|
140 | IBuilderLite IBuilderLite.WeakMergeFrom(IMessageLite message)
|
---|
141 | {
|
---|
142 | return MergeFrom(message);
|
---|
143 | }
|
---|
144 |
|
---|
145 | IBuilderLite IBuilderLite.WeakMergeFrom(ByteString data)
|
---|
146 | {
|
---|
147 | return MergeFrom(data);
|
---|
148 | }
|
---|
149 |
|
---|
150 | IBuilderLite IBuilderLite.WeakMergeFrom(ByteString data, ExtensionRegistry registry)
|
---|
151 | {
|
---|
152 | return MergeFrom(data, registry);
|
---|
153 | }
|
---|
154 |
|
---|
155 | IBuilderLite IBuilderLite.WeakMergeFrom(ICodedInputStream input)
|
---|
156 | {
|
---|
157 | return MergeFrom(input);
|
---|
158 | }
|
---|
159 |
|
---|
160 | IBuilderLite IBuilderLite.WeakMergeFrom(ICodedInputStream input, ExtensionRegistry registry)
|
---|
161 | {
|
---|
162 | return MergeFrom(input, registry);
|
---|
163 | }
|
---|
164 |
|
---|
165 | IMessageLite IBuilderLite.WeakBuild()
|
---|
166 | {
|
---|
167 | return Build();
|
---|
168 | }
|
---|
169 |
|
---|
170 | IMessageLite IBuilderLite.WeakBuildPartial()
|
---|
171 | {
|
---|
172 | return BuildPartial();
|
---|
173 | }
|
---|
174 |
|
---|
175 | IBuilderLite IBuilderLite.WeakClone()
|
---|
176 | {
|
---|
177 | return Clone();
|
---|
178 | }
|
---|
179 |
|
---|
180 | IMessageLite IBuilderLite.WeakDefaultInstanceForType
|
---|
181 | {
|
---|
182 | get { return DefaultInstanceForType; }
|
---|
183 | }
|
---|
184 |
|
---|
185 | #endregion
|
---|
186 |
|
---|
187 | #region LimitedInputStream
|
---|
188 |
|
---|
189 | /// <summary>
|
---|
190 | /// Stream implementation which proxies another stream, only allowing a certain amount
|
---|
191 | /// of data to be read. Note that this is only used to read delimited streams, so it
|
---|
192 | /// doesn't attempt to implement everything.
|
---|
193 | /// </summary>
|
---|
194 | private class LimitedInputStream : Stream
|
---|
195 | {
|
---|
196 | private readonly Stream proxied;
|
---|
197 | private int bytesLeft;
|
---|
198 |
|
---|
199 | internal LimitedInputStream(Stream proxied, int size)
|
---|
200 | {
|
---|
201 | this.proxied = proxied;
|
---|
202 | bytesLeft = size;
|
---|
203 | }
|
---|
204 |
|
---|
205 | public override bool CanRead
|
---|
206 | {
|
---|
207 | get { return true; }
|
---|
208 | }
|
---|
209 |
|
---|
210 | public override bool CanSeek
|
---|
211 | {
|
---|
212 | get { return false; }
|
---|
213 | }
|
---|
214 |
|
---|
215 | public override bool CanWrite
|
---|
216 | {
|
---|
217 | get { return false; }
|
---|
218 | }
|
---|
219 |
|
---|
220 | public override void Flush()
|
---|
221 | {
|
---|
222 | }
|
---|
223 |
|
---|
224 | public override long Length
|
---|
225 | {
|
---|
226 | get { throw new NotSupportedException(); }
|
---|
227 | }
|
---|
228 |
|
---|
229 | public override long Position
|
---|
230 | {
|
---|
231 | get { throw new NotSupportedException(); }
|
---|
232 | set { throw new NotSupportedException(); }
|
---|
233 | }
|
---|
234 |
|
---|
235 | public override int Read(byte[] buffer, int offset, int count)
|
---|
236 | {
|
---|
237 | if (bytesLeft > 0)
|
---|
238 | {
|
---|
239 | int bytesRead = proxied.Read(buffer, offset, Math.Min(bytesLeft, count));
|
---|
240 | bytesLeft -= bytesRead;
|
---|
241 | return bytesRead;
|
---|
242 | }
|
---|
243 | return 0;
|
---|
244 | }
|
---|
245 |
|
---|
246 | public override long Seek(long offset, SeekOrigin origin)
|
---|
247 | {
|
---|
248 | throw new NotSupportedException();
|
---|
249 | }
|
---|
250 |
|
---|
251 | public override void SetLength(long value)
|
---|
252 | {
|
---|
253 | throw new NotSupportedException();
|
---|
254 | }
|
---|
255 |
|
---|
256 | public override void Write(byte[] buffer, int offset, int count)
|
---|
257 | {
|
---|
258 | throw new NotSupportedException();
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | #endregion
|
---|
263 | }
|
---|
264 | } |
---|