2
0

TextWriter.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. //
  2. // System.IO.TextWriter
  3. //
  4. // Authors:
  5. // Marcin Szczepanski ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. //
  8. using System.Text;
  9. namespace System.IO {
  10. [Serializable]
  11. public abstract class TextWriter : MarshalByRefObject, IDisposable {
  12. protected TextWriter() {
  13. CoreNewLine = "\n".ToCharArray ();
  14. }
  15. protected TextWriter( IFormatProvider formatProvider ) {
  16. internalFormatProvider = formatProvider;
  17. }
  18. protected char[] CoreNewLine;
  19. internal IFormatProvider internalFormatProvider;
  20. public static readonly TextWriter Null = new NullTextWriter ();
  21. public abstract Encoding Encoding { get; }
  22. public virtual IFormatProvider FormatProvider {
  23. get {
  24. return internalFormatProvider;
  25. }
  26. }
  27. public virtual string NewLine {
  28. get {
  29. return new String(CoreNewLine);
  30. }
  31. set {
  32. CoreNewLine = value.ToCharArray();
  33. }
  34. }
  35. public virtual void Close () {
  36. Dispose (true);
  37. }
  38. protected virtual void Dispose (bool disposing) { }
  39. void System.IDisposable.Dispose () {
  40. Dispose (true);
  41. }
  42. public virtual void Flush()
  43. {
  44. // do nothing
  45. }
  46. public static TextWriter Synchronized (TextWriter writer)
  47. {
  48. if (writer == null)
  49. throw new ArgumentNullException ("writer is null");
  50. if (writer is SynchronizedWriter)
  51. return writer;
  52. return new SynchronizedWriter (writer);
  53. }
  54. public virtual void Write (bool value)
  55. {
  56. Write (value.ToString ());
  57. }
  58. public virtual void Write (char value)
  59. {
  60. Write (value.ToString (internalFormatProvider));
  61. }
  62. public virtual void Write (char[] value)
  63. {
  64. if (value != null)
  65. Write (new String (value));
  66. }
  67. public virtual void Write (decimal value)
  68. {
  69. Write (value.ToString (internalFormatProvider));
  70. }
  71. public virtual void Write (double value)
  72. {
  73. Write (value.ToString (internalFormatProvider));
  74. }
  75. public virtual void Write (int value)
  76. {
  77. Write (value.ToString (internalFormatProvider));
  78. }
  79. public virtual void Write (long value)
  80. {
  81. Write (value.ToString (internalFormatProvider));
  82. }
  83. public virtual void Write (object value)
  84. {
  85. if (value != null)
  86. Write (value.ToString ());
  87. }
  88. public virtual void Write (float value)
  89. {
  90. Write (value.ToString (internalFormatProvider));
  91. }
  92. public virtual void Write (string value)
  93. {
  94. // do nothing
  95. }
  96. [CLSCompliant(false)]
  97. public virtual void Write (uint value)
  98. {
  99. Write (value.ToString (internalFormatProvider));
  100. }
  101. [CLSCompliant(false)]
  102. public virtual void Write (ulong value)
  103. {
  104. Write (value.ToString (internalFormatProvider));
  105. }
  106. public virtual void Write (string format, object arg0)
  107. {
  108. Write (String.Format (format, arg0));
  109. }
  110. public virtual void Write (string format, params object[] arg)
  111. {
  112. Write (String.Format (format, arg));
  113. }
  114. public virtual void Write (char[] buffer, int index, int count)
  115. {
  116. Write (new String (buffer, index, count));
  117. }
  118. public virtual void Write (string format, object arg0, object arg1)
  119. {
  120. Write (String.Format (format, arg0, arg1));
  121. }
  122. public virtual void Write (string format, object arg0, object arg1, object arg2 )
  123. {
  124. Write (String.Format (format, arg0, arg1, arg2));
  125. }
  126. public virtual void WriteLine ()
  127. {
  128. Write (NewLine);
  129. }
  130. public virtual void WriteLine (bool value)
  131. {
  132. Write (value);
  133. WriteLine();
  134. }
  135. public virtual void WriteLine (char value)
  136. {
  137. Write (value);
  138. WriteLine();
  139. }
  140. public virtual void WriteLine (char[] value)
  141. {
  142. Write (value);
  143. WriteLine();
  144. }
  145. public virtual void WriteLine (decimal value)
  146. {
  147. Write (value);
  148. WriteLine();
  149. }
  150. public virtual void WriteLine (double value)
  151. {
  152. Write (value);
  153. WriteLine();
  154. }
  155. public virtual void WriteLine (int value)
  156. {
  157. Write (value);
  158. WriteLine();
  159. }
  160. public virtual void WriteLine (long value)
  161. {
  162. Write (value);
  163. WriteLine();
  164. }
  165. public virtual void WriteLine (object value)
  166. {
  167. Write (value);
  168. WriteLine();
  169. }
  170. public virtual void WriteLine (float value)
  171. {
  172. Write (value);
  173. WriteLine();
  174. }
  175. public virtual void WriteLine (string value)
  176. {
  177. Write (value);
  178. WriteLine();
  179. }
  180. [CLSCompliant(false)]
  181. public virtual void WriteLine (uint value)
  182. {
  183. Write (value);
  184. WriteLine();
  185. }
  186. [CLSCompliant(false)]
  187. public virtual void WriteLine (ulong value)
  188. {
  189. Write (value);
  190. WriteLine();
  191. }
  192. public virtual void WriteLine (string format, object arg0)
  193. {
  194. Write (format, arg0);
  195. WriteLine();
  196. }
  197. public virtual void WriteLine (string format, params object[] arg)
  198. {
  199. Write (format, arg);
  200. WriteLine();
  201. }
  202. public virtual void WriteLine (char[] buffer, int index, int count)
  203. {
  204. Write (buffer, index, count);
  205. WriteLine();
  206. }
  207. public virtual void WriteLine (string format, object arg0, object arg1)
  208. {
  209. Write (format, arg0, arg1);
  210. WriteLine();
  211. }
  212. public virtual void WriteLine (string format, object arg0, object arg1, object arg2)
  213. {
  214. Write (format, arg0, arg1, arg2);
  215. WriteLine();
  216. }
  217. //
  218. // Null version of the TextWriter, for the `Null' instance variable
  219. //
  220. sealed class NullTextWriter : TextWriter {
  221. public override Encoding Encoding {
  222. get {
  223. return Encoding.Default;
  224. }
  225. }
  226. public override void Write (string s)
  227. {
  228. }
  229. }
  230. }
  231. //
  232. // Sychronized version of the TextWriter.
  233. //
  234. [Serializable]
  235. internal class SynchronizedWriter : TextWriter {
  236. private TextWriter writer;
  237. public SynchronizedWriter (TextWriter writer)
  238. {
  239. this.writer = writer;
  240. }
  241. public override void Close ()
  242. {
  243. lock (this){
  244. writer.Close ();
  245. }
  246. }
  247. public override void Flush ()
  248. {
  249. lock (this){
  250. writer.Flush ();
  251. }
  252. }
  253. #region Write methods
  254. public override void Write (bool value)
  255. {
  256. lock (this){
  257. writer.Write (value);
  258. }
  259. }
  260. public override void Write (char value)
  261. {
  262. lock (this){
  263. writer.Write (value);
  264. }
  265. }
  266. public override void Write (char [] value)
  267. {
  268. lock (this){
  269. writer.Write (value);
  270. }
  271. }
  272. public override void Write (Decimal value)
  273. {
  274. lock (this){
  275. writer.Write (value);
  276. }
  277. }
  278. public override void Write (int value)
  279. {
  280. lock (this){
  281. writer.Write (value);
  282. }
  283. }
  284. public override void Write (long value)
  285. {
  286. lock (this){
  287. writer.Write (value);
  288. }
  289. }
  290. public override void Write (object value)
  291. {
  292. lock (this){
  293. writer.Write (value);
  294. }
  295. }
  296. public override void Write (float value)
  297. {
  298. lock (this){
  299. writer.Write (value);
  300. }
  301. }
  302. public override void Write (string value)
  303. {
  304. lock (this){
  305. writer.Write (value);
  306. }
  307. }
  308. [CLSCompliant(false)]
  309. public override void Write (uint value)
  310. {
  311. lock (this){
  312. writer.Write (value);
  313. }
  314. }
  315. [CLSCompliant(false)]
  316. public override void Write (ulong value)
  317. {
  318. lock (this){
  319. writer.Write (value);
  320. }
  321. }
  322. public override void Write (string format, object value)
  323. {
  324. lock (this){
  325. writer.Write (format, value);
  326. }
  327. }
  328. public override void Write (string format, object [] value)
  329. {
  330. lock (this){
  331. writer.Write (format, value);
  332. }
  333. }
  334. public override void Write (char [] buffer, int index, int count)
  335. {
  336. lock (this){
  337. writer.Write (buffer, index, count);
  338. }
  339. }
  340. public override void Write (string format, object arg0, object arg1)
  341. {
  342. lock (this){
  343. writer.Write (format, arg0, arg1);
  344. }
  345. }
  346. public override void Write (string format, object arg0, object arg1, object arg2)
  347. {
  348. lock (this){
  349. writer.Write (format, arg0, arg1, arg2);
  350. }
  351. }
  352. #endregion
  353. #region WriteLine methods
  354. public override void WriteLine ()
  355. {
  356. lock (this){
  357. writer.WriteLine ();
  358. }
  359. }
  360. public override void WriteLine (bool value)
  361. {
  362. lock (this){
  363. writer.WriteLine (value);
  364. }
  365. }
  366. public override void WriteLine (char value)
  367. {
  368. lock (this){
  369. writer.WriteLine (value);
  370. }
  371. }
  372. public override void WriteLine (char [] value)
  373. {
  374. lock (this){
  375. writer.WriteLine (value);
  376. }
  377. }
  378. public override void WriteLine (Decimal value)
  379. {
  380. lock (this){
  381. writer.WriteLine (value);
  382. }
  383. }
  384. public override void WriteLine (double value)
  385. {
  386. lock (this){
  387. writer.WriteLine (value);
  388. }
  389. }
  390. public override void WriteLine (int value)
  391. {
  392. lock (this){
  393. writer.WriteLine (value);
  394. }
  395. }
  396. public override void WriteLine (long value)
  397. {
  398. lock (this){
  399. writer.WriteLine (value);
  400. }
  401. }
  402. public override void WriteLine (object value)
  403. {
  404. lock (this){
  405. writer.WriteLine (value);
  406. }
  407. }
  408. public override void WriteLine (float value)
  409. {
  410. lock (this){
  411. writer.WriteLine (value);
  412. }
  413. }
  414. public override void WriteLine (string value)
  415. {
  416. lock (this){
  417. writer.WriteLine (value);
  418. }
  419. }
  420. [CLSCompliant(false)]
  421. public override void WriteLine (uint value)
  422. {
  423. lock (this){
  424. writer.WriteLine (value);
  425. }
  426. }
  427. [CLSCompliant(false)]
  428. public override void WriteLine (ulong value)
  429. {
  430. lock (this){
  431. writer.WriteLine (value);
  432. }
  433. }
  434. public override void WriteLine (string format, object value)
  435. {
  436. lock (this){
  437. writer.WriteLine (format, value);
  438. }
  439. }
  440. public override void WriteLine (string format, object [] value)
  441. {
  442. lock (this){
  443. writer.WriteLine (format, value);
  444. }
  445. }
  446. public override void WriteLine (char [] buffer, int index, int count)
  447. {
  448. lock (this){
  449. writer.WriteLine (buffer, index, count);
  450. }
  451. }
  452. public override void WriteLine (string format, object arg0, object arg1)
  453. {
  454. lock (this){
  455. writer.WriteLine (format, arg0, arg1);
  456. }
  457. }
  458. public override void WriteLine (string format, object arg0, object arg1, object arg2)
  459. {
  460. lock (this){
  461. writer.WriteLine (format, arg0, arg1, arg2);
  462. }
  463. }
  464. #endregion
  465. public override Encoding Encoding {
  466. get {
  467. lock (this){
  468. return writer.Encoding;
  469. }
  470. }
  471. }
  472. public override IFormatProvider FormatProvider {
  473. get {
  474. lock (this){
  475. return writer.FormatProvider;
  476. }
  477. }
  478. }
  479. public override string NewLine {
  480. get {
  481. lock (this){
  482. return writer.NewLine;
  483. }
  484. }
  485. set {
  486. lock (this){
  487. writer.NewLine = value;
  488. }
  489. }
  490. }
  491. }
  492. }