TextWriter.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. //
  2. // System.IO.TextWriter.cs
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. // Paolo Molaro ([email protected])
  8. // Marek Safar ([email protected])
  9. //
  10. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  11. // Copyright 2011 Xamarin Inc.
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.Text;
  33. using System.Runtime.InteropServices;
  34. #if NET_4_5
  35. using System.Threading.Tasks;
  36. #endif
  37. namespace System.IO
  38. {
  39. [Serializable]
  40. [ComVisible (true)]
  41. #if NET_2_1
  42. public abstract class TextWriter : IDisposable {
  43. #else
  44. public abstract class TextWriter : MarshalByRefObject, IDisposable
  45. {
  46. #endif
  47. //
  48. // Null version of the TextWriter, for the `Null' instance variable
  49. //
  50. sealed class NullTextWriter : TextWriter
  51. {
  52. public override Encoding Encoding
  53. {
  54. get
  55. {
  56. return Encoding.Default;
  57. }
  58. }
  59. public override void Write (string s)
  60. {
  61. }
  62. public override void Write (char value)
  63. {
  64. }
  65. public override void Write (char[] value, int index, int count)
  66. {
  67. }
  68. }
  69. protected TextWriter ()
  70. {
  71. CoreNewLine = System.Environment.NewLine.ToCharArray ();
  72. }
  73. protected TextWriter (IFormatProvider formatProvider)
  74. {
  75. CoreNewLine = System.Environment.NewLine.ToCharArray ();
  76. internalFormatProvider = formatProvider;
  77. }
  78. protected char[] CoreNewLine;
  79. internal IFormatProvider internalFormatProvider;
  80. public static readonly TextWriter Null = new NullTextWriter ();
  81. public abstract Encoding Encoding { get; }
  82. public virtual IFormatProvider FormatProvider {
  83. get {
  84. return internalFormatProvider;
  85. }
  86. }
  87. public virtual string NewLine {
  88. get {
  89. return new string (CoreNewLine);
  90. }
  91. set {
  92. if (value == null)
  93. value = Environment.NewLine;
  94. CoreNewLine = value.ToCharArray ();
  95. }
  96. }
  97. public virtual void Close ()
  98. {
  99. Dispose (true);
  100. }
  101. protected virtual void Dispose (bool disposing)
  102. {
  103. if (disposing) {
  104. // If we are explicitly disposed, we can avoid finalization.
  105. GC.SuppressFinalize (this);
  106. }
  107. }
  108. public void Dispose ()
  109. {
  110. Dispose (true);
  111. // If we are explicitly disposed, we can avoid finalization.
  112. GC.SuppressFinalize (this);
  113. }
  114. public virtual void Flush ()
  115. {
  116. // do nothing
  117. }
  118. public static TextWriter Synchronized (TextWriter writer)
  119. {
  120. return Synchronized (writer, false);
  121. }
  122. internal static TextWriter Synchronized (TextWriter writer, bool neverClose)
  123. {
  124. if (writer == null)
  125. throw new ArgumentNullException ("writer is null");
  126. if (writer is SynchronizedWriter)
  127. return writer;
  128. return new SynchronizedWriter (writer, neverClose);
  129. }
  130. public virtual void Write (bool value)
  131. {
  132. Write (value.ToString ());
  133. }
  134. public virtual void Write (char value)
  135. {
  136. // Do nothing
  137. }
  138. public virtual void Write (char[] buffer)
  139. {
  140. if (buffer == null)
  141. return;
  142. Write (buffer, 0, buffer.Length);
  143. }
  144. public virtual void Write (decimal value)
  145. {
  146. Write (value.ToString (internalFormatProvider));
  147. }
  148. public virtual void Write (double value)
  149. {
  150. Write (value.ToString (internalFormatProvider));
  151. }
  152. public virtual void Write (int value)
  153. {
  154. Write (value.ToString (internalFormatProvider));
  155. }
  156. public virtual void Write (long value)
  157. {
  158. Write (value.ToString (internalFormatProvider));
  159. }
  160. public virtual void Write (object value)
  161. {
  162. if (value != null)
  163. Write (value.ToString ());
  164. }
  165. public virtual void Write (float value)
  166. {
  167. Write (value.ToString (internalFormatProvider));
  168. }
  169. public virtual void Write (string value)
  170. {
  171. if (value != null)
  172. Write (value.ToCharArray ());
  173. }
  174. [CLSCompliant (false)]
  175. public virtual void Write (uint value)
  176. {
  177. Write (value.ToString (internalFormatProvider));
  178. }
  179. [CLSCompliant (false)]
  180. public virtual void Write (ulong value)
  181. {
  182. Write (value.ToString (internalFormatProvider));
  183. }
  184. public virtual void Write (string format, object arg0)
  185. {
  186. Write (String.Format (format, arg0));
  187. }
  188. public virtual void Write (string format, params object[] arg)
  189. {
  190. Write (String.Format (format, arg));
  191. }
  192. public virtual void Write (char[] buffer, int index, int count)
  193. {
  194. if (buffer == null)
  195. throw new ArgumentNullException ("buffer");
  196. if (index < 0 || index > buffer.Length)
  197. throw new ArgumentOutOfRangeException ("index");
  198. // re-ordered to avoid possible integer overflow
  199. if (count < 0 || (index > buffer.Length - count))
  200. throw new ArgumentOutOfRangeException ("count");
  201. for (; count > 0; --count, ++index) {
  202. Write (buffer[index]);
  203. }
  204. }
  205. public virtual void Write (string format, object arg0, object arg1)
  206. {
  207. Write (String.Format (format, arg0, arg1));
  208. }
  209. public virtual void Write (string format, object arg0, object arg1, object arg2)
  210. {
  211. Write (String.Format (format, arg0, arg1, arg2));
  212. }
  213. public virtual void WriteLine ()
  214. {
  215. Write (CoreNewLine);
  216. }
  217. public virtual void WriteLine (bool value)
  218. {
  219. Write (value);
  220. WriteLine ();
  221. }
  222. public virtual void WriteLine (char value)
  223. {
  224. Write (value);
  225. WriteLine ();
  226. }
  227. public virtual void WriteLine (char[] buffer)
  228. {
  229. Write (buffer);
  230. WriteLine ();
  231. }
  232. public virtual void WriteLine (decimal value)
  233. {
  234. Write (value);
  235. WriteLine ();
  236. }
  237. public virtual void WriteLine (double value)
  238. {
  239. Write (value);
  240. WriteLine ();
  241. }
  242. public virtual void WriteLine (int value)
  243. {
  244. Write (value);
  245. WriteLine ();
  246. }
  247. public virtual void WriteLine (long value)
  248. {
  249. Write (value);
  250. WriteLine ();
  251. }
  252. public virtual void WriteLine (object value)
  253. {
  254. Write (value);
  255. WriteLine ();
  256. }
  257. public virtual void WriteLine (float value)
  258. {
  259. Write (value);
  260. WriteLine ();
  261. }
  262. public virtual void WriteLine (string value)
  263. {
  264. Write (value);
  265. WriteLine ();
  266. }
  267. [CLSCompliant (false)]
  268. public virtual void WriteLine (uint value)
  269. {
  270. Write (value);
  271. WriteLine ();
  272. }
  273. [CLSCompliant (false)]
  274. public virtual void WriteLine (ulong value)
  275. {
  276. Write (value);
  277. WriteLine ();
  278. }
  279. public virtual void WriteLine (string format, object arg0)
  280. {
  281. Write (format, arg0);
  282. WriteLine ();
  283. }
  284. public virtual void WriteLine (string format, params object[] arg)
  285. {
  286. Write (format, arg);
  287. WriteLine ();
  288. }
  289. public virtual void WriteLine (char[] buffer, int index, int count)
  290. {
  291. Write (buffer, index, count);
  292. WriteLine ();
  293. }
  294. public virtual void WriteLine (string format, object arg0, object arg1)
  295. {
  296. Write (format, arg0, arg1);
  297. WriteLine ();
  298. }
  299. public virtual void WriteLine (string format, object arg0, object arg1, object arg2)
  300. {
  301. Write (format, arg0, arg1, arg2);
  302. WriteLine ();
  303. }
  304. #if NET_4_5
  305. public virtual Task FlushAsync ()
  306. {
  307. return Task.Factory.StartNew (l => ((TextWriter)l).Flush (), this);
  308. }
  309. //
  310. // Use tuple to pack the arguments because it's faster than
  311. // setting up anonymous method container with an instance delegate
  312. //
  313. public virtual Task WriteAsync (char value)
  314. {
  315. return Task.Factory.StartNew (l => {
  316. var t = (Tuple<TextWriter, char>) l;
  317. t.Item1.Write (t.Item2);
  318. }, Tuple.Create (this, value));
  319. }
  320. public Task WriteAsync (char[] buffer)
  321. {
  322. if (buffer == null)
  323. return TaskConstants.Finished;
  324. return WriteAsync (buffer, 0, buffer.Length);
  325. }
  326. public virtual Task WriteAsync (char[] buffer, int index, int count)
  327. {
  328. return Task.Factory.StartNew (l => {
  329. var t = (Tuple<TextWriter, char[], int, int>) l;
  330. t.Item1.Write (t.Item2, t.Item3, t.Item4);
  331. }, Tuple.Create (this, buffer, index, count));
  332. }
  333. public virtual Task WriteAsync (string value)
  334. {
  335. return Task.Factory.StartNew (l => {
  336. var t = (Tuple<TextWriter, string>) l;
  337. t.Item1.Write (t.Item2);
  338. }, Tuple.Create (this, value));
  339. }
  340. public virtual Task WriteLineAsync ()
  341. {
  342. return WriteAsync (CoreNewLine);
  343. }
  344. public virtual Task WriteLineAsync (char value)
  345. {
  346. return Task.Factory.StartNew (l => {
  347. var t = (Tuple<TextWriter, char>) l;
  348. t.Item1.WriteLine (t.Item2);
  349. }, Tuple.Create (this, value));
  350. }
  351. public Task WriteLineAsync (char[] buffer)
  352. {
  353. return Task.Factory.StartNew (l => {
  354. var t = (Tuple<TextWriter, char[]>) l;
  355. t.Item1.WriteLine (t.Item2);
  356. }, Tuple.Create (this, buffer));
  357. }
  358. public virtual Task WriteLineAsync (char[] buffer, int index, int count)
  359. {
  360. return Task.Factory.StartNew (l => {
  361. var t = (Tuple<TextWriter, char[], int, int>) l;
  362. t.Item1.WriteLine (t.Item2, t.Item3, t.Item4);
  363. }, Tuple.Create (this, buffer, index, count));
  364. }
  365. public virtual Task WriteLineAsync (string value)
  366. {
  367. return Task.Factory.StartNew (l => {
  368. var t = (Tuple<TextWriter, string>) l;
  369. t.Item1.WriteLine (t.Item2);
  370. }, Tuple.Create (this, value));
  371. }
  372. #endif
  373. }
  374. //
  375. // Sychronized version of the TextWriter.
  376. //
  377. [Serializable]
  378. sealed class SynchronizedWriter : TextWriter
  379. {
  380. private TextWriter writer;
  381. private bool neverClose;
  382. public SynchronizedWriter (TextWriter writer)
  383. : this (writer, false)
  384. {
  385. }
  386. public SynchronizedWriter (TextWriter writer, bool neverClose)
  387. {
  388. this.writer = writer;
  389. this.neverClose = neverClose;
  390. }
  391. public override void Close ()
  392. {
  393. if (neverClose)
  394. return;
  395. lock (this) {
  396. writer.Close ();
  397. }
  398. }
  399. public override void Flush ()
  400. {
  401. lock (this) {
  402. writer.Flush ();
  403. }
  404. }
  405. #region Write methods
  406. public override void Write (bool value)
  407. {
  408. lock (this) {
  409. writer.Write (value);
  410. }
  411. }
  412. public override void Write (char value)
  413. {
  414. lock (this) {
  415. writer.Write (value);
  416. }
  417. }
  418. public override void Write (char[] value)
  419. {
  420. lock (this) {
  421. writer.Write (value);
  422. }
  423. }
  424. public override void Write (Decimal value)
  425. {
  426. lock (this) {
  427. writer.Write (value);
  428. }
  429. }
  430. public override void Write (int value)
  431. {
  432. lock (this) {
  433. writer.Write (value);
  434. }
  435. }
  436. public override void Write (long value)
  437. {
  438. lock (this) {
  439. writer.Write (value);
  440. }
  441. }
  442. public override void Write (object value)
  443. {
  444. lock (this) {
  445. writer.Write (value);
  446. }
  447. }
  448. public override void Write (float value)
  449. {
  450. lock (this) {
  451. writer.Write (value);
  452. }
  453. }
  454. public override void Write (string value)
  455. {
  456. lock (this) {
  457. writer.Write (value);
  458. }
  459. }
  460. public override void Write (uint value)
  461. {
  462. lock (this) {
  463. writer.Write (value);
  464. }
  465. }
  466. public override void Write (ulong value)
  467. {
  468. lock (this) {
  469. writer.Write (value);
  470. }
  471. }
  472. public override void Write (string format, object value)
  473. {
  474. lock (this) {
  475. writer.Write (format, value);
  476. }
  477. }
  478. public override void Write (string format, object[] value)
  479. {
  480. lock (this) {
  481. writer.Write (format, value);
  482. }
  483. }
  484. public override void Write (char[] buffer, int index, int count)
  485. {
  486. lock (this) {
  487. writer.Write (buffer, index, count);
  488. }
  489. }
  490. public override void Write (string format, object arg0, object arg1)
  491. {
  492. lock (this) {
  493. writer.Write (format, arg0, arg1);
  494. }
  495. }
  496. public override void Write (string format, object arg0, object arg1, object arg2)
  497. {
  498. lock (this) {
  499. writer.Write (format, arg0, arg1, arg2);
  500. }
  501. }
  502. #endregion
  503. #region WriteLine methods
  504. public override void WriteLine ()
  505. {
  506. lock (this) {
  507. writer.WriteLine ();
  508. }
  509. }
  510. public override void WriteLine (bool value)
  511. {
  512. lock (this) {
  513. writer.WriteLine (value);
  514. }
  515. }
  516. public override void WriteLine (char value)
  517. {
  518. lock (this) {
  519. writer.WriteLine (value);
  520. }
  521. }
  522. public override void WriteLine (char[] value)
  523. {
  524. lock (this) {
  525. writer.WriteLine (value);
  526. }
  527. }
  528. public override void WriteLine (Decimal value)
  529. {
  530. lock (this) {
  531. writer.WriteLine (value);
  532. }
  533. }
  534. public override void WriteLine (double value)
  535. {
  536. lock (this) {
  537. writer.WriteLine (value);
  538. }
  539. }
  540. public override void WriteLine (int value)
  541. {
  542. lock (this) {
  543. writer.WriteLine (value);
  544. }
  545. }
  546. public override void WriteLine (long value)
  547. {
  548. lock (this) {
  549. writer.WriteLine (value);
  550. }
  551. }
  552. public override void WriteLine (object value)
  553. {
  554. lock (this) {
  555. writer.WriteLine (value);
  556. }
  557. }
  558. public override void WriteLine (float value)
  559. {
  560. lock (this) {
  561. writer.WriteLine (value);
  562. }
  563. }
  564. public override void WriteLine (string value)
  565. {
  566. lock (this) {
  567. writer.WriteLine (value);
  568. }
  569. }
  570. public override void WriteLine (uint value)
  571. {
  572. lock (this) {
  573. writer.WriteLine (value);
  574. }
  575. }
  576. public override void WriteLine (ulong value)
  577. {
  578. lock (this) {
  579. writer.WriteLine (value);
  580. }
  581. }
  582. public override void WriteLine (string format, object value)
  583. {
  584. lock (this) {
  585. writer.WriteLine (format, value);
  586. }
  587. }
  588. public override void WriteLine (string format, object[] value)
  589. {
  590. lock (this) {
  591. writer.WriteLine (format, value);
  592. }
  593. }
  594. public override void WriteLine (char[] buffer, int index, int count)
  595. {
  596. lock (this) {
  597. writer.WriteLine (buffer, index, count);
  598. }
  599. }
  600. public override void WriteLine (string format, object arg0, object arg1)
  601. {
  602. lock (this) {
  603. writer.WriteLine (format, arg0, arg1);
  604. }
  605. }
  606. public override void WriteLine (string format, object arg0, object arg1, object arg2)
  607. {
  608. lock (this) {
  609. writer.WriteLine (format, arg0, arg1, arg2);
  610. }
  611. }
  612. #endregion
  613. #if NET_4_5
  614. public override Task FlushAsync ()
  615. {
  616. lock (this) {
  617. return writer.FlushAsync ();
  618. }
  619. }
  620. public override Task WriteAsync (char value)
  621. {
  622. lock (this) {
  623. return writer.WriteAsync (value);
  624. }
  625. }
  626. public override Task WriteAsync (char[] buffer, int index, int count)
  627. {
  628. lock (this) {
  629. return writer.WriteAsync (buffer, index, count);
  630. }
  631. }
  632. public override Task WriteAsync (string value)
  633. {
  634. lock (this) {
  635. return writer.WriteAsync (value);
  636. }
  637. }
  638. public override Task WriteLineAsync ()
  639. {
  640. lock (this) {
  641. return writer.WriteLineAsync ();
  642. }
  643. }
  644. public override Task WriteLineAsync (char value)
  645. {
  646. lock (this) {
  647. return writer.WriteLineAsync (value);
  648. }
  649. }
  650. public override Task WriteLineAsync (char[] buffer, int index, int count)
  651. {
  652. lock (this) {
  653. return writer.WriteLineAsync (buffer, index, count);
  654. }
  655. }
  656. public override Task WriteLineAsync (string value)
  657. {
  658. lock (this) {
  659. return writer.WriteLineAsync (value);
  660. }
  661. }
  662. #endif
  663. public override Encoding Encoding {
  664. get {
  665. lock (this) {
  666. return writer.Encoding;
  667. }
  668. }
  669. }
  670. public override IFormatProvider FormatProvider {
  671. get {
  672. lock (this) {
  673. return writer.FormatProvider;
  674. }
  675. }
  676. }
  677. public override string NewLine {
  678. get {
  679. lock (this) {
  680. return writer.NewLine;
  681. }
  682. }
  683. set {
  684. lock (this) {
  685. writer.NewLine = value;
  686. }
  687. }
  688. }
  689. }
  690. }