CodedOutputStream.cs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://github.com/jskeet/dotnet-protobufs/
  4. // Original C++/Java/Python code:
  5. // http://code.google.com/p/protobuf/
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. //
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Google Inc. nor the names of its
  18. // contributors may be used to endorse or promote products derived from
  19. // this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. using System;
  33. using System.IO;
  34. using System.Text;
  35. using Google.ProtocolBuffers.Descriptors;
  36. namespace Google.ProtocolBuffers {
  37. /// <summary>
  38. /// Encodes and writes protocol message fields.
  39. /// </summary>
  40. /// <remarks>
  41. /// This class contains two kinds of methods: methods that write specific
  42. /// protocol message constructs and field types (e.g. WriteTag and
  43. /// WriteInt32) and methods that write low-level values (e.g.
  44. /// WriteRawVarint32 and WriteRawBytes). If you are writing encoded protocol
  45. /// messages, you should use the former methods, but if you are writing some
  46. /// other format of your own design, use the latter. The names of the former
  47. /// methods are taken from the protocol buffer type names, not .NET types.
  48. /// (Hence WriteFloat instead of WriteSingle, and WriteBool instead of WriteBoolean.)
  49. /// </remarks>
  50. public sealed class CodedOutputStream {
  51. /// <summary>
  52. /// The buffer size used by CreateInstance(Stream).
  53. /// </summary>
  54. public static readonly int DefaultBufferSize = 4096;
  55. private readonly byte[] buffer;
  56. private readonly int limit;
  57. private int position;
  58. private readonly Stream output;
  59. #region Construction
  60. private CodedOutputStream(byte[] buffer, int offset, int length) {
  61. this.output = null;
  62. this.buffer = buffer;
  63. this.position = offset;
  64. this.limit = offset + length;
  65. }
  66. private CodedOutputStream(Stream output, byte[] buffer) {
  67. this.output = output;
  68. this.buffer = buffer;
  69. this.position = 0;
  70. this.limit = buffer.Length;
  71. }
  72. /// <summary>
  73. /// Creates a new CodedOutputStream which write to the given stream.
  74. /// </summary>
  75. public static CodedOutputStream CreateInstance(Stream output) {
  76. return CreateInstance(output, DefaultBufferSize);
  77. }
  78. /// <summary>
  79. /// Creates a new CodedOutputStream which write to the given stream and uses
  80. /// the specified buffer size.
  81. /// </summary>
  82. public static CodedOutputStream CreateInstance(Stream output, int bufferSize) {
  83. return new CodedOutputStream(output, new byte[bufferSize]);
  84. }
  85. /// <summary>
  86. /// Creates a new CodedOutputStream that writes directly to the given
  87. /// byte array. If more bytes are written than fit in the array,
  88. /// OutOfSpaceException will be thrown.
  89. /// </summary>
  90. public static CodedOutputStream CreateInstance(byte[] flatArray) {
  91. return CreateInstance(flatArray, 0, flatArray.Length);
  92. }
  93. /// <summary>
  94. /// Creates a new CodedOutputStream that writes directly to the given
  95. /// byte array slice. If more bytes are written than fit in the array,
  96. /// OutOfSpaceException will be thrown.
  97. /// </summary>
  98. public static CodedOutputStream CreateInstance(byte[] flatArray, int offset, int length) {
  99. return new CodedOutputStream(flatArray, offset, length);
  100. }
  101. #endregion
  102. #region Writing of tags etc
  103. /// <summary>
  104. /// Writes a double field value, including tag, to the stream.
  105. /// </summary>
  106. public void WriteDouble(int fieldNumber, double value) {
  107. // TODO(jonskeet): Test this on different endiannesses
  108. WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
  109. WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value));
  110. }
  111. /// <summary>
  112. /// Writes a float field value, including tag, to the stream.
  113. /// </summary>
  114. public void WriteFloat(int fieldNumber, float value) {
  115. WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
  116. // TODO(jonskeet): Test this on different endiannesses
  117. byte[] rawBytes = BitConverter.GetBytes(value);
  118. uint asInteger = BitConverter.ToUInt32(rawBytes, 0);
  119. WriteRawLittleEndian32(asInteger);
  120. }
  121. /// <summary>
  122. /// Writes a uint64 field value, including tag, to the stream.
  123. /// </summary>
  124. public void WriteUInt64(int fieldNumber, ulong value) {
  125. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  126. WriteRawVarint64(value);
  127. }
  128. /// <summary>
  129. /// Writes an int64 field value, including tag, to the stream.
  130. /// </summary>
  131. public void WriteInt64(int fieldNumber, long value) {
  132. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  133. WriteRawVarint64((ulong)value);
  134. }
  135. /// <summary>
  136. /// Writes an int32 field value, including tag, to the stream.
  137. /// </summary>
  138. public void WriteInt32(int fieldNumber, int value) {
  139. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  140. if (value >= 0) {
  141. WriteRawVarint32((uint)value);
  142. } else {
  143. // Must sign-extend.
  144. WriteRawVarint64((ulong)value);
  145. }
  146. }
  147. /// <summary>
  148. /// Writes a fixed64 field value, including tag, to the stream.
  149. /// </summary>
  150. public void WriteFixed64(int fieldNumber, ulong value) {
  151. WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
  152. WriteRawLittleEndian64(value);
  153. }
  154. /// <summary>
  155. /// Writes a fixed32 field value, including tag, to the stream.
  156. /// </summary>
  157. public void WriteFixed32(int fieldNumber, uint value) {
  158. WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
  159. WriteRawLittleEndian32(value);
  160. }
  161. /// <summary>
  162. /// Writes a bool field value, including tag, to the stream.
  163. /// </summary>
  164. public void WriteBool(int fieldNumber, bool value) {
  165. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  166. WriteRawByte(value ? (byte)1 : (byte)0);
  167. }
  168. /// <summary>
  169. /// Writes a string field value, including tag, to the stream.
  170. /// </summary>
  171. public void WriteString(int fieldNumber, string value) {
  172. WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
  173. // Optimise the case where we have enough space to write
  174. // the string directly to the buffer, which should be common.
  175. int length = Encoding.UTF8.GetByteCount(value);
  176. WriteRawVarint32((uint) length);
  177. if (limit - position >= length) {
  178. Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, position);
  179. position += length;
  180. } else {
  181. byte[] bytes = Encoding.UTF8.GetBytes(value);
  182. WriteRawBytes(bytes);
  183. }
  184. }
  185. /// <summary>
  186. /// Writes a group field value, including tag, to the stream.
  187. /// </summary>
  188. public void WriteGroup(int fieldNumber, IMessage value) {
  189. WriteTag(fieldNumber, WireFormat.WireType.StartGroup);
  190. value.WriteTo(this);
  191. WriteTag(fieldNumber, WireFormat.WireType.EndGroup);
  192. }
  193. public void WriteUnknownGroup(int fieldNumber, UnknownFieldSet value) {
  194. WriteTag(fieldNumber, WireFormat.WireType.StartGroup);
  195. value.WriteTo(this);
  196. WriteTag(fieldNumber, WireFormat.WireType.EndGroup);
  197. }
  198. public void WriteMessage(int fieldNumber, IMessage value) {
  199. WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
  200. WriteRawVarint32((uint)value.SerializedSize);
  201. value.WriteTo(this);
  202. }
  203. public void WriteBytes(int fieldNumber, ByteString value) {
  204. // TODO(jonskeet): Optimise this! (No need to copy the bytes twice.)
  205. WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
  206. byte[] bytes = value.ToByteArray();
  207. WriteRawVarint32((uint)bytes.Length);
  208. WriteRawBytes(bytes);
  209. }
  210. public void WriteUInt32(int fieldNumber, uint value) {
  211. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  212. WriteRawVarint32(value);
  213. }
  214. public void WriteEnum(int fieldNumber, int value) {
  215. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  216. WriteRawVarint32((uint)value);
  217. }
  218. public void WriteSFixed32(int fieldNumber, int value) {
  219. WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
  220. WriteRawLittleEndian32((uint)value);
  221. }
  222. public void WriteSFixed64(int fieldNumber, long value) {
  223. WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
  224. WriteRawLittleEndian64((ulong)value);
  225. }
  226. public void WriteSInt32(int fieldNumber, int value) {
  227. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  228. WriteRawVarint32(EncodeZigZag32(value));
  229. }
  230. public void WriteSInt64(int fieldNumber, long value) {
  231. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  232. WriteRawVarint64(EncodeZigZag64(value));
  233. }
  234. public void WriteMessageSetExtension(int fieldNumber, IMessage value) {
  235. WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.StartGroup);
  236. WriteUInt32(WireFormat.MessageSetField.TypeID, (uint)fieldNumber);
  237. WriteMessage(WireFormat.MessageSetField.Message, value);
  238. WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.EndGroup);
  239. }
  240. public void WriteRawMessageSetExtension(int fieldNumber, ByteString value) {
  241. WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.StartGroup);
  242. WriteUInt32(WireFormat.MessageSetField.TypeID, (uint)fieldNumber);
  243. WriteBytes(WireFormat.MessageSetField.Message, value);
  244. WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.EndGroup);
  245. }
  246. public void WriteField(FieldType fieldType, int fieldNumber, object value) {
  247. switch (fieldType) {
  248. case FieldType.Double: WriteDouble(fieldNumber, (double)value); break;
  249. case FieldType.Float: WriteFloat(fieldNumber, (float)value); break;
  250. case FieldType.Int64: WriteInt64(fieldNumber, (long)value); break;
  251. case FieldType.UInt64: WriteUInt64(fieldNumber, (ulong)value); break;
  252. case FieldType.Int32: WriteInt32(fieldNumber, (int)value); break;
  253. case FieldType.Fixed64: WriteFixed64(fieldNumber, (ulong)value); break;
  254. case FieldType.Fixed32: WriteFixed32(fieldNumber, (uint)value); break;
  255. case FieldType.Bool: WriteBool(fieldNumber, (bool)value); break;
  256. case FieldType.String: WriteString(fieldNumber, (string)value); break;
  257. case FieldType.Group: WriteGroup(fieldNumber, (IMessage)value); break;
  258. case FieldType.Message: WriteMessage(fieldNumber, (IMessage)value); break;
  259. case FieldType.Bytes: WriteBytes(fieldNumber, (ByteString)value); break;
  260. case FieldType.UInt32: WriteUInt32(fieldNumber, (uint)value); break;
  261. case FieldType.SFixed32: WriteSFixed32(fieldNumber, (int)value); break;
  262. case FieldType.SFixed64: WriteSFixed64(fieldNumber, (long)value); break;
  263. case FieldType.SInt32: WriteSInt32(fieldNumber, (int)value); break;
  264. case FieldType.SInt64: WriteSInt64(fieldNumber, (long)value); break;
  265. case FieldType.Enum: WriteEnum(fieldNumber, ((EnumValueDescriptor)value).Number);
  266. break;
  267. }
  268. }
  269. public void WriteFieldNoTag(FieldType fieldType, object value) {
  270. switch (fieldType) {
  271. case FieldType.Double: WriteDoubleNoTag((double)value); break;
  272. case FieldType.Float: WriteFloatNoTag((float)value); break;
  273. case FieldType.Int64: WriteInt64NoTag((long)value); break;
  274. case FieldType.UInt64: WriteUInt64NoTag((ulong)value); break;
  275. case FieldType.Int32: WriteInt32NoTag((int)value); break;
  276. case FieldType.Fixed64: WriteFixed64NoTag((ulong)value); break;
  277. case FieldType.Fixed32: WriteFixed32NoTag((uint)value); break;
  278. case FieldType.Bool: WriteBoolNoTag((bool)value); break;
  279. case FieldType.String: WriteStringNoTag((string)value); break;
  280. case FieldType.Group: WriteGroupNoTag((IMessage)value); break;
  281. case FieldType.Message: WriteMessageNoTag((IMessage)value); break;
  282. case FieldType.Bytes: WriteBytesNoTag((ByteString)value); break;
  283. case FieldType.UInt32: WriteUInt32NoTag((uint)value); break;
  284. case FieldType.SFixed32: WriteSFixed32NoTag((int)value); break;
  285. case FieldType.SFixed64: WriteSFixed64NoTag((long)value); break;
  286. case FieldType.SInt32: WriteSInt32NoTag((int)value); break;
  287. case FieldType.SInt64: WriteSInt64NoTag((long)value); break;
  288. case FieldType.Enum: WriteEnumNoTag(((EnumValueDescriptor)value).Number);
  289. break;
  290. }
  291. }
  292. #endregion
  293. #region Writing of values without tags
  294. /// <summary>
  295. /// Writes a double field value, including tag, to the stream.
  296. /// </summary>
  297. public void WriteDoubleNoTag(double value) {
  298. WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value));
  299. }
  300. /// <summary>
  301. /// Writes a float field value, without a tag, to the stream.
  302. /// </summary>
  303. public void WriteFloatNoTag(float value) {
  304. // TODO(jonskeet): Test this on different endiannesses
  305. byte[] rawBytes = BitConverter.GetBytes(value);
  306. uint asInteger = BitConverter.ToUInt32(rawBytes, 0);
  307. WriteRawLittleEndian32(asInteger);
  308. }
  309. /// <summary>
  310. /// Writes a uint64 field value, without a tag, to the stream.
  311. /// </summary>
  312. public void WriteUInt64NoTag(ulong value) {
  313. WriteRawVarint64(value);
  314. }
  315. /// <summary>
  316. /// Writes an int64 field value, without a tag, to the stream.
  317. /// </summary>
  318. public void WriteInt64NoTag(long value) {
  319. WriteRawVarint64((ulong)value);
  320. }
  321. /// <summary>
  322. /// Writes an int32 field value, without a tag, to the stream.
  323. /// </summary>
  324. public void WriteInt32NoTag(int value) {
  325. if (value >= 0) {
  326. WriteRawVarint32((uint)value);
  327. } else {
  328. // Must sign-extend.
  329. WriteRawVarint64((ulong)value);
  330. }
  331. }
  332. /// <summary>
  333. /// Writes a fixed64 field value, without a tag, to the stream.
  334. /// </summary>
  335. public void WriteFixed64NoTag(ulong value) {
  336. WriteRawLittleEndian64(value);
  337. }
  338. /// <summary>
  339. /// Writes a fixed32 field value, without a tag, to the stream.
  340. /// </summary>
  341. public void WriteFixed32NoTag(uint value) {
  342. WriteRawLittleEndian32(value);
  343. }
  344. /// <summary>
  345. /// Writes a bool field value, without a tag, to the stream.
  346. /// </summary>
  347. public void WriteBoolNoTag(bool value) {
  348. WriteRawByte(value ? (byte)1 : (byte)0);
  349. }
  350. /// <summary>
  351. /// Writes a string field value, without a tag, to the stream.
  352. /// </summary>
  353. public void WriteStringNoTag(string value) {
  354. // Optimise the case where we have enough space to write
  355. // the string directly to the buffer, which should be common.
  356. int length = Encoding.UTF8.GetByteCount(value);
  357. WriteRawVarint32((uint)length);
  358. if (limit - position >= length) {
  359. Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, position);
  360. position += length;
  361. } else {
  362. byte[] bytes = Encoding.UTF8.GetBytes(value);
  363. WriteRawBytes(bytes);
  364. }
  365. }
  366. /// <summary>
  367. /// Writes a group field value, without a tag, to the stream.
  368. /// </summary>
  369. public void WriteGroupNoTag(IMessage value) {
  370. value.WriteTo(this);
  371. }
  372. public void WriteMessageNoTag(IMessage value) {
  373. WriteRawVarint32((uint)value.SerializedSize);
  374. value.WriteTo(this);
  375. }
  376. public void WriteBytesNoTag(ByteString value) {
  377. // TODO(jonskeet): Optimise this! (No need to copy the bytes twice.)
  378. byte[] bytes = value.ToByteArray();
  379. WriteRawVarint32((uint)bytes.Length);
  380. WriteRawBytes(bytes);
  381. }
  382. public void WriteUInt32NoTag(uint value) {
  383. WriteRawVarint32(value);
  384. }
  385. public void WriteEnumNoTag(int value) {
  386. WriteRawVarint32((uint)value);
  387. }
  388. public void WriteSFixed32NoTag(int value) {
  389. WriteRawLittleEndian32((uint)value);
  390. }
  391. public void WriteSFixed64NoTag(long value) {
  392. WriteRawLittleEndian64((ulong)value);
  393. }
  394. public void WriteSInt32NoTag(int value) {
  395. WriteRawVarint32(EncodeZigZag32(value));
  396. }
  397. public void WriteSInt64NoTag(long value) {
  398. WriteRawVarint64(EncodeZigZag64(value));
  399. }
  400. #endregion
  401. #region Underlying writing primitives
  402. /// <summary>
  403. /// Encodes and writes a tag.
  404. /// </summary>
  405. public void WriteTag(int fieldNumber, WireFormat.WireType type) {
  406. WriteRawVarint32(WireFormat.MakeTag(fieldNumber, type));
  407. }
  408. private void SlowWriteRawVarint32(uint value) {
  409. while (true) {
  410. if ((value & ~0x7F) == 0) {
  411. WriteRawByte(value);
  412. return;
  413. } else {
  414. WriteRawByte((value & 0x7F) | 0x80);
  415. value >>= 7;
  416. }
  417. }
  418. }
  419. /// <summary>
  420. /// Writes a 32 bit value as a varint. The fast route is taken when
  421. /// there's enough buffer space left to whizz through without checking
  422. /// for each byte; otherwise, we resort to calling WriteRawByte each time.
  423. /// </summary>
  424. public void WriteRawVarint32(uint value) {
  425. if (position + 5 > limit) {
  426. SlowWriteRawVarint32(value);
  427. return;
  428. }
  429. while (true) {
  430. if ((value & ~0x7F) == 0) {
  431. buffer[position++] = (byte) value;
  432. return;
  433. } else {
  434. buffer[position++] = (byte)((value & 0x7F) | 0x80);
  435. value >>= 7;
  436. }
  437. }
  438. }
  439. public void WriteRawVarint64(ulong value) {
  440. while (true) {
  441. if ((value & ~0x7FUL) == 0) {
  442. WriteRawByte((uint)value);
  443. return;
  444. } else {
  445. WriteRawByte(((uint)value & 0x7F) | 0x80);
  446. value >>= 7;
  447. }
  448. }
  449. }
  450. public void WriteRawLittleEndian32(uint value) {
  451. WriteRawByte((byte)value);
  452. WriteRawByte((byte)(value >> 8));
  453. WriteRawByte((byte)(value >> 16));
  454. WriteRawByte((byte)(value >> 24));
  455. }
  456. public void WriteRawLittleEndian64(ulong value) {
  457. WriteRawByte((byte)value);
  458. WriteRawByte((byte)(value >> 8));
  459. WriteRawByte((byte)(value >> 16));
  460. WriteRawByte((byte)(value >> 24));
  461. WriteRawByte((byte)(value >> 32));
  462. WriteRawByte((byte)(value >> 40));
  463. WriteRawByte((byte)(value >> 48));
  464. WriteRawByte((byte)(value >> 56));
  465. }
  466. public void WriteRawByte(byte value) {
  467. if (position == limit) {
  468. RefreshBuffer();
  469. }
  470. buffer[position++] = value;
  471. }
  472. public void WriteRawByte(uint value) {
  473. WriteRawByte((byte)value);
  474. }
  475. /// <summary>
  476. /// Writes out an array of bytes.
  477. /// </summary>
  478. public void WriteRawBytes(byte[] value) {
  479. WriteRawBytes(value, 0, value.Length);
  480. }
  481. /// <summary>
  482. /// Writes out part of an array of bytes.
  483. /// </summary>
  484. public void WriteRawBytes(byte[] value, int offset, int length) {
  485. if (limit - position >= length) {
  486. Array.Copy(value, offset, buffer, position, length);
  487. // We have room in the current buffer.
  488. position += length;
  489. } else {
  490. // Write extends past current buffer. Fill the rest of this buffer and
  491. // flush.
  492. int bytesWritten = limit - position;
  493. Array.Copy(value, offset, buffer, position, bytesWritten);
  494. offset += bytesWritten;
  495. length -= bytesWritten;
  496. position = limit;
  497. RefreshBuffer();
  498. // Now deal with the rest.
  499. // Since we have an output stream, this is our buffer
  500. // and buffer offset == 0
  501. if (length <= limit) {
  502. // Fits in new buffer.
  503. Array.Copy(value, offset, buffer, 0, length);
  504. position = length;
  505. } else {
  506. // Write is very big. Let's do it all at once.
  507. output.Write(value, offset, length);
  508. }
  509. }
  510. }
  511. #endregion
  512. #region Size computations
  513. const int LittleEndian64Size = 8;
  514. const int LittleEndian32Size = 4;
  515. /// <summary>
  516. /// Compute the number of bytes that would be needed to encode a
  517. /// double field, including the tag.
  518. /// </summary>
  519. public static int ComputeDoubleSize(int fieldNumber, double value) {
  520. return ComputeTagSize(fieldNumber) + LittleEndian64Size;
  521. }
  522. /// <summary>
  523. /// Compute the number of bytes that would be needed to encode a
  524. /// float field, including the tag.
  525. /// </summary>
  526. public static int ComputeFloatSize(int fieldNumber, float value) {
  527. return ComputeTagSize(fieldNumber) + LittleEndian32Size;
  528. }
  529. /// <summary>
  530. /// Compute the number of bytes that would be needed to encode a
  531. /// uint64 field, including the tag.
  532. /// </summary>
  533. public static int ComputeUInt64Size(int fieldNumber, ulong value) {
  534. return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size(value);
  535. }
  536. /// <summary>
  537. /// Compute the number of bytes that would be needed to encode an
  538. /// int64 field, including the tag.
  539. /// </summary>
  540. public static int ComputeInt64Size(int fieldNumber, long value) {
  541. return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size((ulong)value);
  542. }
  543. /// <summary>
  544. /// Compute the number of bytes that would be needed to encode an
  545. /// int32 field, including the tag.
  546. /// </summary>
  547. public static int ComputeInt32Size(int fieldNumber, int value) {
  548. if (value >= 0) {
  549. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint)value);
  550. } else {
  551. // Must sign-extend.
  552. return ComputeTagSize(fieldNumber) + 10;
  553. }
  554. }
  555. /// <summary>
  556. /// Compute the number of bytes that would be needed to encode a
  557. /// fixed64 field, including the tag.
  558. /// </summary>
  559. public static int ComputeFixed64Size(int fieldNumber, ulong value) {
  560. return ComputeTagSize(fieldNumber) + LittleEndian64Size;
  561. }
  562. /// <summary>
  563. /// Compute the number of bytes that would be needed to encode a
  564. /// fixed32 field, including the tag.
  565. /// </summary>
  566. public static int ComputeFixed32Size(int fieldNumber, uint value) {
  567. return ComputeTagSize(fieldNumber) + LittleEndian32Size;
  568. }
  569. /// <summary>
  570. /// Compute the number of bytes that would be needed to encode a
  571. /// bool field, including the tag.
  572. /// </summary>
  573. public static int ComputeBoolSize(int fieldNumber, bool value) {
  574. return ComputeTagSize(fieldNumber) + 1;
  575. }
  576. /// <summary>
  577. /// Compute the number of bytes that would be needed to encode a
  578. /// string field, including the tag.
  579. /// </summary>
  580. public static int ComputeStringSize(int fieldNumber, String value) {
  581. int byteArraySize = Encoding.UTF8.GetByteCount(value);
  582. return ComputeTagSize(fieldNumber) +
  583. ComputeRawVarint32Size((uint)byteArraySize) +
  584. byteArraySize;
  585. }
  586. /// <summary>
  587. /// Compute the number of bytes that would be needed to encode a
  588. /// group field, including the tag.
  589. /// </summary>
  590. public static int ComputeGroupSize(int fieldNumber, IMessage value) {
  591. return ComputeTagSize(fieldNumber) * 2 + value.SerializedSize;
  592. }
  593. /// <summary>
  594. /// Compute the number of bytes that would be needed to encode a
  595. /// group field represented by an UnknownFieldSet, including the tag.
  596. /// </summary>
  597. public static int ComputeUnknownGroupSize(int fieldNumber,
  598. UnknownFieldSet value) {
  599. return ComputeTagSize(fieldNumber) * 2 + value.SerializedSize;
  600. }
  601. /// <summary>
  602. /// Compute the number of bytes that would be needed to encode an
  603. /// embedded message field, including the tag.
  604. /// </summary>
  605. public static int ComputeMessageSize(int fieldNumber, IMessage value) {
  606. int size = value.SerializedSize;
  607. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint)size) + size;
  608. }
  609. /// <summary>
  610. /// Compute the number of bytes that would be needed to encode a
  611. /// bytes field, including the tag.
  612. /// </summary>
  613. public static int ComputeBytesSize(int fieldNumber, ByteString value) {
  614. return ComputeTagSize(fieldNumber) +
  615. ComputeRawVarint32Size((uint)value.Length) +
  616. value.Length;
  617. }
  618. /// <summary>
  619. /// Compute the number of bytes that would be needed to encode a
  620. /// uint32 field, including the tag.
  621. /// </summary>
  622. public static int ComputeUInt32Size(int fieldNumber, uint value) {
  623. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size(value);
  624. }
  625. /// <summary>
  626. /// Compute the number of bytes that would be needed to encode a
  627. /// enum field, including the tag. The caller is responsible for
  628. /// converting the enum value to its numeric value.
  629. /// </summary>
  630. public static int ComputeEnumSize(int fieldNumber, int value) {
  631. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint)value);
  632. }
  633. /// <summary>
  634. /// Compute the number of bytes that would be needed to encode an
  635. /// sfixed32 field, including the tag.
  636. /// </summary>
  637. public static int ComputeSFixed32Size(int fieldNumber, int value) {
  638. return ComputeTagSize(fieldNumber) + LittleEndian32Size;
  639. }
  640. /// <summary>
  641. /// Compute the number of bytes that would be needed to encode an
  642. /// sfixed64 field, including the tag.
  643. /// </summary>
  644. public static int ComputeSFixed64Size(int fieldNumber, long value) {
  645. return ComputeTagSize(fieldNumber) + LittleEndian64Size;
  646. }
  647. /// <summary>
  648. /// Compute the number of bytes that would be needed to encode an
  649. /// sint32 field, including the tag.
  650. /// </summary>
  651. public static int ComputeSInt32Size(int fieldNumber, int value) {
  652. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size(EncodeZigZag32(value));
  653. }
  654. /// <summary>
  655. /// Compute the number of bytes that would be needed to encode an
  656. /// sint64 field, including the tag.
  657. /// </summary>
  658. public static int ComputeSInt64Size(int fieldNumber, long value) {
  659. return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size(EncodeZigZag64(value));
  660. }
  661. /// <summary>
  662. /// Compute the number of bytes that would be needed to encode a
  663. /// double field, including the tag.
  664. /// </summary>
  665. public static int ComputeDoubleSizeNoTag(double value) {
  666. return LittleEndian64Size;
  667. }
  668. /// <summary>
  669. /// Compute the number of bytes that would be needed to encode a
  670. /// float field, including the tag.
  671. /// </summary>
  672. public static int ComputeFloatSizeNoTag(float value) {
  673. return LittleEndian32Size;
  674. }
  675. /// <summary>
  676. /// Compute the number of bytes that would be needed to encode a
  677. /// uint64 field, including the tag.
  678. /// </summary>
  679. public static int ComputeUInt64SizeNoTag(ulong value) {
  680. return ComputeRawVarint64Size(value);
  681. }
  682. /// <summary>
  683. /// Compute the number of bytes that would be needed to encode an
  684. /// int64 field, including the tag.
  685. /// </summary>
  686. public static int ComputeInt64SizeNoTag(long value) {
  687. return ComputeRawVarint64Size((ulong)value);
  688. }
  689. /// <summary>
  690. /// Compute the number of bytes that would be needed to encode an
  691. /// int32 field, including the tag.
  692. /// </summary>
  693. public static int ComputeInt32SizeNoTag(int value) {
  694. if (value >= 0) {
  695. return ComputeRawVarint32Size((uint)value);
  696. } else {
  697. // Must sign-extend.
  698. return 10;
  699. }
  700. }
  701. /// <summary>
  702. /// Compute the number of bytes that would be needed to encode a
  703. /// fixed64 field, including the tag.
  704. /// </summary>
  705. public static int ComputeFixed64SizeNoTag(ulong value) {
  706. return LittleEndian64Size;
  707. }
  708. /// <summary>
  709. /// Compute the number of bytes that would be needed to encode a
  710. /// fixed32 field, including the tag.
  711. /// </summary>
  712. public static int ComputeFixed32SizeNoTag(uint value) {
  713. return LittleEndian32Size;
  714. }
  715. /// <summary>
  716. /// Compute the number of bytes that would be needed to encode a
  717. /// bool field, including the tag.
  718. /// </summary>
  719. public static int ComputeBoolSizeNoTag(bool value) {
  720. return 1;
  721. }
  722. /// <summary>
  723. /// Compute the number of bytes that would be needed to encode a
  724. /// string field, including the tag.
  725. /// </summary>
  726. public static int ComputeStringSizeNoTag(String value) {
  727. int byteArraySize = Encoding.UTF8.GetByteCount(value);
  728. return ComputeRawVarint32Size((uint)byteArraySize) +
  729. byteArraySize;
  730. }
  731. /// <summary>
  732. /// Compute the number of bytes that would be needed to encode a
  733. /// group field, including the tag.
  734. /// </summary>
  735. public static int ComputeGroupSizeNoTag(IMessage value) {
  736. return value.SerializedSize;
  737. }
  738. /// <summary>
  739. /// Compute the number of bytes that would be needed to encode a
  740. /// group field represented by an UnknownFieldSet, including the tag.
  741. /// </summary>
  742. public static int ComputeUnknownGroupSizeNoTag(UnknownFieldSet value) {
  743. return value.SerializedSize;
  744. }
  745. /// <summary>
  746. /// Compute the number of bytes that would be needed to encode an
  747. /// embedded message field, including the tag.
  748. /// </summary>
  749. public static int ComputeMessageSizeNoTag(IMessage value) {
  750. int size = value.SerializedSize;
  751. return ComputeRawVarint32Size((uint)size) + size;
  752. }
  753. /// <summary>
  754. /// Compute the number of bytes that would be needed to encode a
  755. /// bytes field, including the tag.
  756. /// </summary>
  757. public static int ComputeBytesSizeNoTag(ByteString value) {
  758. return ComputeRawVarint32Size((uint)value.Length) +
  759. value.Length;
  760. }
  761. /// <summary>
  762. /// Compute the number of bytes that would be needed to encode a
  763. /// uint32 field, including the tag.
  764. /// </summary>
  765. public static int ComputeUInt32SizeNoTag(uint value) {
  766. return ComputeRawVarint32Size(value);
  767. }
  768. /// <summary>
  769. /// Compute the number of bytes that would be needed to encode a
  770. /// enum field, including the tag. The caller is responsible for
  771. /// converting the enum value to its numeric value.
  772. /// </summary>
  773. public static int ComputeEnumSizeNoTag(int value) {
  774. return ComputeRawVarint32Size((uint)value);
  775. }
  776. /// <summary>
  777. /// Compute the number of bytes that would be needed to encode an
  778. /// sfixed32 field, including the tag.
  779. /// </summary>
  780. public static int ComputeSFixed32SizeNoTag(int value) {
  781. return LittleEndian32Size;
  782. }
  783. /// <summary>
  784. /// Compute the number of bytes that would be needed to encode an
  785. /// sfixed64 field, including the tag.
  786. /// </summary>
  787. public static int ComputeSFixed64SizeNoTag(long value) {
  788. return LittleEndian64Size;
  789. }
  790. /// <summary>
  791. /// Compute the number of bytes that would be needed to encode an
  792. /// sint32 field, including the tag.
  793. /// </summary>
  794. public static int ComputeSInt32SizeNoTag(int value) {
  795. return ComputeRawVarint32Size(EncodeZigZag32(value));
  796. }
  797. /// <summary>
  798. /// Compute the number of bytes that would be needed to encode an
  799. /// sint64 field, including the tag.
  800. /// </summary>
  801. public static int ComputeSInt64SizeNoTag(long value) {
  802. return ComputeRawVarint64Size(EncodeZigZag64(value));
  803. }
  804. /*
  805. * Compute the number of bytes that would be needed to encode a
  806. * MessageSet extension to the stream. For historical reasons,
  807. * the wire format differs from normal fields.
  808. */
  809. /// <summary>
  810. /// Compute the number of bytes that would be needed to encode a
  811. /// MessageSet extension to the stream. For historical reasons,
  812. /// the wire format differs from normal fields.
  813. /// </summary>
  814. public static int ComputeMessageSetExtensionSize(int fieldNumber, IMessage value) {
  815. return ComputeTagSize(WireFormat.MessageSetField.Item) * 2 +
  816. ComputeUInt32Size(WireFormat.MessageSetField.TypeID, (uint) fieldNumber) +
  817. ComputeMessageSize(WireFormat.MessageSetField.Message, value);
  818. }
  819. /// <summary>
  820. /// Compute the number of bytes that would be needed to encode an
  821. /// unparsed MessageSet extension field to the stream. For
  822. /// historical reasons, the wire format differs from normal fields.
  823. /// </summary>
  824. public static int ComputeRawMessageSetExtensionSize(int fieldNumber, ByteString value) {
  825. return ComputeTagSize(WireFormat.MessageSetField.Item) * 2 +
  826. ComputeUInt32Size(WireFormat.MessageSetField.TypeID, (uint) fieldNumber) +
  827. ComputeBytesSize(WireFormat.MessageSetField.Message, value);
  828. }
  829. /// <summary>
  830. /// Compute the number of bytes that would be needed to encode a varint.
  831. /// </summary>
  832. public static int ComputeRawVarint32Size(uint value) {
  833. if ((value & (0xffffffff << 7)) == 0) return 1;
  834. if ((value & (0xffffffff << 14)) == 0) return 2;
  835. if ((value & (0xffffffff << 21)) == 0) return 3;
  836. if ((value & (0xffffffff << 28)) == 0) return 4;
  837. return 5;
  838. }
  839. /// <summary>
  840. /// Compute the number of bytes that would be needed to encode a varint.
  841. /// </summary>
  842. public static int ComputeRawVarint64Size(ulong value) {
  843. if ((value & (0xffffffffffffffffL << 7)) == 0) return 1;
  844. if ((value & (0xffffffffffffffffL << 14)) == 0) return 2;
  845. if ((value & (0xffffffffffffffffL << 21)) == 0) return 3;
  846. if ((value & (0xffffffffffffffffL << 28)) == 0) return 4;
  847. if ((value & (0xffffffffffffffffL << 35)) == 0) return 5;
  848. if ((value & (0xffffffffffffffffL << 42)) == 0) return 6;
  849. if ((value & (0xffffffffffffffffL << 49)) == 0) return 7;
  850. if ((value & (0xffffffffffffffffL << 56)) == 0) return 8;
  851. if ((value & (0xffffffffffffffffL << 63)) == 0) return 9;
  852. return 10;
  853. }
  854. /// <summary>
  855. /// Compute the number of bytes that would be needed to encode a
  856. /// field of arbitrary type, including the tag, to the stream.
  857. /// </summary>
  858. public static int ComputeFieldSize(FieldType fieldType, int fieldNumber, Object value) {
  859. switch (fieldType) {
  860. case FieldType.Double: return ComputeDoubleSize(fieldNumber, (double)value);
  861. case FieldType.Float: return ComputeFloatSize(fieldNumber, (float)value);
  862. case FieldType.Int64: return ComputeInt64Size(fieldNumber, (long)value);
  863. case FieldType.UInt64: return ComputeUInt64Size(fieldNumber, (ulong)value);
  864. case FieldType.Int32: return ComputeInt32Size(fieldNumber, (int)value);
  865. case FieldType.Fixed64: return ComputeFixed64Size(fieldNumber, (ulong)value);
  866. case FieldType.Fixed32: return ComputeFixed32Size(fieldNumber, (uint)value);
  867. case FieldType.Bool: return ComputeBoolSize(fieldNumber, (bool)value);
  868. case FieldType.String: return ComputeStringSize(fieldNumber, (string)value);
  869. case FieldType.Group: return ComputeGroupSize(fieldNumber, (IMessage)value);
  870. case FieldType.Message: return ComputeMessageSize(fieldNumber, (IMessage)value);
  871. case FieldType.Bytes: return ComputeBytesSize(fieldNumber, (ByteString)value);
  872. case FieldType.UInt32: return ComputeUInt32Size(fieldNumber, (uint)value);
  873. case FieldType.SFixed32: return ComputeSFixed32Size(fieldNumber, (int)value);
  874. case FieldType.SFixed64: return ComputeSFixed64Size(fieldNumber, (long)value);
  875. case FieldType.SInt32: return ComputeSInt32Size(fieldNumber, (int)value);
  876. case FieldType.SInt64: return ComputeSInt64Size(fieldNumber, (long)value);
  877. case FieldType.Enum: return ComputeEnumSize(fieldNumber, ((EnumValueDescriptor)value).Number);
  878. default:
  879. throw new ArgumentOutOfRangeException("Invalid field type " + fieldType);
  880. }
  881. }
  882. /// <summary>
  883. /// Compute the number of bytes that would be needed to encode a
  884. /// field of arbitrary type, excluding the tag, to the stream.
  885. /// </summary>
  886. public static int ComputeFieldSizeNoTag(FieldType fieldType, Object value) {
  887. switch (fieldType) {
  888. case FieldType.Double: return ComputeDoubleSizeNoTag((double)value);
  889. case FieldType.Float: return ComputeFloatSizeNoTag((float)value);
  890. case FieldType.Int64: return ComputeInt64SizeNoTag((long)value);
  891. case FieldType.UInt64: return ComputeUInt64SizeNoTag((ulong)value);
  892. case FieldType.Int32: return ComputeInt32SizeNoTag((int)value);
  893. case FieldType.Fixed64: return ComputeFixed64SizeNoTag((ulong)value);
  894. case FieldType.Fixed32: return ComputeFixed32SizeNoTag((uint)value);
  895. case FieldType.Bool: return ComputeBoolSizeNoTag((bool)value);
  896. case FieldType.String: return ComputeStringSizeNoTag((string)value);
  897. case FieldType.Group: return ComputeGroupSizeNoTag((IMessage)value);
  898. case FieldType.Message: return ComputeMessageSizeNoTag((IMessage)value);
  899. case FieldType.Bytes: return ComputeBytesSizeNoTag((ByteString)value);
  900. case FieldType.UInt32: return ComputeUInt32SizeNoTag((uint)value);
  901. case FieldType.SFixed32: return ComputeSFixed32SizeNoTag((int)value);
  902. case FieldType.SFixed64: return ComputeSFixed64SizeNoTag((long)value);
  903. case FieldType.SInt32: return ComputeSInt32SizeNoTag((int)value);
  904. case FieldType.SInt64: return ComputeSInt64SizeNoTag((long)value);
  905. case FieldType.Enum: return ComputeEnumSizeNoTag(((EnumValueDescriptor)value).Number);
  906. default:
  907. throw new ArgumentOutOfRangeException("Invalid field type " + fieldType);
  908. }
  909. }
  910. /// <summary>
  911. /// Compute the number of bytes that would be needed to encode a tag.
  912. /// </summary>
  913. public static int ComputeTagSize(int fieldNumber) {
  914. return ComputeRawVarint32Size(WireFormat.MakeTag(fieldNumber, 0));
  915. }
  916. #endregion
  917. /// <summary>
  918. /// Encode a 32-bit value with ZigZag encoding.
  919. /// </summary>
  920. /// <remarks>
  921. /// ZigZag encodes signed integers into values that can be efficiently
  922. /// encoded with varint. (Otherwise, negative values must be
  923. /// sign-extended to 64 bits to be varint encoded, thus always taking
  924. /// 10 bytes on the wire.)
  925. /// </remarks>
  926. public static uint EncodeZigZag32(int n) {
  927. // Note: the right-shift must be arithmetic
  928. return (uint)((n << 1) ^ (n >> 31));
  929. }
  930. /// <summary>
  931. /// Encode a 64-bit value with ZigZag encoding.
  932. /// </summary>
  933. /// <remarks>
  934. /// ZigZag encodes signed integers into values that can be efficiently
  935. /// encoded with varint. (Otherwise, negative values must be
  936. /// sign-extended to 64 bits to be varint encoded, thus always taking
  937. /// 10 bytes on the wire.)
  938. /// </remarks>
  939. public static ulong EncodeZigZag64(long n) {
  940. return (ulong)((n << 1) ^ (n >> 63));
  941. }
  942. private void RefreshBuffer() {
  943. if (output == null) {
  944. // We're writing to a single buffer.
  945. throw new OutOfSpaceException();
  946. }
  947. // Since we have an output stream, this is our buffer
  948. // and buffer offset == 0
  949. output.Write(buffer, 0, position);
  950. position = 0;
  951. }
  952. /// <summary>
  953. /// Indicates that a CodedOutputStream wrapping a flat byte array
  954. /// ran out of space.
  955. /// </summary>
  956. public sealed class OutOfSpaceException : IOException {
  957. internal OutOfSpaceException()
  958. : base("CodedOutputStream was writing to a flat byte array and ran out of space.") {
  959. }
  960. }
  961. public void Flush() {
  962. if (output != null) {
  963. RefreshBuffer();
  964. }
  965. }
  966. /// <summary>
  967. /// Verifies that SpaceLeft returns zero. It's common to create a byte array
  968. /// that is exactly big enough to hold a message, then write to it with
  969. /// a CodedOutputStream. Calling CheckNoSpaceLeft after writing verifies that
  970. /// the message was actually as big as expected, which can help bugs.
  971. /// </summary>
  972. public void CheckNoSpaceLeft() {
  973. if (SpaceLeft != 0) {
  974. throw new InvalidOperationException("Did not write as much data as expected.");
  975. }
  976. }
  977. /// <summary>
  978. /// If writing to a flat array, returns the space left in the array. Otherwise,
  979. /// throws an InvalidOperationException.
  980. /// </summary>
  981. public int SpaceLeft {
  982. get {
  983. if (output == null) {
  984. return limit - position;
  985. } else {
  986. throw new InvalidOperationException(
  987. "SpaceLeft can only be called on CodedOutputStreams that are " +
  988. "writing to a flat array.");
  989. }
  990. }
  991. }
  992. }
  993. }