TextWriter.cs 13 KB

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