CodedOutputStream.cs 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. using System;
  35. using System.IO;
  36. using System.Text;
  37. using Google.ProtocolBuffers.Descriptors;
  38. namespace Google.ProtocolBuffers
  39. {
  40. /// <summary>
  41. /// Encodes and writes protocol message fields.
  42. /// </summary>
  43. /// <remarks>
  44. /// This class contains two kinds of methods: methods that write specific
  45. /// protocol message constructs and field types (e.g. WriteTag and
  46. /// WriteInt32) and methods that write low-level values (e.g.
  47. /// WriteRawVarint32 and WriteRawBytes). If you are writing encoded protocol
  48. /// messages, you should use the former methods, but if you are writing some
  49. /// other format of your own design, use the latter. The names of the former
  50. /// methods are taken from the protocol buffer type names, not .NET types.
  51. /// (Hence WriteFloat instead of WriteSingle, and WriteBool instead of WriteBoolean.)
  52. /// </remarks>
  53. public sealed partial class CodedOutputStream
  54. {
  55. /// <summary>
  56. /// The buffer size used by CreateInstance(Stream).
  57. /// </summary>
  58. public static readonly int DefaultBufferSize = 4096;
  59. private readonly byte[] buffer;
  60. private readonly int limit;
  61. private int position;
  62. private readonly Stream output;
  63. #region Construction
  64. private CodedOutputStream(byte[] buffer, int offset, int length)
  65. {
  66. this.output = null;
  67. this.buffer = buffer;
  68. this.position = offset;
  69. this.limit = offset + length;
  70. }
  71. private CodedOutputStream(Stream output, byte[] buffer)
  72. {
  73. this.output = output;
  74. this.buffer = buffer;
  75. this.position = 0;
  76. this.limit = buffer.Length;
  77. }
  78. /// <summary>
  79. /// Creates a new CodedOutputStream which write to the given stream.
  80. /// </summary>
  81. public static CodedOutputStream CreateInstance(Stream output)
  82. {
  83. return CreateInstance(output, DefaultBufferSize);
  84. }
  85. /// <summary>
  86. /// Creates a new CodedOutputStream which write to the given stream and uses
  87. /// the specified buffer size.
  88. /// </summary>
  89. public static CodedOutputStream CreateInstance(Stream output, int bufferSize)
  90. {
  91. return new CodedOutputStream(output, new byte[bufferSize]);
  92. }
  93. /// <summary>
  94. /// Creates a new CodedOutputStream that writes directly to the given
  95. /// byte array. If more bytes are written than fit in the array,
  96. /// OutOfSpaceException will be thrown.
  97. /// </summary>
  98. public static CodedOutputStream CreateInstance(byte[] flatArray)
  99. {
  100. return CreateInstance(flatArray, 0, flatArray.Length);
  101. }
  102. /// <summary>
  103. /// Creates a new CodedOutputStream that writes directly to the given
  104. /// byte array slice. If more bytes are written than fit in the array,
  105. /// OutOfSpaceException will be thrown.
  106. /// </summary>
  107. public static CodedOutputStream CreateInstance(byte[] flatArray, int offset, int length)
  108. {
  109. return new CodedOutputStream(flatArray, offset, length);
  110. }
  111. #endregion
  112. #region Writing of tags etc
  113. /// <summary>
  114. /// Writes a double field value, including tag, to the stream.
  115. /// </summary>
  116. public void WriteDouble(int fieldNumber, double value)
  117. {
  118. WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
  119. WriteDoubleNoTag(value);
  120. }
  121. /// <summary>
  122. /// Writes a float field value, including tag, to the stream.
  123. /// </summary>
  124. public void WriteFloat(int fieldNumber, float value)
  125. {
  126. WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
  127. WriteFloatNoTag(value);
  128. }
  129. /// <summary>
  130. /// Writes a uint64 field value, including tag, to the stream.
  131. /// </summary>
  132. [CLSCompliant(false)]
  133. public void WriteUInt64(int fieldNumber, ulong value)
  134. {
  135. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  136. WriteRawVarint64(value);
  137. }
  138. /// <summary>
  139. /// Writes an int64 field value, including tag, to the stream.
  140. /// </summary>
  141. public void WriteInt64(int fieldNumber, long value)
  142. {
  143. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  144. WriteRawVarint64((ulong) value);
  145. }
  146. /// <summary>
  147. /// Writes an int32 field value, including tag, to the stream.
  148. /// </summary>
  149. public void WriteInt32(int fieldNumber, int value)
  150. {
  151. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  152. if (value >= 0)
  153. {
  154. WriteRawVarint32((uint) value);
  155. }
  156. else
  157. {
  158. // Must sign-extend.
  159. WriteRawVarint64((ulong) value);
  160. }
  161. }
  162. /// <summary>
  163. /// Writes a fixed64 field value, including tag, to the stream.
  164. /// </summary>
  165. [CLSCompliant(false)]
  166. public void WriteFixed64(int fieldNumber, ulong value)
  167. {
  168. WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
  169. WriteRawLittleEndian64(value);
  170. }
  171. /// <summary>
  172. /// Writes a fixed32 field value, including tag, to the stream.
  173. /// </summary>
  174. [CLSCompliant(false)]
  175. public void WriteFixed32(int fieldNumber, uint value)
  176. {
  177. WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
  178. WriteRawLittleEndian32(value);
  179. }
  180. /// <summary>
  181. /// Writes a bool field value, including tag, to the stream.
  182. /// </summary>
  183. public void WriteBool(int fieldNumber, bool value)
  184. {
  185. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  186. WriteRawByte(value ? (byte) 1 : (byte) 0);
  187. }
  188. /// <summary>
  189. /// Writes a string field value, including tag, to the stream.
  190. /// </summary>
  191. public void WriteString(int fieldNumber, string value)
  192. {
  193. WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
  194. // Optimise the case where we have enough space to write
  195. // the string directly to the buffer, which should be common.
  196. int length = Encoding.UTF8.GetByteCount(value);
  197. WriteRawVarint32((uint) length);
  198. if (limit - position >= length)
  199. {
  200. Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, position);
  201. position += length;
  202. }
  203. else
  204. {
  205. byte[] bytes = Encoding.UTF8.GetBytes(value);
  206. WriteRawBytes(bytes);
  207. }
  208. }
  209. /// <summary>
  210. /// Writes a group field value, including tag, to the stream.
  211. /// </summary>
  212. public void WriteGroup(int fieldNumber, IMessageLite value)
  213. {
  214. WriteTag(fieldNumber, WireFormat.WireType.StartGroup);
  215. value.WriteTo(this);
  216. WriteTag(fieldNumber, WireFormat.WireType.EndGroup);
  217. }
  218. [Obsolete]
  219. public void WriteUnknownGroup(int fieldNumber, IMessageLite value)
  220. {
  221. WriteTag(fieldNumber, WireFormat.WireType.StartGroup);
  222. value.WriteTo(this);
  223. WriteTag(fieldNumber, WireFormat.WireType.EndGroup);
  224. }
  225. public void WriteMessage(int fieldNumber, IMessageLite value)
  226. {
  227. WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
  228. WriteRawVarint32((uint) value.SerializedSize);
  229. value.WriteTo(this);
  230. }
  231. public void WriteBytes(int fieldNumber, ByteString value)
  232. {
  233. WriteTag(fieldNumber, WireFormat.WireType.LengthDelimited);
  234. WriteRawVarint32((uint)value.Length);
  235. value.WriteTo(this);
  236. }
  237. [CLSCompliant(false)]
  238. public void WriteUInt32(int fieldNumber, uint value)
  239. {
  240. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  241. WriteRawVarint32(value);
  242. }
  243. public void WriteEnum(int fieldNumber, int value)
  244. {
  245. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  246. WriteRawVarint32((uint) value);
  247. }
  248. public void WriteSFixed32(int fieldNumber, int value)
  249. {
  250. WriteTag(fieldNumber, WireFormat.WireType.Fixed32);
  251. WriteRawLittleEndian32((uint) value);
  252. }
  253. public void WriteSFixed64(int fieldNumber, long value)
  254. {
  255. WriteTag(fieldNumber, WireFormat.WireType.Fixed64);
  256. WriteRawLittleEndian64((ulong) value);
  257. }
  258. public void WriteSInt32(int fieldNumber, int value)
  259. {
  260. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  261. WriteRawVarint32(EncodeZigZag32(value));
  262. }
  263. public void WriteSInt64(int fieldNumber, long value)
  264. {
  265. WriteTag(fieldNumber, WireFormat.WireType.Varint);
  266. WriteRawVarint64(EncodeZigZag64(value));
  267. }
  268. public void WriteMessageSetExtension(int fieldNumber, IMessageLite value)
  269. {
  270. WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.StartGroup);
  271. WriteUInt32(WireFormat.MessageSetField.TypeID, (uint) fieldNumber);
  272. WriteMessage(WireFormat.MessageSetField.Message, value);
  273. WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.EndGroup);
  274. }
  275. public void WriteRawMessageSetExtension(int fieldNumber, ByteString value)
  276. {
  277. WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.StartGroup);
  278. WriteUInt32(WireFormat.MessageSetField.TypeID, (uint) fieldNumber);
  279. WriteBytes(WireFormat.MessageSetField.Message, value);
  280. WriteTag(WireFormat.MessageSetField.Item, WireFormat.WireType.EndGroup);
  281. }
  282. public void WriteField(FieldType fieldType, int fieldNumber, object value)
  283. {
  284. switch (fieldType)
  285. {
  286. case FieldType.Double:
  287. WriteDouble(fieldNumber, (double) value);
  288. break;
  289. case FieldType.Float:
  290. WriteFloat(fieldNumber, (float) value);
  291. break;
  292. case FieldType.Int64:
  293. WriteInt64(fieldNumber, (long) value);
  294. break;
  295. case FieldType.UInt64:
  296. WriteUInt64(fieldNumber, (ulong) value);
  297. break;
  298. case FieldType.Int32:
  299. WriteInt32(fieldNumber, (int) value);
  300. break;
  301. case FieldType.Fixed64:
  302. WriteFixed64(fieldNumber, (ulong) value);
  303. break;
  304. case FieldType.Fixed32:
  305. WriteFixed32(fieldNumber, (uint) value);
  306. break;
  307. case FieldType.Bool:
  308. WriteBool(fieldNumber, (bool) value);
  309. break;
  310. case FieldType.String:
  311. WriteString(fieldNumber, (string) value);
  312. break;
  313. case FieldType.Group:
  314. WriteGroup(fieldNumber, (IMessageLite) value);
  315. break;
  316. case FieldType.Message:
  317. WriteMessage(fieldNumber, (IMessageLite) value);
  318. break;
  319. case FieldType.Bytes:
  320. WriteBytes(fieldNumber, (ByteString) value);
  321. break;
  322. case FieldType.UInt32:
  323. WriteUInt32(fieldNumber, (uint) value);
  324. break;
  325. case FieldType.SFixed32:
  326. WriteSFixed32(fieldNumber, (int) value);
  327. break;
  328. case FieldType.SFixed64:
  329. WriteSFixed64(fieldNumber, (long) value);
  330. break;
  331. case FieldType.SInt32:
  332. WriteSInt32(fieldNumber, (int) value);
  333. break;
  334. case FieldType.SInt64:
  335. WriteSInt64(fieldNumber, (long) value);
  336. break;
  337. case FieldType.Enum:
  338. WriteEnum(fieldNumber, ((IEnumLite) value).Number);
  339. break;
  340. }
  341. }
  342. public void WriteFieldNoTag(FieldType fieldType, object value)
  343. {
  344. switch (fieldType)
  345. {
  346. case FieldType.Double:
  347. WriteDoubleNoTag((double) value);
  348. break;
  349. case FieldType.Float:
  350. WriteFloatNoTag((float) value);
  351. break;
  352. case FieldType.Int64:
  353. WriteInt64NoTag((long) value);
  354. break;
  355. case FieldType.UInt64:
  356. WriteUInt64NoTag((ulong) value);
  357. break;
  358. case FieldType.Int32:
  359. WriteInt32NoTag((int) value);
  360. break;
  361. case FieldType.Fixed64:
  362. WriteFixed64NoTag((ulong) value);
  363. break;
  364. case FieldType.Fixed32:
  365. WriteFixed32NoTag((uint) value);
  366. break;
  367. case FieldType.Bool:
  368. WriteBoolNoTag((bool) value);
  369. break;
  370. case FieldType.String:
  371. WriteStringNoTag((string) value);
  372. break;
  373. case FieldType.Group:
  374. WriteGroupNoTag((IMessageLite) value);
  375. break;
  376. case FieldType.Message:
  377. WriteMessageNoTag((IMessageLite) value);
  378. break;
  379. case FieldType.Bytes:
  380. WriteBytesNoTag((ByteString) value);
  381. break;
  382. case FieldType.UInt32:
  383. WriteUInt32NoTag((uint) value);
  384. break;
  385. case FieldType.SFixed32:
  386. WriteSFixed32NoTag((int) value);
  387. break;
  388. case FieldType.SFixed64:
  389. WriteSFixed64NoTag((long) value);
  390. break;
  391. case FieldType.SInt32:
  392. WriteSInt32NoTag((int) value);
  393. break;
  394. case FieldType.SInt64:
  395. WriteSInt64NoTag((long) value);
  396. break;
  397. case FieldType.Enum:
  398. WriteEnumNoTag(((IEnumLite) value).Number);
  399. break;
  400. }
  401. }
  402. #endregion
  403. #region Writing of values without tags
  404. /// <summary>
  405. /// Writes a double field value, including tag, to the stream.
  406. /// </summary>
  407. public void WriteDoubleNoTag(double value)
  408. {
  409. // TODO(jonskeet): Test this on different endiannesses
  410. #if SILVERLIGHT2 || COMPACT_FRAMEWORK_35
  411. byte[] bytes = BitConverter.GetBytes(value);
  412. WriteRawBytes(bytes, 0, 8);
  413. #else
  414. WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value));
  415. #endif
  416. }
  417. /// <summary>
  418. /// Writes a float field value, without a tag, to the stream.
  419. /// </summary>
  420. public void WriteFloatNoTag(float value)
  421. {
  422. // TODO(jonskeet): Test this on different endiannesses
  423. byte[] rawBytes = BitConverter.GetBytes(value);
  424. uint asInteger = BitConverter.ToUInt32(rawBytes, 0);
  425. WriteRawLittleEndian32(asInteger);
  426. }
  427. /// <summary>
  428. /// Writes a uint64 field value, without a tag, to the stream.
  429. /// </summary>
  430. [CLSCompliant(false)]
  431. public void WriteUInt64NoTag(ulong value)
  432. {
  433. WriteRawVarint64(value);
  434. }
  435. /// <summary>
  436. /// Writes an int64 field value, without a tag, to the stream.
  437. /// </summary>
  438. public void WriteInt64NoTag(long value)
  439. {
  440. WriteRawVarint64((ulong) value);
  441. }
  442. /// <summary>
  443. /// Writes an int32 field value, without a tag, to the stream.
  444. /// </summary>
  445. public void WriteInt32NoTag(int value)
  446. {
  447. if (value >= 0)
  448. {
  449. WriteRawVarint32((uint) value);
  450. }
  451. else
  452. {
  453. // Must sign-extend.
  454. WriteRawVarint64((ulong) value);
  455. }
  456. }
  457. /// <summary>
  458. /// Writes a fixed64 field value, without a tag, to the stream.
  459. /// </summary>
  460. [CLSCompliant(false)]
  461. public void WriteFixed64NoTag(ulong value)
  462. {
  463. WriteRawLittleEndian64(value);
  464. }
  465. /// <summary>
  466. /// Writes a fixed32 field value, without a tag, to the stream.
  467. /// </summary>
  468. [CLSCompliant(false)]
  469. public void WriteFixed32NoTag(uint value)
  470. {
  471. WriteRawLittleEndian32(value);
  472. }
  473. /// <summary>
  474. /// Writes a bool field value, without a tag, to the stream.
  475. /// </summary>
  476. public void WriteBoolNoTag(bool value)
  477. {
  478. WriteRawByte(value ? (byte) 1 : (byte) 0);
  479. }
  480. /// <summary>
  481. /// Writes a string field value, without a tag, to the stream.
  482. /// </summary>
  483. public void WriteStringNoTag(string value)
  484. {
  485. // Optimise the case where we have enough space to write
  486. // the string directly to the buffer, which should be common.
  487. int length = Encoding.UTF8.GetByteCount(value);
  488. WriteRawVarint32((uint) length);
  489. if (limit - position >= length)
  490. {
  491. Encoding.UTF8.GetBytes(value, 0, value.Length, buffer, position);
  492. position += length;
  493. }
  494. else
  495. {
  496. byte[] bytes = Encoding.UTF8.GetBytes(value);
  497. WriteRawBytes(bytes);
  498. }
  499. }
  500. /// <summary>
  501. /// Writes a group field value, without a tag, to the stream.
  502. /// </summary>
  503. public void WriteGroupNoTag(IMessageLite value)
  504. {
  505. value.WriteTo(this);
  506. }
  507. public void WriteMessageNoTag(IMessageLite value)
  508. {
  509. WriteRawVarint32((uint) value.SerializedSize);
  510. value.WriteTo(this);
  511. }
  512. public void WriteBytesNoTag(ByteString value)
  513. {
  514. WriteRawVarint32((uint)value.Length);
  515. value.WriteTo(this);
  516. }
  517. [CLSCompliant(false)]
  518. public void WriteUInt32NoTag(uint value)
  519. {
  520. WriteRawVarint32(value);
  521. }
  522. public void WriteEnumNoTag(int value)
  523. {
  524. WriteRawVarint32((uint) value);
  525. }
  526. public void WriteSFixed32NoTag(int value)
  527. {
  528. WriteRawLittleEndian32((uint) value);
  529. }
  530. public void WriteSFixed64NoTag(long value)
  531. {
  532. WriteRawLittleEndian64((ulong) value);
  533. }
  534. public void WriteSInt32NoTag(int value)
  535. {
  536. WriteRawVarint32(EncodeZigZag32(value));
  537. }
  538. public void WriteSInt64NoTag(long value)
  539. {
  540. WriteRawVarint64(EncodeZigZag64(value));
  541. }
  542. #endregion
  543. #region Underlying writing primitives
  544. /// <summary>
  545. /// Encodes and writes a tag.
  546. /// </summary>
  547. [CLSCompliant(false)]
  548. public void WriteTag(int fieldNumber, WireFormat.WireType type)
  549. {
  550. WriteRawVarint32(WireFormat.MakeTag(fieldNumber, type));
  551. }
  552. private void SlowWriteRawVarint32(uint value)
  553. {
  554. while (true)
  555. {
  556. if ((value & ~0x7F) == 0)
  557. {
  558. WriteRawByte(value);
  559. return;
  560. }
  561. else
  562. {
  563. WriteRawByte((value & 0x7F) | 0x80);
  564. value >>= 7;
  565. }
  566. }
  567. }
  568. /// <summary>
  569. /// Writes a 32 bit value as a varint. The fast route is taken when
  570. /// there's enough buffer space left to whizz through without checking
  571. /// for each byte; otherwise, we resort to calling WriteRawByte each time.
  572. /// </summary>
  573. [CLSCompliant(false)]
  574. public void WriteRawVarint32(uint value)
  575. {
  576. if (position + 5 > limit)
  577. {
  578. SlowWriteRawVarint32(value);
  579. return;
  580. }
  581. while (true)
  582. {
  583. if ((value & ~0x7F) == 0)
  584. {
  585. buffer[position++] = (byte) value;
  586. return;
  587. }
  588. else
  589. {
  590. buffer[position++] = (byte) ((value & 0x7F) | 0x80);
  591. value >>= 7;
  592. }
  593. }
  594. }
  595. [CLSCompliant(false)]
  596. public void WriteRawVarint64(ulong value)
  597. {
  598. while (true)
  599. {
  600. if ((value & ~0x7FUL) == 0)
  601. {
  602. WriteRawByte((uint) value);
  603. return;
  604. }
  605. else
  606. {
  607. WriteRawByte(((uint) value & 0x7F) | 0x80);
  608. value >>= 7;
  609. }
  610. }
  611. }
  612. [CLSCompliant(false)]
  613. public void WriteRawLittleEndian32(uint value)
  614. {
  615. WriteRawByte((byte) value);
  616. WriteRawByte((byte) (value >> 8));
  617. WriteRawByte((byte) (value >> 16));
  618. WriteRawByte((byte) (value >> 24));
  619. }
  620. [CLSCompliant(false)]
  621. public void WriteRawLittleEndian64(ulong value)
  622. {
  623. WriteRawByte((byte) value);
  624. WriteRawByte((byte) (value >> 8));
  625. WriteRawByte((byte) (value >> 16));
  626. WriteRawByte((byte) (value >> 24));
  627. WriteRawByte((byte) (value >> 32));
  628. WriteRawByte((byte) (value >> 40));
  629. WriteRawByte((byte) (value >> 48));
  630. WriteRawByte((byte) (value >> 56));
  631. }
  632. public void WriteRawByte(byte value)
  633. {
  634. if (position == limit)
  635. {
  636. RefreshBuffer();
  637. }
  638. buffer[position++] = value;
  639. }
  640. [CLSCompliant(false)]
  641. public void WriteRawByte(uint value)
  642. {
  643. WriteRawByte((byte) value);
  644. }
  645. /// <summary>
  646. /// Writes out an array of bytes.
  647. /// </summary>
  648. public void WriteRawBytes(byte[] value)
  649. {
  650. WriteRawBytes(value, 0, value.Length);
  651. }
  652. /// <summary>
  653. /// Writes out part of an array of bytes.
  654. /// </summary>
  655. public void WriteRawBytes(byte[] value, int offset, int length)
  656. {
  657. if (limit - position >= length)
  658. {
  659. Array.Copy(value, offset, buffer, position, length);
  660. // We have room in the current buffer.
  661. position += length;
  662. }
  663. else
  664. {
  665. // Write extends past current buffer. Fill the rest of this buffer and
  666. // flush.
  667. int bytesWritten = limit - position;
  668. Array.Copy(value, offset, buffer, position, bytesWritten);
  669. offset += bytesWritten;
  670. length -= bytesWritten;
  671. position = limit;
  672. RefreshBuffer();
  673. // Now deal with the rest.
  674. // Since we have an output stream, this is our buffer
  675. // and buffer offset == 0
  676. if (length <= limit)
  677. {
  678. // Fits in new buffer.
  679. Array.Copy(value, offset, buffer, 0, length);
  680. position = length;
  681. }
  682. else
  683. {
  684. // Write is very big. Let's do it all at once.
  685. output.Write(value, offset, length);
  686. }
  687. }
  688. }
  689. #endregion
  690. #region Size computations
  691. private const int LittleEndian64Size = 8;
  692. private const int LittleEndian32Size = 4;
  693. /// <summary>
  694. /// Compute the number of bytes that would be needed to encode a
  695. /// double field, including the tag.
  696. /// </summary>
  697. public static int ComputeDoubleSize(int fieldNumber, double value)
  698. {
  699. return ComputeTagSize(fieldNumber) + LittleEndian64Size;
  700. }
  701. /// <summary>
  702. /// Compute the number of bytes that would be needed to encode a
  703. /// float field, including the tag.
  704. /// </summary>
  705. public static int ComputeFloatSize(int fieldNumber, float value)
  706. {
  707. return ComputeTagSize(fieldNumber) + LittleEndian32Size;
  708. }
  709. /// <summary>
  710. /// Compute the number of bytes that would be needed to encode a
  711. /// uint64 field, including the tag.
  712. /// </summary>
  713. [CLSCompliant(false)]
  714. public static int ComputeUInt64Size(int fieldNumber, ulong value)
  715. {
  716. return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size(value);
  717. }
  718. /// <summary>
  719. /// Compute the number of bytes that would be needed to encode an
  720. /// int64 field, including the tag.
  721. /// </summary>
  722. public static int ComputeInt64Size(int fieldNumber, long value)
  723. {
  724. return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size((ulong) value);
  725. }
  726. /// <summary>
  727. /// Compute the number of bytes that would be needed to encode an
  728. /// int32 field, including the tag.
  729. /// </summary>
  730. public static int ComputeInt32Size(int fieldNumber, int value)
  731. {
  732. if (value >= 0)
  733. {
  734. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint) value);
  735. }
  736. else
  737. {
  738. // Must sign-extend.
  739. return ComputeTagSize(fieldNumber) + 10;
  740. }
  741. }
  742. /// <summary>
  743. /// Compute the number of bytes that would be needed to encode a
  744. /// fixed64 field, including the tag.
  745. /// </summary>
  746. [CLSCompliant(false)]
  747. public static int ComputeFixed64Size(int fieldNumber, ulong value)
  748. {
  749. return ComputeTagSize(fieldNumber) + LittleEndian64Size;
  750. }
  751. /// <summary>
  752. /// Compute the number of bytes that would be needed to encode a
  753. /// fixed32 field, including the tag.
  754. /// </summary>
  755. [CLSCompliant(false)]
  756. public static int ComputeFixed32Size(int fieldNumber, uint value)
  757. {
  758. return ComputeTagSize(fieldNumber) + LittleEndian32Size;
  759. }
  760. /// <summary>
  761. /// Compute the number of bytes that would be needed to encode a
  762. /// bool field, including the tag.
  763. /// </summary>
  764. public static int ComputeBoolSize(int fieldNumber, bool value)
  765. {
  766. return ComputeTagSize(fieldNumber) + 1;
  767. }
  768. /// <summary>
  769. /// Compute the number of bytes that would be needed to encode a
  770. /// string field, including the tag.
  771. /// </summary>
  772. public static int ComputeStringSize(int fieldNumber, String value)
  773. {
  774. int byteArraySize = Encoding.UTF8.GetByteCount(value);
  775. return ComputeTagSize(fieldNumber) +
  776. ComputeRawVarint32Size((uint) byteArraySize) +
  777. byteArraySize;
  778. }
  779. /// <summary>
  780. /// Compute the number of bytes that would be needed to encode a
  781. /// group field, including the tag.
  782. /// </summary>
  783. public static int ComputeGroupSize(int fieldNumber, IMessageLite value)
  784. {
  785. return ComputeTagSize(fieldNumber)*2 + value.SerializedSize;
  786. }
  787. /// <summary>
  788. /// Compute the number of bytes that would be needed to encode a
  789. /// group field represented by an UnknownFieldSet, including the tag.
  790. /// </summary>
  791. [Obsolete]
  792. public static int ComputeUnknownGroupSize(int fieldNumber,
  793. IMessageLite value)
  794. {
  795. return ComputeTagSize(fieldNumber)*2 + value.SerializedSize;
  796. }
  797. /// <summary>
  798. /// Compute the number of bytes that would be needed to encode an
  799. /// embedded message field, including the tag.
  800. /// </summary>
  801. public static int ComputeMessageSize(int fieldNumber, IMessageLite value)
  802. {
  803. int size = value.SerializedSize;
  804. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint) size) + size;
  805. }
  806. /// <summary>
  807. /// Compute the number of bytes that would be needed to encode a
  808. /// bytes field, including the tag.
  809. /// </summary>
  810. public static int ComputeBytesSize(int fieldNumber, ByteString value)
  811. {
  812. return ComputeTagSize(fieldNumber) +
  813. ComputeRawVarint32Size((uint) value.Length) +
  814. value.Length;
  815. }
  816. /// <summary>
  817. /// Compute the number of bytes that would be needed to encode a
  818. /// uint32 field, including the tag.
  819. /// </summary>
  820. [CLSCompliant(false)]
  821. public static int ComputeUInt32Size(int fieldNumber, uint value)
  822. {
  823. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size(value);
  824. }
  825. /// <summary>
  826. /// Compute the number of bytes that would be needed to encode a
  827. /// enum field, including the tag. The caller is responsible for
  828. /// converting the enum value to its numeric value.
  829. /// </summary>
  830. public static int ComputeEnumSize(int fieldNumber, int value)
  831. {
  832. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size((uint) value);
  833. }
  834. /// <summary>
  835. /// Compute the number of bytes that would be needed to encode an
  836. /// sfixed32 field, including the tag.
  837. /// </summary>
  838. public static int ComputeSFixed32Size(int fieldNumber, int value)
  839. {
  840. return ComputeTagSize(fieldNumber) + LittleEndian32Size;
  841. }
  842. /// <summary>
  843. /// Compute the number of bytes that would be needed to encode an
  844. /// sfixed64 field, including the tag.
  845. /// </summary>
  846. public static int ComputeSFixed64Size(int fieldNumber, long value)
  847. {
  848. return ComputeTagSize(fieldNumber) + LittleEndian64Size;
  849. }
  850. /// <summary>
  851. /// Compute the number of bytes that would be needed to encode an
  852. /// sint32 field, including the tag.
  853. /// </summary>
  854. public static int ComputeSInt32Size(int fieldNumber, int value)
  855. {
  856. return ComputeTagSize(fieldNumber) + ComputeRawVarint32Size(EncodeZigZag32(value));
  857. }
  858. /// <summary>
  859. /// Compute the number of bytes that would be needed to encode an
  860. /// sint64 field, including the tag.
  861. /// </summary>
  862. public static int ComputeSInt64Size(int fieldNumber, long value)
  863. {
  864. return ComputeTagSize(fieldNumber) + ComputeRawVarint64Size(EncodeZigZag64(value));
  865. }
  866. /// <summary>
  867. /// Compute the number of bytes that would be needed to encode a
  868. /// double field, including the tag.
  869. /// </summary>
  870. public static int ComputeDoubleSizeNoTag(double value)
  871. {
  872. return LittleEndian64Size;
  873. }
  874. /// <summary>
  875. /// Compute the number of bytes that would be needed to encode a
  876. /// float field, including the tag.
  877. /// </summary>
  878. public static int ComputeFloatSizeNoTag(float value)
  879. {
  880. return LittleEndian32Size;
  881. }
  882. /// <summary>
  883. /// Compute the number of bytes that would be needed to encode a
  884. /// uint64 field, including the tag.
  885. /// </summary>
  886. [CLSCompliant(false)]
  887. public static int ComputeUInt64SizeNoTag(ulong value)
  888. {
  889. return ComputeRawVarint64Size(value);
  890. }
  891. /// <summary>
  892. /// Compute the number of bytes that would be needed to encode an
  893. /// int64 field, including the tag.
  894. /// </summary>
  895. public static int ComputeInt64SizeNoTag(long value)
  896. {
  897. return ComputeRawVarint64Size((ulong) value);
  898. }
  899. /// <summary>
  900. /// Compute the number of bytes that would be needed to encode an
  901. /// int32 field, including the tag.
  902. /// </summary>
  903. public static int ComputeInt32SizeNoTag(int value)
  904. {
  905. if (value >= 0)
  906. {
  907. return ComputeRawVarint32Size((uint) value);
  908. }
  909. else
  910. {
  911. // Must sign-extend.
  912. return 10;
  913. }
  914. }
  915. /// <summary>
  916. /// Compute the number of bytes that would be needed to encode a
  917. /// fixed64 field, including the tag.
  918. /// </summary>
  919. [CLSCompliant(false)]
  920. public static int ComputeFixed64SizeNoTag(ulong value)
  921. {
  922. return LittleEndian64Size;
  923. }
  924. /// <summary>
  925. /// Compute the number of bytes that would be needed to encode a
  926. /// fixed32 field, including the tag.
  927. /// </summary>
  928. [CLSCompliant(false)]
  929. public static int ComputeFixed32SizeNoTag(uint value)
  930. {
  931. return LittleEndian32Size;
  932. }
  933. /// <summary>
  934. /// Compute the number of bytes that would be needed to encode a
  935. /// bool field, including the tag.
  936. /// </summary>
  937. public static int ComputeBoolSizeNoTag(bool value)
  938. {
  939. return 1;
  940. }
  941. /// <summary>
  942. /// Compute the number of bytes that would be needed to encode a
  943. /// string field, including the tag.
  944. /// </summary>
  945. public static int ComputeStringSizeNoTag(String value)
  946. {
  947. int byteArraySize = Encoding.UTF8.GetByteCount(value);
  948. return ComputeRawVarint32Size((uint) byteArraySize) +
  949. byteArraySize;
  950. }
  951. /// <summary>
  952. /// Compute the number of bytes that would be needed to encode a
  953. /// group field, including the tag.
  954. /// </summary>
  955. public static int ComputeGroupSizeNoTag(IMessageLite value)
  956. {
  957. return value.SerializedSize;
  958. }
  959. /// <summary>
  960. /// Compute the number of bytes that would be needed to encode a
  961. /// group field represented by an UnknownFieldSet, including the tag.
  962. /// </summary>
  963. [Obsolete]
  964. public static int ComputeUnknownGroupSizeNoTag(IMessageLite value)
  965. {
  966. return value.SerializedSize;
  967. }
  968. /// <summary>
  969. /// Compute the number of bytes that would be needed to encode an
  970. /// embedded message field, including the tag.
  971. /// </summary>
  972. public static int ComputeMessageSizeNoTag(IMessageLite value)
  973. {
  974. int size = value.SerializedSize;
  975. return ComputeRawVarint32Size((uint) size) + size;
  976. }
  977. /// <summary>
  978. /// Compute the number of bytes that would be needed to encode a
  979. /// bytes field, including the tag.
  980. /// </summary>
  981. public static int ComputeBytesSizeNoTag(ByteString value)
  982. {
  983. return ComputeRawVarint32Size((uint) value.Length) +
  984. value.Length;
  985. }
  986. /// <summary>
  987. /// Compute the number of bytes that would be needed to encode a
  988. /// uint32 field, including the tag.
  989. /// </summary>
  990. [CLSCompliant(false)]
  991. public static int ComputeUInt32SizeNoTag(uint value)
  992. {
  993. return ComputeRawVarint32Size(value);
  994. }
  995. /// <summary>
  996. /// Compute the number of bytes that would be needed to encode a
  997. /// enum field, including the tag. The caller is responsible for
  998. /// converting the enum value to its numeric value.
  999. /// </summary>
  1000. public static int ComputeEnumSizeNoTag(int value)
  1001. {
  1002. return ComputeRawVarint32Size((uint) value);
  1003. }
  1004. /// <summary>
  1005. /// Compute the number of bytes that would be needed to encode an
  1006. /// sfixed32 field, including the tag.
  1007. /// </summary>
  1008. public static int ComputeSFixed32SizeNoTag(int value)
  1009. {
  1010. return LittleEndian32Size;
  1011. }
  1012. /// <summary>
  1013. /// Compute the number of bytes that would be needed to encode an
  1014. /// sfixed64 field, including the tag.
  1015. /// </summary>
  1016. public static int ComputeSFixed64SizeNoTag(long value)
  1017. {
  1018. return LittleEndian64Size;
  1019. }
  1020. /// <summary>
  1021. /// Compute the number of bytes that would be needed to encode an
  1022. /// sint32 field, including the tag.
  1023. /// </summary>
  1024. public static int ComputeSInt32SizeNoTag(int value)
  1025. {
  1026. return ComputeRawVarint32Size(EncodeZigZag32(value));
  1027. }
  1028. /// <summary>
  1029. /// Compute the number of bytes that would be needed to encode an
  1030. /// sint64 field, including the tag.
  1031. /// </summary>
  1032. public static int ComputeSInt64SizeNoTag(long value)
  1033. {
  1034. return ComputeRawVarint64Size(EncodeZigZag64(value));
  1035. }
  1036. /*
  1037. * Compute the number of bytes that would be needed to encode a
  1038. * MessageSet extension to the stream. For historical reasons,
  1039. * the wire format differs from normal fields.
  1040. */
  1041. /// <summary>
  1042. /// Compute the number of bytes that would be needed to encode a
  1043. /// MessageSet extension to the stream. For historical reasons,
  1044. /// the wire format differs from normal fields.
  1045. /// </summary>
  1046. public static int ComputeMessageSetExtensionSize(int fieldNumber, IMessageLite value)
  1047. {
  1048. return ComputeTagSize(WireFormat.MessageSetField.Item)*2 +
  1049. ComputeUInt32Size(WireFormat.MessageSetField.TypeID, (uint) fieldNumber) +
  1050. ComputeMessageSize(WireFormat.MessageSetField.Message, value);
  1051. }
  1052. /// <summary>
  1053. /// Compute the number of bytes that would be needed to encode an
  1054. /// unparsed MessageSet extension field to the stream. For
  1055. /// historical reasons, the wire format differs from normal fields.
  1056. /// </summary>
  1057. public static int ComputeRawMessageSetExtensionSize(int fieldNumber, ByteString value)
  1058. {
  1059. return ComputeTagSize(WireFormat.MessageSetField.Item)*2 +
  1060. ComputeUInt32Size(WireFormat.MessageSetField.TypeID, (uint) fieldNumber) +
  1061. ComputeBytesSize(WireFormat.MessageSetField.Message, value);
  1062. }
  1063. /// <summary>
  1064. /// Compute the number of bytes that would be needed to encode a varint.
  1065. /// </summary>
  1066. [CLSCompliant(false)]
  1067. public static int ComputeRawVarint32Size(uint value)
  1068. {
  1069. if ((value & (0xffffffff << 7)) == 0) return 1;
  1070. if ((value & (0xffffffff << 14)) == 0) return 2;
  1071. if ((value & (0xffffffff << 21)) == 0) return 3;
  1072. if ((value & (0xffffffff << 28)) == 0) return 4;
  1073. return 5;
  1074. }
  1075. /// <summary>
  1076. /// Compute the number of bytes that would be needed to encode a varint.
  1077. /// </summary>
  1078. [CLSCompliant(false)]
  1079. public static int ComputeRawVarint64Size(ulong value)
  1080. {
  1081. if ((value & (0xffffffffffffffffL << 7)) == 0) return 1;
  1082. if ((value & (0xffffffffffffffffL << 14)) == 0) return 2;
  1083. if ((value & (0xffffffffffffffffL << 21)) == 0) return 3;
  1084. if ((value & (0xffffffffffffffffL << 28)) == 0) return 4;
  1085. if ((value & (0xffffffffffffffffL << 35)) == 0) return 5;
  1086. if ((value & (0xffffffffffffffffL << 42)) == 0) return 6;
  1087. if ((value & (0xffffffffffffffffL << 49)) == 0) return 7;
  1088. if ((value & (0xffffffffffffffffL << 56)) == 0) return 8;
  1089. if ((value & (0xffffffffffffffffL << 63)) == 0) return 9;
  1090. return 10;
  1091. }
  1092. /// <summary>
  1093. /// Compute the number of bytes that would be needed to encode a
  1094. /// field of arbitrary type, including the tag, to the stream.
  1095. /// </summary>
  1096. public static int ComputeFieldSize(FieldType fieldType, int fieldNumber, Object value)
  1097. {
  1098. switch (fieldType)
  1099. {
  1100. case FieldType.Double:
  1101. return ComputeDoubleSize(fieldNumber, (double) value);
  1102. case FieldType.Float:
  1103. return ComputeFloatSize(fieldNumber, (float) value);
  1104. case FieldType.Int64:
  1105. return ComputeInt64Size(fieldNumber, (long) value);
  1106. case FieldType.UInt64:
  1107. return ComputeUInt64Size(fieldNumber, (ulong) value);
  1108. case FieldType.Int32:
  1109. return ComputeInt32Size(fieldNumber, (int) value);
  1110. case FieldType.Fixed64:
  1111. return ComputeFixed64Size(fieldNumber, (ulong) value);
  1112. case FieldType.Fixed32:
  1113. return ComputeFixed32Size(fieldNumber, (uint) value);
  1114. case FieldType.Bool:
  1115. return ComputeBoolSize(fieldNumber, (bool) value);
  1116. case FieldType.String:
  1117. return ComputeStringSize(fieldNumber, (string) value);
  1118. case FieldType.Group:
  1119. return ComputeGroupSize(fieldNumber, (IMessageLite) value);
  1120. case FieldType.Message:
  1121. return ComputeMessageSize(fieldNumber, (IMessageLite) value);
  1122. case FieldType.Bytes:
  1123. return ComputeBytesSize(fieldNumber, (ByteString) value);
  1124. case FieldType.UInt32:
  1125. return ComputeUInt32Size(fieldNumber, (uint) value);
  1126. case FieldType.SFixed32:
  1127. return ComputeSFixed32Size(fieldNumber, (int) value);
  1128. case FieldType.SFixed64:
  1129. return ComputeSFixed64Size(fieldNumber, (long) value);
  1130. case FieldType.SInt32:
  1131. return ComputeSInt32Size(fieldNumber, (int) value);
  1132. case FieldType.SInt64:
  1133. return ComputeSInt64Size(fieldNumber, (long) value);
  1134. case FieldType.Enum:
  1135. return ComputeEnumSize(fieldNumber, ((IEnumLite) value).Number);
  1136. default:
  1137. throw new ArgumentOutOfRangeException("Invalid field type " + fieldType);
  1138. }
  1139. }
  1140. /// <summary>
  1141. /// Compute the number of bytes that would be needed to encode a
  1142. /// field of arbitrary type, excluding the tag, to the stream.
  1143. /// </summary>
  1144. public static int ComputeFieldSizeNoTag(FieldType fieldType, Object value)
  1145. {
  1146. switch (fieldType)
  1147. {
  1148. case FieldType.Double:
  1149. return ComputeDoubleSizeNoTag((double) value);
  1150. case FieldType.Float:
  1151. return ComputeFloatSizeNoTag((float) value);
  1152. case FieldType.Int64:
  1153. return ComputeInt64SizeNoTag((long) value);
  1154. case FieldType.UInt64:
  1155. return ComputeUInt64SizeNoTag((ulong) value);
  1156. case FieldType.Int32:
  1157. return ComputeInt32SizeNoTag((int) value);
  1158. case FieldType.Fixed64:
  1159. return ComputeFixed64SizeNoTag((ulong) value);
  1160. case FieldType.Fixed32:
  1161. return ComputeFixed32SizeNoTag((uint) value);
  1162. case FieldType.Bool:
  1163. return ComputeBoolSizeNoTag((bool) value);
  1164. case FieldType.String:
  1165. return ComputeStringSizeNoTag((string) value);
  1166. case FieldType.Group:
  1167. return ComputeGroupSizeNoTag((IMessageLite) value);
  1168. case FieldType.Message:
  1169. return ComputeMessageSizeNoTag((IMessageLite) value);
  1170. case FieldType.Bytes:
  1171. return ComputeBytesSizeNoTag((ByteString) value);
  1172. case FieldType.UInt32:
  1173. return ComputeUInt32SizeNoTag((uint) value);
  1174. case FieldType.SFixed32:
  1175. return ComputeSFixed32SizeNoTag((int) value);
  1176. case FieldType.SFixed64:
  1177. return ComputeSFixed64SizeNoTag((long) value);
  1178. case FieldType.SInt32:
  1179. return ComputeSInt32SizeNoTag((int) value);
  1180. case FieldType.SInt64:
  1181. return ComputeSInt64SizeNoTag((long) value);
  1182. case FieldType.Enum:
  1183. return ComputeEnumSizeNoTag(((IEnumLite) value).Number);
  1184. default:
  1185. throw new ArgumentOutOfRangeException("Invalid field type " + fieldType);
  1186. }
  1187. }
  1188. /// <summary>
  1189. /// Compute the number of bytes that would be needed to encode a tag.
  1190. /// </summary>
  1191. public static int ComputeTagSize(int fieldNumber)
  1192. {
  1193. return ComputeRawVarint32Size(WireFormat.MakeTag(fieldNumber, 0));
  1194. }
  1195. #endregion
  1196. /// <summary>
  1197. /// Encode a 32-bit value with ZigZag encoding.
  1198. /// </summary>
  1199. /// <remarks>
  1200. /// ZigZag encodes signed integers into values that can be efficiently
  1201. /// encoded with varint. (Otherwise, negative values must be
  1202. /// sign-extended to 64 bits to be varint encoded, thus always taking
  1203. /// 10 bytes on the wire.)
  1204. /// </remarks>
  1205. [CLSCompliant(false)]
  1206. public static uint EncodeZigZag32(int n)
  1207. {
  1208. // Note: the right-shift must be arithmetic
  1209. return (uint) ((n << 1) ^ (n >> 31));
  1210. }
  1211. /// <summary>
  1212. /// Encode a 64-bit value with ZigZag encoding.
  1213. /// </summary>
  1214. /// <remarks>
  1215. /// ZigZag encodes signed integers into values that can be efficiently
  1216. /// encoded with varint. (Otherwise, negative values must be
  1217. /// sign-extended to 64 bits to be varint encoded, thus always taking
  1218. /// 10 bytes on the wire.)
  1219. /// </remarks>
  1220. [CLSCompliant(false)]
  1221. public static ulong EncodeZigZag64(long n)
  1222. {
  1223. return (ulong) ((n << 1) ^ (n >> 63));
  1224. }
  1225. private void RefreshBuffer()
  1226. {
  1227. if (output == null)
  1228. {
  1229. // We're writing to a single buffer.
  1230. throw new OutOfSpaceException();
  1231. }
  1232. // Since we have an output stream, this is our buffer
  1233. // and buffer offset == 0
  1234. output.Write(buffer, 0, position);
  1235. position = 0;
  1236. }
  1237. /// <summary>
  1238. /// Indicates that a CodedOutputStream wrapping a flat byte array
  1239. /// ran out of space.
  1240. /// </summary>
  1241. public sealed class OutOfSpaceException : IOException
  1242. {
  1243. internal OutOfSpaceException()
  1244. : base("CodedOutputStream was writing to a flat byte array and ran out of space.")
  1245. {
  1246. }
  1247. }
  1248. public void Flush()
  1249. {
  1250. if (output != null)
  1251. {
  1252. RefreshBuffer();
  1253. }
  1254. }
  1255. /// <summary>
  1256. /// Verifies that SpaceLeft returns zero. It's common to create a byte array
  1257. /// that is exactly big enough to hold a message, then write to it with
  1258. /// a CodedOutputStream. Calling CheckNoSpaceLeft after writing verifies that
  1259. /// the message was actually as big as expected, which can help bugs.
  1260. /// </summary>
  1261. public void CheckNoSpaceLeft()
  1262. {
  1263. if (SpaceLeft != 0)
  1264. {
  1265. throw new InvalidOperationException("Did not write as much data as expected.");
  1266. }
  1267. }
  1268. /// <summary>
  1269. /// If writing to a flat array, returns the space left in the array. Otherwise,
  1270. /// throws an InvalidOperationException.
  1271. /// </summary>
  1272. public int SpaceLeft
  1273. {
  1274. get
  1275. {
  1276. if (output == null)
  1277. {
  1278. return limit - position;
  1279. }
  1280. else
  1281. {
  1282. throw new InvalidOperationException(
  1283. "SpaceLeft can only be called on CodedOutputStreams that are " +
  1284. "writing to a flat array.");
  1285. }
  1286. }
  1287. }
  1288. }
  1289. }