CodedOutputStream.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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.Security;
  36. using System.Text;
  37. namespace Google.Protobuf
  38. {
  39. /// <summary>
  40. /// Encodes and writes protocol message fields.
  41. /// </summary>
  42. /// <remarks>
  43. /// <para>
  44. /// This class is generally used by generated code to write appropriate
  45. /// primitives to the stream. It effectively encapsulates the lowest
  46. /// levels of protocol buffer format. Unlike some other implementations,
  47. /// this does not include combined "write tag and value" methods. Generated
  48. /// code knows the exact byte representations of the tags they're going to write,
  49. /// so there's no need to re-encode them each time. Manually-written code calling
  50. /// this class should just call one of the <c>WriteTag</c> overloads before each value.
  51. /// </para>
  52. /// <para>
  53. /// Repeated fields and map fields are not handled by this class; use <c>RepeatedField&lt;T&gt;</c>
  54. /// and <c>MapField&lt;TKey, TValue&gt;</c> to serialize such fields.
  55. /// </para>
  56. /// </remarks>
  57. [SecuritySafeCritical]
  58. public sealed partial class CodedOutputStream : IDisposable
  59. {
  60. /// <summary>
  61. /// The buffer size used by CreateInstance(Stream).
  62. /// </summary>
  63. public static readonly int DefaultBufferSize = 4096;
  64. private readonly bool leaveOpen;
  65. private readonly byte[] buffer;
  66. private WriterInternalState state;
  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 = ProtoPreconditions.CheckNotNull(buffer, nameof(buffer));
  86. this.state.position = offset;
  87. this.state.limit = offset + length;
  88. WriteBufferHelper.Initialize(this, out this.state.writeBufferHelper);
  89. leaveOpen = true; // Simple way of avoiding trying to dispose of a null reference
  90. }
  91. private CodedOutputStream(Stream output, byte[] buffer, bool leaveOpen)
  92. {
  93. this.output = ProtoPreconditions.CheckNotNull(output, nameof(output));
  94. this.buffer = buffer;
  95. this.state.position = 0;
  96. this.state.limit = buffer.Length;
  97. WriteBufferHelper.Initialize(this, out this.state.writeBufferHelper);
  98. this.leaveOpen = leaveOpen;
  99. }
  100. /// <summary>
  101. /// Creates a new <see cref="CodedOutputStream" /> which write to the given stream, and disposes of that
  102. /// stream when the returned <c>CodedOutputStream</c> is disposed.
  103. /// </summary>
  104. /// <param name="output">The stream to write to. It will be disposed when the returned <c>CodedOutputStream is disposed.</c></param>
  105. public CodedOutputStream(Stream output) : this(output, DefaultBufferSize, false)
  106. {
  107. }
  108. /// <summary>
  109. /// Creates a new CodedOutputStream which write to the given stream and uses
  110. /// the specified buffer size.
  111. /// </summary>
  112. /// <param name="output">The stream to write to. It will be disposed when the returned <c>CodedOutputStream is disposed.</c></param>
  113. /// <param name="bufferSize">The size of buffer to use internally.</param>
  114. public CodedOutputStream(Stream output, int bufferSize) : this(output, new byte[bufferSize], false)
  115. {
  116. }
  117. /// <summary>
  118. /// Creates a new CodedOutputStream which write to the given stream.
  119. /// </summary>
  120. /// <param name="output">The stream to write to.</param>
  121. /// <param name="leaveOpen">If <c>true</c>, <paramref name="output"/> is left open when the returned <c>CodedOutputStream</c> is disposed;
  122. /// if <c>false</c>, the provided stream is disposed as well.</param>
  123. public CodedOutputStream(Stream output, bool leaveOpen) : this(output, DefaultBufferSize, leaveOpen)
  124. {
  125. }
  126. /// <summary>
  127. /// Creates a new CodedOutputStream which write to the given stream and uses
  128. /// the specified buffer size.
  129. /// </summary>
  130. /// <param name="output">The stream to write to.</param>
  131. /// <param name="bufferSize">The size of buffer to use internally.</param>
  132. /// <param name="leaveOpen">If <c>true</c>, <paramref name="output"/> is left open when the returned <c>CodedOutputStream</c> is disposed;
  133. /// if <c>false</c>, the provided stream is disposed as well.</param>
  134. public CodedOutputStream(Stream output, int bufferSize, bool leaveOpen) : this(output, new byte[bufferSize], leaveOpen)
  135. {
  136. }
  137. #endregion
  138. /// <summary>
  139. /// Returns the current position in the stream, or the position in the output buffer
  140. /// </summary>
  141. public long Position
  142. {
  143. get
  144. {
  145. if (output != null)
  146. {
  147. return output.Position + state.position;
  148. }
  149. return state.position;
  150. }
  151. }
  152. #region Writing of values (not including tags)
  153. /// <summary>
  154. /// Writes a double field value, without a tag, to the stream.
  155. /// </summary>
  156. /// <param name="value">The value to write</param>
  157. public void WriteDouble(double value)
  158. {
  159. var span = new Span<byte>(buffer);
  160. WritingPrimitives.WriteDouble(ref span, ref state, value);
  161. }
  162. /// <summary>
  163. /// Writes a float field value, without a tag, to the stream.
  164. /// </summary>
  165. /// <param name="value">The value to write</param>
  166. public void WriteFloat(float value)
  167. {
  168. var span = new Span<byte>(buffer);
  169. WritingPrimitives.WriteFloat(ref span, ref state, value);
  170. }
  171. /// <summary>
  172. /// Writes a uint64 field value, without a tag, to the stream.
  173. /// </summary>
  174. /// <param name="value">The value to write</param>
  175. public void WriteUInt64(ulong value)
  176. {
  177. var span = new Span<byte>(buffer);
  178. WritingPrimitives.WriteUInt64(ref span, ref state, value);
  179. }
  180. /// <summary>
  181. /// Writes an int64 field value, without a tag, to the stream.
  182. /// </summary>
  183. /// <param name="value">The value to write</param>
  184. public void WriteInt64(long value)
  185. {
  186. var span = new Span<byte>(buffer);
  187. WritingPrimitives.WriteInt64(ref span, ref state, value);
  188. }
  189. /// <summary>
  190. /// Writes an int32 field value, without a tag, to the stream.
  191. /// </summary>
  192. /// <param name="value">The value to write</param>
  193. public void WriteInt32(int value)
  194. {
  195. var span = new Span<byte>(buffer);
  196. WritingPrimitives.WriteInt32(ref span, ref state, value);
  197. }
  198. /// <summary>
  199. /// Writes a fixed64 field value, without a tag, to the stream.
  200. /// </summary>
  201. /// <param name="value">The value to write</param>
  202. public void WriteFixed64(ulong value)
  203. {
  204. var span = new Span<byte>(buffer);
  205. WritingPrimitives.WriteFixed64(ref span, ref state, value);
  206. }
  207. /// <summary>
  208. /// Writes a fixed32 field value, without a tag, to the stream.
  209. /// </summary>
  210. /// <param name="value">The value to write</param>
  211. public void WriteFixed32(uint value)
  212. {
  213. var span = new Span<byte>(buffer);
  214. WritingPrimitives.WriteFixed32(ref span, ref state, value);
  215. }
  216. /// <summary>
  217. /// Writes a bool field value, without a tag, to the stream.
  218. /// </summary>
  219. /// <param name="value">The value to write</param>
  220. public void WriteBool(bool value)
  221. {
  222. var span = new Span<byte>(buffer);
  223. WritingPrimitives.WriteBool(ref span, ref state, value);
  224. }
  225. /// <summary>
  226. /// Writes a string field value, without a tag, to the stream.
  227. /// The data is length-prefixed.
  228. /// </summary>
  229. /// <param name="value">The value to write</param>
  230. public void WriteString(string value)
  231. {
  232. var span = new Span<byte>(buffer);
  233. WritingPrimitives.WriteString(ref span, ref state, value);
  234. }
  235. /// <summary>
  236. /// Writes a message, without a tag, to the stream.
  237. /// The data is length-prefixed.
  238. /// </summary>
  239. /// <param name="value">The value to write</param>
  240. public void WriteMessage(IMessage value)
  241. {
  242. // TODO(jtattermusch): if the message doesn't implement IBufferMessage (and thus does not provide the InternalWriteTo method),
  243. // what we're doing here works fine, but could be more efficient.
  244. // For now, this inefficiency is fine, considering this is only a backward-compatibility scenario (and regenerating the code fixes it).
  245. var span = new Span<byte>(buffer);
  246. WriteContext.Initialize(ref span, ref state, out WriteContext ctx);
  247. try
  248. {
  249. WritingPrimitivesMessages.WriteMessage(ref ctx, value);
  250. }
  251. finally
  252. {
  253. ctx.CopyStateTo(this);
  254. }
  255. }
  256. /// <summary>
  257. /// Writes a message, without a tag, to the stream.
  258. /// Only the message data is written, without a length-delimiter.
  259. /// </summary>
  260. /// <param name="value">The value to write</param>
  261. public void WriteRawMessage(IMessage value)
  262. {
  263. // TODO(jtattermusch): if the message doesn't implement IBufferMessage (and thus does not provide the InternalWriteTo method),
  264. // what we're doing here works fine, but could be more efficient.
  265. // For now, this inefficiency is fine, considering this is only a backward-compatibility scenario (and regenerating the code fixes it).
  266. var span = new Span<byte>(buffer);
  267. WriteContext.Initialize(ref span, ref state, out WriteContext ctx);
  268. try
  269. {
  270. WritingPrimitivesMessages.WriteRawMessage(ref ctx, value);
  271. }
  272. finally
  273. {
  274. ctx.CopyStateTo(this);
  275. }
  276. }
  277. /// <summary>
  278. /// Writes a group, without a tag, to the stream.
  279. /// </summary>
  280. /// <param name="value">The value to write</param>
  281. public void WriteGroup(IMessage value)
  282. {
  283. var span = new Span<byte>(buffer);
  284. WriteContext.Initialize(ref span, ref state, out WriteContext ctx);
  285. try
  286. {
  287. WritingPrimitivesMessages.WriteGroup(ref ctx, value);
  288. }
  289. finally
  290. {
  291. ctx.CopyStateTo(this);
  292. }
  293. }
  294. /// <summary>
  295. /// Write a byte string, without a tag, to the stream.
  296. /// The data is length-prefixed.
  297. /// </summary>
  298. /// <param name="value">The value to write</param>
  299. public void WriteBytes(ByteString value)
  300. {
  301. var span = new Span<byte>(buffer);
  302. WritingPrimitives.WriteBytes(ref span, ref state, value);
  303. }
  304. /// <summary>
  305. /// Writes a uint32 value, without a tag, to the stream.
  306. /// </summary>
  307. /// <param name="value">The value to write</param>
  308. public void WriteUInt32(uint value)
  309. {
  310. var span = new Span<byte>(buffer);
  311. WritingPrimitives.WriteUInt32(ref span, ref state, value);
  312. }
  313. /// <summary>
  314. /// Writes an enum value, without a tag, to the stream.
  315. /// </summary>
  316. /// <param name="value">The value to write</param>
  317. public void WriteEnum(int value)
  318. {
  319. var span = new Span<byte>(buffer);
  320. WritingPrimitives.WriteEnum(ref span, ref state, value);
  321. }
  322. /// <summary>
  323. /// Writes an sfixed32 value, without a tag, to the stream.
  324. /// </summary>
  325. /// <param name="value">The value to write.</param>
  326. public void WriteSFixed32(int value)
  327. {
  328. var span = new Span<byte>(buffer);
  329. WritingPrimitives.WriteSFixed32(ref span, ref state, value);
  330. }
  331. /// <summary>
  332. /// Writes an sfixed64 value, without a tag, to the stream.
  333. /// </summary>
  334. /// <param name="value">The value to write</param>
  335. public void WriteSFixed64(long value)
  336. {
  337. var span = new Span<byte>(buffer);
  338. WritingPrimitives.WriteSFixed64(ref span, ref state, value);
  339. }
  340. /// <summary>
  341. /// Writes an sint32 value, without a tag, to the stream.
  342. /// </summary>
  343. /// <param name="value">The value to write</param>
  344. public void WriteSInt32(int value)
  345. {
  346. var span = new Span<byte>(buffer);
  347. WritingPrimitives.WriteSInt32(ref span, ref state, value);
  348. }
  349. /// <summary>
  350. /// Writes an sint64 value, without a tag, to the stream.
  351. /// </summary>
  352. /// <param name="value">The value to write</param>
  353. public void WriteSInt64(long value)
  354. {
  355. var span = new Span<byte>(buffer);
  356. WritingPrimitives.WriteSInt64(ref span, ref state, value);
  357. }
  358. /// <summary>
  359. /// Writes a length (in bytes) for length-delimited data.
  360. /// </summary>
  361. /// <remarks>
  362. /// This method simply writes a rawint, but exists for clarity in calling code.
  363. /// </remarks>
  364. /// <param name="length">Length value, in bytes.</param>
  365. public void WriteLength(int length)
  366. {
  367. var span = new Span<byte>(buffer);
  368. WritingPrimitives.WriteLength(ref span, ref state, length);
  369. }
  370. #endregion
  371. #region Raw tag writing
  372. /// <summary>
  373. /// Encodes and writes a tag.
  374. /// </summary>
  375. /// <param name="fieldNumber">The number of the field to write the tag for</param>
  376. /// <param name="type">The wire format type of the tag to write</param>
  377. public void WriteTag(int fieldNumber, WireFormat.WireType type)
  378. {
  379. var span = new Span<byte>(buffer);
  380. WritingPrimitives.WriteTag(ref span, ref state, fieldNumber, type);
  381. }
  382. /// <summary>
  383. /// Writes an already-encoded tag.
  384. /// </summary>
  385. /// <param name="tag">The encoded tag</param>
  386. public void WriteTag(uint tag)
  387. {
  388. var span = new Span<byte>(buffer);
  389. WritingPrimitives.WriteTag(ref span, ref state, tag);
  390. }
  391. /// <summary>
  392. /// Writes the given single-byte tag directly to the stream.
  393. /// </summary>
  394. /// <param name="b1">The encoded tag</param>
  395. public void WriteRawTag(byte b1)
  396. {
  397. var span = new Span<byte>(buffer);
  398. WritingPrimitives.WriteRawTag(ref span, ref state, b1);
  399. }
  400. /// <summary>
  401. /// Writes the given two-byte tag directly to the stream.
  402. /// </summary>
  403. /// <param name="b1">The first byte of the encoded tag</param>
  404. /// <param name="b2">The second byte of the encoded tag</param>
  405. public void WriteRawTag(byte b1, byte b2)
  406. {
  407. var span = new Span<byte>(buffer);
  408. WritingPrimitives.WriteRawTag(ref span, ref state, b1, b2);
  409. }
  410. /// <summary>
  411. /// Writes the given three-byte tag directly to the stream.
  412. /// </summary>
  413. /// <param name="b1">The first byte of the encoded tag</param>
  414. /// <param name="b2">The second byte of the encoded tag</param>
  415. /// <param name="b3">The third byte of the encoded tag</param>
  416. public void WriteRawTag(byte b1, byte b2, byte b3)
  417. {
  418. var span = new Span<byte>(buffer);
  419. WritingPrimitives.WriteRawTag(ref span, ref state, b1, b2, b3);
  420. }
  421. /// <summary>
  422. /// Writes the given four-byte tag directly to the stream.
  423. /// </summary>
  424. /// <param name="b1">The first byte of the encoded tag</param>
  425. /// <param name="b2">The second byte of the encoded tag</param>
  426. /// <param name="b3">The third byte of the encoded tag</param>
  427. /// <param name="b4">The fourth byte of the encoded tag</param>
  428. public void WriteRawTag(byte b1, byte b2, byte b3, byte b4)
  429. {
  430. var span = new Span<byte>(buffer);
  431. WritingPrimitives.WriteRawTag(ref span, ref state, b1, b2, b3, b4);
  432. }
  433. /// <summary>
  434. /// Writes the given five-byte tag directly to the stream.
  435. /// </summary>
  436. /// <param name="b1">The first byte of the encoded tag</param>
  437. /// <param name="b2">The second byte of the encoded tag</param>
  438. /// <param name="b3">The third byte of the encoded tag</param>
  439. /// <param name="b4">The fourth byte of the encoded tag</param>
  440. /// <param name="b5">The fifth byte of the encoded tag</param>
  441. public void WriteRawTag(byte b1, byte b2, byte b3, byte b4, byte b5)
  442. {
  443. var span = new Span<byte>(buffer);
  444. WritingPrimitives.WriteRawTag(ref span, ref state, b1, b2, b3, b4, b5);
  445. }
  446. #endregion
  447. #region Underlying writing primitives
  448. /// <summary>
  449. /// Writes a 32 bit value as a varint. The fast route is taken when
  450. /// there's enough buffer space left to whizz through without checking
  451. /// for each byte; otherwise, we resort to calling WriteRawByte each time.
  452. /// </summary>
  453. internal void WriteRawVarint32(uint value)
  454. {
  455. var span = new Span<byte>(buffer);
  456. WritingPrimitives.WriteRawVarint32(ref span, ref state, value);
  457. }
  458. internal void WriteRawVarint64(ulong value)
  459. {
  460. var span = new Span<byte>(buffer);
  461. WritingPrimitives.WriteRawVarint64(ref span, ref state, value);
  462. }
  463. internal void WriteRawLittleEndian32(uint value)
  464. {
  465. var span = new Span<byte>(buffer);
  466. WritingPrimitives.WriteRawLittleEndian32(ref span, ref state, value);
  467. }
  468. internal void WriteRawLittleEndian64(ulong value)
  469. {
  470. var span = new Span<byte>(buffer);
  471. WritingPrimitives.WriteRawLittleEndian64(ref span, ref state, value);
  472. }
  473. /// <summary>
  474. /// Writes out an array of bytes.
  475. /// </summary>
  476. internal void WriteRawBytes(byte[] value)
  477. {
  478. WriteRawBytes(value, 0, value.Length);
  479. }
  480. /// <summary>
  481. /// Writes out part of an array of bytes.
  482. /// </summary>
  483. internal void WriteRawBytes(byte[] value, int offset, int length)
  484. {
  485. var span = new Span<byte>(buffer);
  486. WritingPrimitives.WriteRawBytes(ref span, ref state, value, offset, length);
  487. }
  488. #endregion
  489. /// <summary>
  490. /// Indicates that a CodedOutputStream wrapping a flat byte array
  491. /// ran out of space.
  492. /// </summary>
  493. public sealed class OutOfSpaceException : IOException
  494. {
  495. internal OutOfSpaceException()
  496. : base("CodedOutputStream was writing to a flat byte array and ran out of space.")
  497. {
  498. }
  499. }
  500. /// <summary>
  501. /// Flushes any buffered data and optionally closes the underlying stream, if any.
  502. /// </summary>
  503. /// <remarks>
  504. /// <para>
  505. /// By default, any underlying stream is closed by this method. To configure this behaviour,
  506. /// use a constructor overload with a <c>leaveOpen</c> parameter. If this instance does not
  507. /// have an underlying stream, this method does nothing.
  508. /// </para>
  509. /// <para>
  510. /// For the sake of efficiency, calling this method does not prevent future write calls - but
  511. /// if a later write ends up writing to a stream which has been disposed, that is likely to
  512. /// fail. It is recommend that you not call any other methods after this.
  513. /// </para>
  514. /// </remarks>
  515. public void Dispose()
  516. {
  517. Flush();
  518. if (!leaveOpen)
  519. {
  520. output.Dispose();
  521. }
  522. }
  523. /// <summary>
  524. /// Flushes any buffered data to the underlying stream (if there is one).
  525. /// </summary>
  526. public void Flush()
  527. {
  528. var span = new Span<byte>(buffer);
  529. WriteBufferHelper.Flush(ref span, ref state);
  530. }
  531. /// <summary>
  532. /// Verifies that SpaceLeft returns zero. It's common to create a byte array
  533. /// that is exactly big enough to hold a message, then write to it with
  534. /// a CodedOutputStream. Calling CheckNoSpaceLeft after writing verifies that
  535. /// the message was actually as big as expected, which can help finding bugs.
  536. /// </summary>
  537. public void CheckNoSpaceLeft()
  538. {
  539. WriteBufferHelper.CheckNoSpaceLeft(ref state);
  540. }
  541. /// <summary>
  542. /// If writing to a flat array, returns the space left in the array. Otherwise,
  543. /// throws an InvalidOperationException.
  544. /// </summary>
  545. public int SpaceLeft => WriteBufferHelper.GetSpaceLeft(ref state);
  546. internal byte[] InternalBuffer => buffer;
  547. internal Stream InternalOutputStream => output;
  548. internal ref WriterInternalState InternalState => ref state;
  549. }
  550. }