JsonWriter.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. #region License
  2. // Copyright (c) 2007 James Newton-King
  3. // Copyright 2007 Konstantin Triger <[email protected]>
  4. //
  5. // Permission is hereby granted, free of charge, to any person
  6. // obtaining a copy of this software and associated documentation
  7. // files (the "Software"), to deal in the Software without
  8. // restriction, including without limitation the rights to use,
  9. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the
  11. // Software is furnished to do so, subject to the following
  12. // conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. // OTHER DEALINGS IN THE SOFTWARE.
  25. #endregion
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Text;
  29. using System.IO;
  30. using System.Xml;
  31. using Newtonsoft.Json.Utilities;
  32. using System.Collections;
  33. using System.Web.Script.Serialization;
  34. namespace Newtonsoft.Json
  35. {
  36. internal enum JsonType
  37. {
  38. Object,
  39. Array,
  40. None
  41. }
  42. /// <summary>
  43. /// Specifies the state of the <see cref="JsonWriter"/>.
  44. /// </summary>
  45. enum WriteState
  46. {
  47. /// <summary>
  48. /// An exception has been thrown, which has left the <see cref="JsonWriter"/> in an invalid state.
  49. /// You may call the <see cref="JsonWriter.Close"/> method to put the <see cref="JsonWriter"/> in the <c>Closed</c> state.
  50. /// Any other <see cref="JsonWriter"/> method calls results in an <see cref="InvalidOperationException"/> being thrown.
  51. /// </summary>
  52. Error,
  53. /// <summary>
  54. /// The <see cref="JsonWriter.Close"/> method has been called.
  55. /// </summary>
  56. Closed,
  57. /// <summary>
  58. /// An object is being written.
  59. /// </summary>
  60. Object,
  61. /// <summary>
  62. /// A array is being written.
  63. /// </summary>
  64. Array,
  65. /// <summary>
  66. /// A property is being written.
  67. /// </summary>
  68. Property,
  69. /// <summary>
  70. /// A write method has not been called.
  71. /// </summary>
  72. Start
  73. }
  74. /// <summary>
  75. /// Specifies formatting options for the <see cref="JsonWriter"/>.
  76. /// </summary>
  77. enum Formatting
  78. {
  79. /// <summary>
  80. /// No special formatting is applied. This is the default.
  81. /// </summary>
  82. None,
  83. /// <summary>
  84. /// Causes child objects to be indented according to the <see cref="JsonWriter.Indentation"/> and <see cref="JsonWriter.IndentChar"/> settings.
  85. /// </summary>
  86. Indented
  87. }
  88. /// <summary>
  89. /// Represents a writer that provides a fast, non-cached, forward-only way of generating Json data.
  90. /// </summary>
  91. sealed class JsonWriter : IDisposable
  92. {
  93. private enum State
  94. {
  95. Start,
  96. Property,
  97. ObjectStart,
  98. Object,
  99. ArrayStart,
  100. Array,
  101. Closed,
  102. Error
  103. }
  104. // array that gives a new state based on the current state an the token being written
  105. private static readonly State[,] stateArray = {
  106. // Start PropertyName ObjectStart Object ArrayStart Array Closed Error
  107. //
  108. /* None */{ State.Error, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error, State.Error },
  109. /* StartObject */{ State.ObjectStart, State.ObjectStart, State.Error, State.Error, State.ObjectStart, State.ObjectStart, State.Error, State.Error },
  110. /* StartArray */{ State.ArrayStart, State.ArrayStart, State.Error, State.Error, State.ArrayStart, State.ArrayStart, State.Error, State.Error },
  111. /* StartProperty */{ State.Error, State.Error, State.Property, State.Property, State.Error, State.Error, State.Error, State.Error },
  112. /* Comment */{ State.Error, State.Property, State.ObjectStart, State.Object, State.ArrayStart, State.Array, State.Error, State.Error },
  113. /* Value */{ State.Closed, State.Object, State.Error, State.Error, State.Array, State.Array, State.Error, State.Error },
  114. };
  115. private int _top;
  116. private List<JsonType> _stack;
  117. private Stack _serializeStack;
  118. private TextWriter _writer;
  119. private Formatting _formatting;
  120. private char _indentChar;
  121. private int _indentation;
  122. private char _quoteChar;
  123. private bool _quoteName;
  124. private State _currentState;
  125. internal sealed class Stack
  126. {
  127. readonly ArrayList _list;
  128. public Stack (JsonWriter writer) {
  129. _list = new ArrayList ();
  130. }
  131. public void Push (object value) {
  132. _list.Add (value);
  133. }
  134. public object Pop () {
  135. int index = _list.Count - 1;
  136. object item = _list [index];
  137. _list.RemoveAt (index);
  138. return item;
  139. }
  140. public bool Contains (object item) {
  141. for (int i = 0; i < _list.Count; i++)
  142. if (item == _list [i])
  143. return true;
  144. return false;
  145. }
  146. }
  147. internal Stack SerializeStack
  148. {
  149. get
  150. {
  151. if (_serializeStack == null)
  152. _serializeStack = new Stack (this);
  153. return _serializeStack;
  154. }
  155. }
  156. /// <summary>
  157. /// Gets the state of the writer.
  158. /// </summary>
  159. public WriteState WriteState
  160. {
  161. get
  162. {
  163. switch (_currentState)
  164. {
  165. case State.Error:
  166. return WriteState.Error;
  167. case State.Closed:
  168. return WriteState.Closed;
  169. case State.Object:
  170. case State.ObjectStart:
  171. return WriteState.Object;
  172. case State.Array:
  173. case State.ArrayStart:
  174. return WriteState.Array;
  175. case State.Property:
  176. return WriteState.Property;
  177. case State.Start:
  178. return WriteState.Start;
  179. default:
  180. throw new JsonWriterException("Invalid state: " + _currentState);
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// Indicates how the output is formatted.
  186. /// </summary>
  187. public Formatting Formatting
  188. {
  189. get { return _formatting; }
  190. set { _formatting = value; }
  191. }
  192. /// <summary>
  193. /// Gets or sets how many IndentChars to write for each level in the hierarchy when <paramref name="Formatting"/> is set to <c>Formatting.Indented</c>.
  194. /// </summary>
  195. public int Indentation
  196. {
  197. get { return _indentation; }
  198. set
  199. {
  200. if (value < 0)
  201. throw new ArgumentException("Indentation value must be greater than 0.");
  202. _indentation = value;
  203. }
  204. }
  205. /// <summary>
  206. /// Gets or sets which character to use to quote attribute values.
  207. /// </summary>
  208. public char QuoteChar
  209. {
  210. get { return _quoteChar; }
  211. set
  212. {
  213. if (value != '"' && value != '\'')
  214. throw new ArgumentException(@"Invalid JavaScript string quote character. Valid quote characters are ' and "".");
  215. _quoteChar = value;
  216. }
  217. }
  218. /// <summary>
  219. /// Gets or sets which character to use for indenting when <paramref name="Formatting"/> is set to <c>Formatting.Indented</c>.
  220. /// </summary>
  221. public char IndentChar
  222. {
  223. get { return _indentChar; }
  224. set { _indentChar = value; }
  225. }
  226. /// <summary>
  227. /// Gets or sets a value indicating whether object names will be surrounded with quotes.
  228. /// </summary>
  229. public bool QuoteName
  230. {
  231. get { return _quoteName; }
  232. set { _quoteName = value; }
  233. }
  234. /// <summary>
  235. /// Creates an instance of the <c>JsonWriter</c> class using the specified <see cref="TextWriter"/>.
  236. /// </summary>
  237. /// <param name="textWriter">The <c>TextWriter</c> to write to.</param>
  238. public JsonWriter(TextWriter textWriter, int maxJsonLength)
  239. {
  240. if (textWriter == null)
  241. throw new ArgumentNullException("textWriter");
  242. if (maxJsonLength > 0)
  243. textWriter = new CountingTextWriter (textWriter, maxJsonLength);
  244. _writer = textWriter;
  245. _quoteChar = '"';
  246. _quoteName = true;
  247. _indentChar = ' ';
  248. _indentation = 2;
  249. _formatting = Formatting.None;
  250. _stack = new List<JsonType>(1);
  251. _stack.Add(JsonType.None);
  252. _currentState = State.Start;
  253. }
  254. private void Push(JsonType value)
  255. {
  256. _top++;
  257. if (_stack.Count <= _top)
  258. _stack.Add(value);
  259. else
  260. _stack[_top] = value;
  261. }
  262. private JsonType Pop()
  263. {
  264. JsonType value = Peek();
  265. _top--;
  266. return value;
  267. }
  268. private JsonType Peek()
  269. {
  270. return _stack[_top];
  271. }
  272. /// <summary>
  273. /// Flushes whatever is in the buffer to the underlying streams and also flushes the underlying stream.
  274. /// </summary>
  275. public void Flush()
  276. {
  277. _writer.Flush();
  278. }
  279. /// <summary>
  280. /// Closes this stream and the underlying stream.
  281. /// </summary>
  282. public void Close()
  283. {
  284. AutoCompleteAll();
  285. _writer.Close();
  286. }
  287. /// <summary>
  288. /// Writes the beginning of a Json object.
  289. /// </summary>
  290. public void WriteStartObject()
  291. {
  292. AutoComplete(JsonToken.StartObject);
  293. Push(JsonType.Object);
  294. _writer.Write("{");
  295. }
  296. /// <summary>
  297. /// Writes the end of a Json object.
  298. /// </summary>
  299. public void WriteEndObject()
  300. {
  301. AutoCompleteClose(JsonToken.EndObject);
  302. }
  303. /// <summary>
  304. /// Writes the beginning of a Json array.
  305. /// </summary>
  306. public void WriteStartArray()
  307. {
  308. AutoComplete(JsonToken.StartArray);
  309. Push(JsonType.Array);
  310. _writer.Write("[");
  311. }
  312. /// <summary>
  313. /// Writes the end of an array.
  314. /// </summary>
  315. public void WriteEndArray()
  316. {
  317. AutoCompleteClose(JsonToken.EndArray);
  318. }
  319. /// <summary>
  320. /// Writes the property name of a name/value pair on a Json object.
  321. /// </summary>
  322. /// <param name="name"></param>
  323. public void WritePropertyName(string name)
  324. {
  325. //_objectStack.Push(new JsonObjectInfo(JsonType.Property));
  326. AutoComplete(JsonToken.PropertyName);
  327. if (_quoteName)
  328. _writer.Write(_quoteChar);
  329. _writer.Write(name);
  330. if (_quoteName)
  331. _writer.Write(_quoteChar);
  332. _writer.Write(':');
  333. }
  334. /// <summary>
  335. /// Writes the end of the current Json object or array.
  336. /// </summary>
  337. public void WriteEnd()
  338. {
  339. WriteEnd(Peek());
  340. }
  341. private void WriteEnd(JsonType type)
  342. {
  343. switch (type)
  344. {
  345. case JsonType.Object:
  346. WriteEndObject();
  347. break;
  348. case JsonType.Array:
  349. WriteEndArray();
  350. break;
  351. default:
  352. throw new JsonWriterException("Unexpected type when writing end: " + type);
  353. }
  354. }
  355. private void AutoCompleteAll()
  356. {
  357. while (_top > 0)
  358. {
  359. WriteEnd();
  360. }
  361. }
  362. private JsonType GetTypeForCloseToken(JsonToken token)
  363. {
  364. switch (token)
  365. {
  366. case JsonToken.EndObject:
  367. return JsonType.Object;
  368. case JsonToken.EndArray:
  369. return JsonType.Array;
  370. default:
  371. throw new JsonWriterException("No type for token: " + token);
  372. }
  373. }
  374. private JsonToken GetCloseTokenForType(JsonType type)
  375. {
  376. switch (type)
  377. {
  378. case JsonType.Object:
  379. return JsonToken.EndObject;
  380. case JsonType.Array:
  381. return JsonToken.EndArray;
  382. default:
  383. throw new JsonWriterException("No close token for type: " + type);
  384. }
  385. }
  386. private void AutoCompleteClose(JsonToken tokenBeingClosed)
  387. {
  388. // write closing symbol and calculate new state
  389. int levelsToComplete = 0;
  390. for (int i = 0; i < _top; i++)
  391. {
  392. int currentLevel = _top - i;
  393. if (_stack[currentLevel] == GetTypeForCloseToken(tokenBeingClosed))
  394. {
  395. levelsToComplete = i + 1;
  396. break;
  397. }
  398. }
  399. if (levelsToComplete == 0)
  400. throw new JsonWriterException("No token to close.");
  401. for (int i = 0; i < levelsToComplete; i++)
  402. {
  403. JsonToken token = GetCloseTokenForType(Pop());
  404. if (_currentState != State.ObjectStart && _currentState != State.ArrayStart)
  405. WriteIndent();
  406. switch (token)
  407. {
  408. case JsonToken.EndObject:
  409. _writer.Write("}");
  410. break;
  411. case JsonToken.EndArray:
  412. _writer.Write("]");
  413. break;
  414. default:
  415. throw new JsonWriterException("Invalid JsonToken: " + token);
  416. }
  417. }
  418. JsonType currentLevelType = Peek();
  419. switch (currentLevelType)
  420. {
  421. case JsonType.Object:
  422. _currentState = State.Object;
  423. break;
  424. case JsonType.Array:
  425. _currentState = State.Array;
  426. break;
  427. case JsonType.None:
  428. _currentState = State.Start;
  429. break;
  430. default:
  431. throw new JsonWriterException("Unknown JsonType: " + currentLevelType);
  432. }
  433. }
  434. private void WriteIndent()
  435. {
  436. if (_formatting == Formatting.Indented)
  437. {
  438. _writer.Write(Environment.NewLine);
  439. // for each level of object...
  440. for (int i = 0; i < _top; i++)
  441. {
  442. // ...write the indent char the specified number of times
  443. for (int j = 0; j < _indentation; j++)
  444. {
  445. _writer.Write(_indentChar);
  446. }
  447. }
  448. }
  449. }
  450. private void AutoComplete(JsonToken tokenBeingWritten)
  451. {
  452. int token;
  453. switch (tokenBeingWritten)
  454. {
  455. default:
  456. token = (int) tokenBeingWritten;
  457. break;
  458. case JsonToken.Integer:
  459. case JsonToken.Float:
  460. case JsonToken.String:
  461. case JsonToken.Boolean:
  462. case JsonToken.Null:
  463. case JsonToken.Undefined:
  464. case JsonToken.Date:
  465. // a value is being written
  466. token = 5;
  467. break;
  468. }
  469. // gets new state based on the current state and what is being written
  470. State newState = stateArray[token, (int) _currentState];
  471. if (newState == State.Error)
  472. throw new JsonWriterException(string.Format("Token {0} in state {1} would result in an invalid JavaScript object.", tokenBeingWritten.ToString(), _currentState.ToString()));
  473. if ((_currentState == State.Object || _currentState == State.Array) && tokenBeingWritten != JsonToken.Comment)
  474. {
  475. _writer.Write(',');
  476. }
  477. else if (_currentState == State.Property)
  478. {
  479. if (_formatting == Formatting.Indented)
  480. _writer.Write(' ');
  481. }
  482. if (tokenBeingWritten == JsonToken.PropertyName ||
  483. (WriteState == WriteState.Array))
  484. {
  485. WriteIndent();
  486. }
  487. _currentState = newState;
  488. }
  489. private void WriteValueInternal(string value, JsonToken token)
  490. {
  491. AutoComplete(token);
  492. _writer.Write(value);
  493. }
  494. #region WriteValue methods
  495. /// <summary>
  496. /// Writes a null value.
  497. /// </summary>
  498. public void WriteNull()
  499. {
  500. WriteValueInternal(JavaScriptConvert.Null, JsonToken.Null);
  501. }
  502. /// <summary>
  503. /// Writes an undefined value.
  504. /// </summary>
  505. public void WriteUndefined()
  506. {
  507. WriteValueInternal(JavaScriptConvert.Undefined, JsonToken.Undefined);
  508. }
  509. /// <summary>
  510. /// Writes raw JavaScript manually.
  511. /// </summary>
  512. /// <param name="javaScript">The raw JavaScript to write.</param>
  513. public void WriteRaw(string javaScript)
  514. {
  515. // hack. some 'raw' or 'other' token perhaps?
  516. WriteValueInternal(javaScript, JsonToken.Undefined);
  517. }
  518. /// <summary>
  519. /// Writes a <see cref="String"/> value.
  520. /// </summary>
  521. /// <param name="value">The <see cref="String"/> value to write.</param>
  522. public void WriteValue(string value)
  523. {
  524. AutoComplete (JsonToken.String);
  525. JavaScriptConvert.WriteString(value, _quoteChar, _writer);
  526. }
  527. /// <summary>
  528. /// Writes a <see cref="Int32"/> value.
  529. /// </summary>
  530. /// <param name="value">The <see cref="Int32"/> value to write.</param>
  531. public void WriteValue(int value)
  532. {
  533. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Integer);
  534. }
  535. /// <summary>
  536. /// Writes a <see cref="UInt32"/> value.
  537. /// </summary>
  538. /// <param name="value">The <see cref="UInt32"/> value to write.</param>
  539. public void WriteValue(uint value)
  540. {
  541. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Integer);
  542. }
  543. /// <summary>
  544. /// Writes a <see cref="Int64"/> value.
  545. /// </summary>
  546. /// <param name="value">The <see cref="Int64"/> value to write.</param>
  547. public void WriteValue(long value)
  548. {
  549. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Integer);
  550. }
  551. /// <summary>
  552. /// Writes a <see cref="UInt64"/> value.
  553. /// </summary>
  554. /// <param name="value">The <see cref="UInt64"/> value to write.</param>
  555. public void WriteValue(ulong value)
  556. {
  557. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Integer);
  558. }
  559. /// <summary>
  560. /// Writes a <see cref="Single"/> value.
  561. /// </summary>
  562. /// <param name="value">The <see cref="Single"/> value to write.</param>
  563. public void WriteValue(float value)
  564. {
  565. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Float);
  566. }
  567. /// <summary>
  568. /// Writes a <see cref="Double"/> value.
  569. /// </summary>
  570. /// <param name="value">The <see cref="Double"/> value to write.</param>
  571. public void WriteValue(double value)
  572. {
  573. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Float);
  574. }
  575. /// <summary>
  576. /// Writes a <see cref="Boolean"/> value.
  577. /// </summary>
  578. /// <param name="value">The <see cref="Boolean"/> value to write.</param>
  579. public void WriteValue(bool value)
  580. {
  581. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Boolean);
  582. }
  583. /// <summary>
  584. /// Writes a <see cref="Int16"/> value.
  585. /// </summary>
  586. /// <param name="value">The <see cref="Int16"/> value to write.</param>
  587. public void WriteValue(short value)
  588. {
  589. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Integer);
  590. }
  591. /// <summary>
  592. /// Writes a <see cref="UInt16"/> value.
  593. /// </summary>
  594. /// <param name="value">The <see cref="UInt16"/> value to write.</param>
  595. public void WriteValue(ushort value)
  596. {
  597. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Integer);
  598. }
  599. /// <summary>
  600. /// Writes a <see cref="Char"/> value.
  601. /// </summary>
  602. /// <param name="value">The <see cref="Char"/> value to write.</param>
  603. public void WriteValue(char value)
  604. {
  605. AutoComplete (JsonToken.Integer);
  606. JavaScriptConvert.WriteChar (value, _writer);
  607. }
  608. /// <summary>
  609. /// Writes a <see cref="Byte"/> value.
  610. /// </summary>
  611. /// <param name="value">The <see cref="Byte"/> value to write.</param>
  612. public void WriteValue(byte value)
  613. {
  614. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Integer);
  615. }
  616. /// <summary>
  617. /// Writes a <see cref="SByte"/> value.
  618. /// </summary>
  619. /// <param name="value">The <see cref="SByte"/> value to write.</param>
  620. public void WriteValue(sbyte value)
  621. {
  622. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Integer);
  623. }
  624. /// <summary>
  625. /// Writes a <see cref="Decimal"/> value.
  626. /// </summary>
  627. /// <param name="value">The <see cref="Decimal"/> value to write.</param>
  628. public void WriteValue(decimal value)
  629. {
  630. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Float);
  631. }
  632. /// <summary>
  633. /// Writes a <see cref="DateTime"/> value.
  634. /// </summary>
  635. /// <param name="value">The <see cref="DateTime"/> value to write.</param>
  636. public void WriteValue(DateTime value)
  637. {
  638. WriteValueInternal(JavaScriptConvert.ToString(value), JsonToken.Date);
  639. }
  640. #endregion
  641. /// <summary>
  642. /// Writes out a comment <code>/*...*/</code> containing the specified text.
  643. /// </summary>
  644. /// <param name="text">Text to place inside the comment.</param>
  645. public void WriteComment(string text)
  646. {
  647. AutoComplete(JsonToken.Comment);
  648. _writer.Write("/*");
  649. _writer.Write(text);
  650. _writer.Write("*/");
  651. }
  652. void IDisposable.Dispose()
  653. {
  654. Dispose(true);
  655. }
  656. private void Dispose(bool disposing)
  657. {
  658. if (WriteState != WriteState.Closed)
  659. Close();
  660. }
  661. }
  662. }