CodedOutputStream.cs 47 KB

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