CodedOutputStream.cs 47 KB

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