CodedOutputStream.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2008 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using Google.Protobuf.Collections;
  33. using System;
  34. using System.IO;
  35. using System.Text;
  36. namespace Google.Protobuf
  37. {
  38. /// <summary>
  39. /// Encodes and writes protocol message fields.
  40. /// </summary>
  41. /// <remarks>
  42. /// <para>
  43. /// This class is generally used by generated code to write appropriate
  44. /// primitives to the stream. It effectively encapsulates the lowest
  45. /// levels of protocol buffer format. Unlike some other implementations,
  46. /// this does not include combined "write tag and value" methods. Generated
  47. /// code knows the exact byte representations of the tags they're going to write,
  48. /// so there's no need to re-encode them each time. Manually-written code calling
  49. /// this class should just call one of the <c>WriteTag</c> overloads before each value.
  50. /// </para>
  51. /// <para>
  52. /// Repeated fields and map fields are not handled by this class; use <c>RepeatedField&lt;T&gt;</c>
  53. /// and <c>MapField&lt;TKey, TValue&gt;</c> to serialize such fields.
  54. /// </para>
  55. /// </remarks>
  56. public sealed partial class CodedOutputStream
  57. {
  58. // "Local" copy of Encoding.UTF8, for efficiency. (Yes, it makes a difference.)
  59. internal static readonly Encoding Utf8Encoding = Encoding.UTF8;
  60. /// <summary>
  61. /// The buffer size used by CreateInstance(Stream).
  62. /// </summary>
  63. public static readonly int DefaultBufferSize = 4096;
  64. private readonly byte[] buffer;
  65. private readonly int limit;
  66. private int position;
  67. private readonly Stream output;
  68. #region Construction
  69. /// <summary>
  70. /// Creates a new CodedOutputStream that writes directly to the given
  71. /// byte array. If more bytes are written than fit in the array,
  72. /// OutOfSpaceException will be thrown.
  73. /// </summary>
  74. public CodedOutputStream(byte[] flatArray) : this(flatArray, 0, flatArray.Length)
  75. {
  76. }
  77. /// <summary>
  78. /// Creates a new CodedOutputStream that writes directly to the given
  79. /// byte array slice. If more bytes are written than fit in the array,
  80. /// OutOfSpaceException will be thrown.
  81. /// </summary>
  82. private CodedOutputStream(byte[] buffer, int offset, int length)
  83. {
  84. this.output = null;
  85. this.buffer = buffer;
  86. this.position = offset;
  87. this.limit = offset + length;
  88. }
  89. private CodedOutputStream(Stream output, byte[] buffer)
  90. {
  91. this.output = output;
  92. this.buffer = buffer;
  93. this.position = 0;
  94. this.limit = buffer.Length;
  95. }
  96. /// <summary>
  97. /// Creates a new CodedOutputStream which write to the given stream.
  98. /// </summary>
  99. public CodedOutputStream(Stream output) : this(output, DefaultBufferSize)
  100. {
  101. }
  102. /// <summary>
  103. /// Creates a new CodedOutputStream which write to the given stream and uses
  104. /// the specified buffer size.
  105. /// </summary>
  106. public CodedOutputStream(Stream output, int bufferSize) : this(output, new byte[bufferSize])
  107. {
  108. }
  109. #endregion
  110. /// <summary>
  111. /// Returns the current position in the stream, or the position in the output buffer
  112. /// </summary>
  113. public long Position
  114. {
  115. get
  116. {
  117. if (output != null)
  118. {
  119. return output.Position + position;
  120. }
  121. return position;
  122. }
  123. }
  124. #region Writing of values (not including tags)
  125. /// <summary>
  126. /// Writes a double field value, without a tag, to the stream.
  127. /// </summary>
  128. /// <param name="value">The value to write</param>
  129. public void WriteDouble(double value)
  130. {
  131. WriteRawLittleEndian64((ulong)BitConverter.DoubleToInt64Bits(value));
  132. }
  133. /// <summary>
  134. /// Writes a float field value, without a tag, to the stream.
  135. /// </summary>
  136. /// <param name="value">The value to write</param>
  137. public void WriteFloat(float value)
  138. {
  139. byte[] rawBytes = BitConverter.GetBytes(value);
  140. if (!BitConverter.IsLittleEndian)
  141. {
  142. ByteArray.Reverse(rawBytes);
  143. }
  144. if (limit - position >= 4)
  145. {
  146. buffer[position++] = rawBytes[0];
  147. buffer[position++] = rawBytes[1];
  148. buffer[position++] = rawBytes[2];
  149. buffer[position++] = rawBytes[3];
  150. }
  151. else
  152. {
  153. WriteRawBytes(rawBytes, 0, 4);
  154. }
  155. }
  156. /// <summary>
  157. /// Writes a uint64 field value, without a tag, to the stream.
  158. /// </summary>
  159. /// <param name="value">The value to write</param>
  160. public void WriteUInt64(ulong value)
  161. {
  162. WriteRawVarint64(value);
  163. }
  164. /// <summary>
  165. /// Writes an int64 field value, without a tag, to the stream.
  166. /// </summary>
  167. /// <param name="value">The value to write</param>
  168. public void WriteInt64(long value)
  169. {
  170. WriteRawVarint64((ulong) value);
  171. }
  172. /// <summary>
  173. /// Writes an int32 field value, without a tag, to the stream.
  174. /// </summary>
  175. /// <param name="value">The value to write</param>
  176. public void WriteInt32(int value)
  177. {
  178. if (value >= 0)
  179. {
  180. WriteRawVarint32((uint) value);
  181. }
  182. else
  183. {
  184. // Must sign-extend.
  185. WriteRawVarint64((ulong) value);
  186. }
  187. }
  188. /// <summary>
  189. /// Writes a fixed64 field value, without a tag, to the stream.
  190. /// </summary>
  191. /// <param name="value">The value to write</param>
  192. public void WriteFixed64(ulong value)
  193. {
  194. WriteRawLittleEndian64(value);
  195. }
  196. /// <summary>
  197. /// Writes a fixed32 field value, without a tag, to the stream.
  198. /// </summary>
  199. /// <param name="value">The value to write</param>
  200. public void WriteFixed32(uint value)
  201. {
  202. WriteRawLittleEndian32(value);
  203. }
  204. /// <summary>
  205. /// Writes a bool field value, without a tag, to the stream.
  206. /// </summary>
  207. /// <param name="value">The value to write</param>
  208. public void WriteBool(bool value)
  209. {
  210. WriteRawByte(value ? (byte) 1 : (byte) 0);
  211. }
  212. /// <summary>
  213. /// Writes a string field value, without a tag, to the stream.
  214. /// The data is length-prefixed.
  215. /// </summary>
  216. /// <param name="value">The value to write</param>
  217. public void WriteString(string value)
  218. {
  219. // Optimise the case where we have enough space to write
  220. // the string directly to the buffer, which should be common.
  221. int length = Utf8Encoding.GetByteCount(value);
  222. WriteLength(length);
  223. if (limit - position >= length)
  224. {
  225. if (length == value.Length) // Must be all ASCII...
  226. {
  227. for (int i = 0; i < length; i++)
  228. {
  229. buffer[position + i] = (byte)value[i];
  230. }
  231. }
  232. else
  233. {
  234. Utf8Encoding.GetBytes(value, 0, value.Length, buffer, position);
  235. }
  236. position += length;
  237. }
  238. else
  239. {
  240. byte[] bytes = Utf8Encoding.GetBytes(value);
  241. WriteRawBytes(bytes);
  242. }
  243. }
  244. /// <summary>
  245. /// Writes a message, without a tag, to the stream.
  246. /// The data is length-prefixed.
  247. /// </summary>
  248. /// <param name="value">The value to write</param>
  249. public void WriteMessage(IMessage value)
  250. {
  251. WriteLength(value.CalculateSize());
  252. value.WriteTo(this);
  253. }
  254. /// <summary>
  255. /// Write a byte string, without a tag, to the stream.
  256. /// The data is length-prefixed.
  257. /// </summary>
  258. /// <param name="value">The value to write</param>
  259. public void WriteBytes(ByteString value)
  260. {
  261. WriteLength(value.Length);
  262. value.WriteRawBytesTo(this);
  263. }
  264. /// <summary>
  265. /// Writes a uint32 value, without a tag, to the stream.
  266. /// </summary>
  267. /// <param name="value">The value to write</param>
  268. public void WriteUInt32(uint value)
  269. {
  270. WriteRawVarint32(value);
  271. }
  272. /// <summary>
  273. /// Writes an enum value, without a tag, to the stream.
  274. /// </summary>
  275. /// <param name="value">The value to write</param>
  276. public void WriteEnum(int value)
  277. {
  278. WriteInt32(value);
  279. }
  280. /// <summary>
  281. /// Writes an sfixed32 value, without a tag, to the stream.
  282. /// </summary>
  283. /// <param name="value">The value to write.</param>
  284. public void WriteSFixed32(int value)
  285. {
  286. WriteRawLittleEndian32((uint) value);
  287. }
  288. /// <summary>
  289. /// Writes an sfixed64 value, without a tag, to the stream.
  290. /// </summary>
  291. /// <param name="value">The value to write</param>
  292. public void WriteSFixed64(long value)
  293. {
  294. WriteRawLittleEndian64((ulong) value);
  295. }
  296. /// <summary>
  297. /// Writes an sint32 value, without a tag, to the stream.
  298. /// </summary>
  299. /// <param name="value">The value to write</param>
  300. public void WriteSInt32(int value)
  301. {
  302. WriteRawVarint32(EncodeZigZag32(value));
  303. }
  304. /// <summary>
  305. /// Writes an sint64 value, without a tag, to the stream.
  306. /// </summary>
  307. /// <param name="value">The value to write</param>
  308. public void WriteSInt64(long value)
  309. {
  310. WriteRawVarint64(EncodeZigZag64(value));
  311. }
  312. /// <summary>
  313. /// Writes a length (in bytes) for length-delimited data.
  314. /// </summary>
  315. /// <remarks>
  316. /// This method simply writes a rawint, but exists for clarity in calling code.
  317. /// </remarks>
  318. /// <param name="length">Length value, in bytes.</param>
  319. public void WriteLength(int length)
  320. {
  321. WriteRawVarint32((uint) length);
  322. }
  323. #endregion
  324. #region Raw tag writing
  325. /// <summary>
  326. /// Encodes and writes a tag.
  327. /// </summary>
  328. /// <param name="fieldNumber">The number of the field to write the tag for</param>
  329. /// <param name="type">The wire format type of the tag to write</param>
  330. public void WriteTag(int fieldNumber, WireFormat.WireType type)
  331. {
  332. WriteRawVarint32(WireFormat.MakeTag(fieldNumber, type));
  333. }
  334. /// <summary>
  335. /// Writes an already-encoded tag.
  336. /// </summary>
  337. /// <param name="tag">The encoded tag</param>
  338. public void WriteTag(uint tag)
  339. {
  340. WriteRawVarint32(tag);
  341. }
  342. /// <summary>
  343. /// Writes the given single-byte tag directly to the stream.
  344. /// </summary>
  345. /// <param name="b1">The encoded tag</param>
  346. public void WriteRawTag(byte b1)
  347. {
  348. WriteRawByte(b1);
  349. }
  350. /// <summary>
  351. /// Writes the given two-byte tag directly to the stream.
  352. /// </summary>
  353. /// <param name="b1">The first byte of the encoded tag</param>
  354. /// <param name="b2">The second byte of the encoded tag</param>
  355. public void WriteRawTag(byte b1, byte b2)
  356. {
  357. WriteRawByte(b1);
  358. WriteRawByte(b2);
  359. }
  360. /// <summary>
  361. /// Writes the given three-byte tag directly to the stream.
  362. /// </summary>
  363. /// <param name="b1">The first byte of the encoded tag</param>
  364. /// <param name="b2">The second byte of the encoded tag</param>
  365. /// <param name="b3">The third byte of the encoded tag</param>
  366. public void WriteRawTag(byte b1, byte b2, byte b3)
  367. {
  368. WriteRawByte(b1);
  369. WriteRawByte(b2);
  370. WriteRawByte(b3);
  371. }
  372. /// <summary>
  373. /// Writes the given four-byte tag directly to the stream.
  374. /// </summary>
  375. /// <param name="b1">The first byte of the encoded tag</param>
  376. /// <param name="b2">The second byte of the encoded tag</param>
  377. /// <param name="b3">The third byte of the encoded tag</param>
  378. /// <param name="b4">The fourth byte of the encoded tag</param>
  379. public void WriteRawTag(byte b1, byte b2, byte b3, byte b4)
  380. {
  381. WriteRawByte(b1);
  382. WriteRawByte(b2);
  383. WriteRawByte(b3);
  384. WriteRawByte(b4);
  385. }
  386. /// <summary>
  387. /// Writes the given five-byte tag directly to the stream.
  388. /// </summary>
  389. /// <param name="b1">The first byte of the encoded tag</param>
  390. /// <param name="b2">The second byte of the encoded tag</param>
  391. /// <param name="b3">The third byte of the encoded tag</param>
  392. /// <param name="b4">The fourth byte of the encoded tag</param>
  393. /// <param name="b5">The fifth byte of the encoded tag</param>
  394. public void WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5)
  395. {
  396. WriteRawByte(b1);
  397. WriteRawByte(b2);
  398. WriteRawByte(b3);
  399. WriteRawByte(b4);
  400. WriteRawByte(b5);
  401. }
  402. #endregion
  403. #region Underlying writing primitives
  404. /// <summary>
  405. /// Writes a 32 bit value as a varint. The fast route is taken when
  406. /// there's enough buffer space left to whizz through without checking
  407. /// for each byte; otherwise, we resort to calling WriteRawByte each time.
  408. /// </summary>
  409. internal void WriteRawVarint32(uint value)
  410. {
  411. // Optimize for the common case of a single byte value
  412. if (value < 128 && position < limit)
  413. {
  414. buffer[position++] = (byte)value;
  415. return;
  416. }
  417. while (value > 127 && position < limit)
  418. {
  419. buffer[position++] = (byte) ((value & 0x7F) | 0x80);
  420. value >>= 7;
  421. }
  422. while (value > 127)
  423. {
  424. WriteRawByte((byte) ((value & 0x7F) | 0x80));
  425. value >>= 7;
  426. }
  427. if (position < limit)
  428. {
  429. buffer[position++] = (byte) value;
  430. }
  431. else
  432. {
  433. WriteRawByte((byte) value);
  434. }
  435. }
  436. internal void WriteRawVarint64(ulong value)
  437. {
  438. while (value > 127 && position < limit)
  439. {
  440. buffer[position++] = (byte) ((value & 0x7F) | 0x80);
  441. value >>= 7;
  442. }
  443. while (value > 127)
  444. {
  445. WriteRawByte((byte) ((value & 0x7F) | 0x80));
  446. value >>= 7;
  447. }
  448. if (position < limit)
  449. {
  450. buffer[position++] = (byte) value;
  451. }
  452. else
  453. {
  454. WriteRawByte((byte) value);
  455. }
  456. }
  457. internal void WriteRawLittleEndian32(uint value)
  458. {
  459. if (position + 4 > limit)
  460. {
  461. WriteRawByte((byte) value);
  462. WriteRawByte((byte) (value >> 8));
  463. WriteRawByte((byte) (value >> 16));
  464. WriteRawByte((byte) (value >> 24));
  465. }
  466. else
  467. {
  468. buffer[position++] = ((byte) value);
  469. buffer[position++] = ((byte) (value >> 8));
  470. buffer[position++] = ((byte) (value >> 16));
  471. buffer[position++] = ((byte) (value >> 24));
  472. }
  473. }
  474. internal void WriteRawLittleEndian64(ulong value)
  475. {
  476. if (position + 8 > limit)
  477. {
  478. WriteRawByte((byte) value);
  479. WriteRawByte((byte) (value >> 8));
  480. WriteRawByte((byte) (value >> 16));
  481. WriteRawByte((byte) (value >> 24));
  482. WriteRawByte((byte) (value >> 32));
  483. WriteRawByte((byte) (value >> 40));
  484. WriteRawByte((byte) (value >> 48));
  485. WriteRawByte((byte) (value >> 56));
  486. }
  487. else
  488. {
  489. buffer[position++] = ((byte) value);
  490. buffer[position++] = ((byte) (value >> 8));
  491. buffer[position++] = ((byte) (value >> 16));
  492. buffer[position++] = ((byte) (value >> 24));
  493. buffer[position++] = ((byte) (value >> 32));
  494. buffer[position++] = ((byte) (value >> 40));
  495. buffer[position++] = ((byte) (value >> 48));
  496. buffer[position++] = ((byte) (value >> 56));
  497. }
  498. }
  499. internal void WriteRawByte(byte value)
  500. {
  501. if (position == limit)
  502. {
  503. RefreshBuffer();
  504. }
  505. buffer[position++] = value;
  506. }
  507. internal void WriteRawByte(uint value)
  508. {
  509. WriteRawByte((byte) value);
  510. }
  511. /// <summary>
  512. /// Writes out an array of bytes.
  513. /// </summary>
  514. internal void WriteRawBytes(byte[] value)
  515. {
  516. WriteRawBytes(value, 0, value.Length);
  517. }
  518. /// <summary>
  519. /// Writes out part of an array of bytes.
  520. /// </summary>
  521. internal void WriteRawBytes(byte[] value, int offset, int length)
  522. {
  523. if (limit - position >= length)
  524. {
  525. ByteArray.Copy(value, offset, buffer, position, length);
  526. // We have room in the current buffer.
  527. position += length;
  528. }
  529. else
  530. {
  531. // Write extends past current buffer. Fill the rest of this buffer and
  532. // flush.
  533. int bytesWritten = limit - position;
  534. ByteArray.Copy(value, offset, buffer, position, bytesWritten);
  535. offset += bytesWritten;
  536. length -= bytesWritten;
  537. position = limit;
  538. RefreshBuffer();
  539. // Now deal with the rest.
  540. // Since we have an output stream, this is our buffer
  541. // and buffer offset == 0
  542. if (length <= limit)
  543. {
  544. // Fits in new buffer.
  545. ByteArray.Copy(value, offset, buffer, 0, length);
  546. position = length;
  547. }
  548. else
  549. {
  550. // Write is very big. Let's do it all at once.
  551. output.Write(value, offset, length);
  552. }
  553. }
  554. }
  555. #endregion
  556. /// <summary>
  557. /// Encode a 32-bit value with ZigZag encoding.
  558. /// </summary>
  559. /// <remarks>
  560. /// ZigZag encodes signed integers into values that can be efficiently
  561. /// encoded with varint. (Otherwise, negative values must be
  562. /// sign-extended to 64 bits to be varint encoded, thus always taking
  563. /// 10 bytes on the wire.)
  564. /// </remarks>
  565. internal static uint EncodeZigZag32(int n)
  566. {
  567. // Note: the right-shift must be arithmetic
  568. return (uint) ((n << 1) ^ (n >> 31));
  569. }
  570. /// <summary>
  571. /// Encode a 64-bit value with ZigZag encoding.
  572. /// </summary>
  573. /// <remarks>
  574. /// ZigZag encodes signed integers into values that can be efficiently
  575. /// encoded with varint. (Otherwise, negative values must be
  576. /// sign-extended to 64 bits to be varint encoded, thus always taking
  577. /// 10 bytes on the wire.)
  578. /// </remarks>
  579. internal static ulong EncodeZigZag64(long n)
  580. {
  581. return (ulong) ((n << 1) ^ (n >> 63));
  582. }
  583. private void RefreshBuffer()
  584. {
  585. if (output == null)
  586. {
  587. // We're writing to a single buffer.
  588. throw new OutOfSpaceException();
  589. }
  590. // Since we have an output stream, this is our buffer
  591. // and buffer offset == 0
  592. output.Write(buffer, 0, position);
  593. position = 0;
  594. }
  595. /// <summary>
  596. /// Indicates that a CodedOutputStream wrapping a flat byte array
  597. /// ran out of space.
  598. /// </summary>
  599. public sealed class OutOfSpaceException : IOException
  600. {
  601. internal OutOfSpaceException()
  602. : base("CodedOutputStream was writing to a flat byte array and ran out of space.")
  603. {
  604. }
  605. }
  606. /// <summary>
  607. /// Flushes any buffered data to the underlying stream (if there is one).
  608. /// </summary>
  609. public void Flush()
  610. {
  611. if (output != null)
  612. {
  613. RefreshBuffer();
  614. }
  615. }
  616. /// <summary>
  617. /// Verifies that SpaceLeft returns zero. It's common to create a byte array
  618. /// that is exactly big enough to hold a message, then write to it with
  619. /// a CodedOutputStream. Calling CheckNoSpaceLeft after writing verifies that
  620. /// the message was actually as big as expected, which can help bugs.
  621. /// </summary>
  622. public void CheckNoSpaceLeft()
  623. {
  624. if (SpaceLeft != 0)
  625. {
  626. throw new InvalidOperationException("Did not write as much data as expected.");
  627. }
  628. }
  629. /// <summary>
  630. /// If writing to a flat array, returns the space left in the array. Otherwise,
  631. /// throws an InvalidOperationException.
  632. /// </summary>
  633. public int SpaceLeft
  634. {
  635. get
  636. {
  637. if (output == null)
  638. {
  639. return limit - position;
  640. }
  641. else
  642. {
  643. throw new InvalidOperationException(
  644. "SpaceLeft can only be called on CodedOutputStreams that are " +
  645. "writing to a flat array.");
  646. }
  647. }
  648. }
  649. }
  650. }