HttpWriter.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // System.Web.HttpWriter
  3. //
  4. // Author:
  5. // Patrik Torstensson ([email protected])
  6. //
  7. using System;
  8. using System.IO;
  9. using System.Text;
  10. using System.Web.Util;
  11. namespace System.Web
  12. {
  13. public sealed class HttpWriter : TextWriter
  14. {
  15. HttpResponse _Response;
  16. HttpResponseStream _ResponseStream;
  17. MemoryStream _OutputStream;
  18. StreamWriter _OutputHelper;
  19. Encoding _Encoding;
  20. Stream _OutputFilter;
  21. HttpResponseStreamProxy _OutputProxy;
  22. internal HttpWriter (HttpResponse Response)
  23. {
  24. _Response = Response;
  25. _OutputStream = new MemoryStream (32768);
  26. _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
  27. _ResponseStream = new HttpResponseStream (this);
  28. Update ();
  29. }
  30. internal void Dispose ()
  31. {
  32. _OutputHelper.Close ();
  33. _OutputStream.Close ();
  34. _OutputFilter.Close ();
  35. }
  36. internal Stream GetActiveFilter ()
  37. {
  38. if (null == _OutputFilter) {
  39. // Create a filter proxy to allow us to know if we have a valid filter
  40. if (null == _OutputProxy)
  41. _OutputProxy = new HttpResponseStreamProxy (this);
  42. return _OutputProxy;
  43. }
  44. return _OutputFilter;
  45. }
  46. internal void ActivateFilter (Stream OutputFilter)
  47. {
  48. if (null == _OutputProxy)
  49. throw new HttpException ("Invalid filter usage");
  50. _OutputFilter = OutputFilter;
  51. }
  52. internal void FilterData (bool CloseStream)
  53. {
  54. // Check if we have any filter at all
  55. if (null == _OutputFilter)
  56. return;
  57. FlushBuffers ();
  58. // Save our current data
  59. byte [] arrData = _OutputStream.GetBuffer ();
  60. int size = (int) _OutputStream.Length;
  61. // Remove our internal data
  62. Clear ();
  63. // If we have a filter then we have a proxy
  64. _OutputProxy.Active = true;
  65. try {
  66. // Call the filter (it does a callback into our HttpWriter again)
  67. _OutputFilter.Write (arrData, 0, size);
  68. _OutputFilter.Flush ();
  69. if (CloseStream)
  70. _OutputFilter.Close ();
  71. } finally {
  72. _OutputProxy.Active = false;
  73. }
  74. }
  75. internal void Clear ()
  76. {
  77. _OutputHelper.Close ();
  78. _OutputStream.Close ();
  79. // Quick way of doing cleanup
  80. _OutputStream = new MemoryStream (32768);
  81. _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
  82. }
  83. internal void SendContent (HttpWorkerRequest Handler)
  84. {
  85. FlushBuffers();
  86. int l = (int)_OutputStream.Length;
  87. if (l > 0) {
  88. byte [] arrContent = _OutputStream.GetBuffer ();
  89. Handler.SendResponseFromMemory (arrContent, l);
  90. }
  91. }
  92. internal void Update ()
  93. {
  94. _Encoding = _Response.ContentEncoding;
  95. _OutputHelper.Flush ();
  96. _OutputHelper = new StreamWriter (_OutputStream, _Encoding);
  97. }
  98. internal long BufferSize
  99. {
  100. get {
  101. FlushBuffers ();
  102. return _OutputStream.Length;
  103. }
  104. }
  105. internal void FlushBuffers ()
  106. {
  107. _OutputHelper.Flush ();
  108. _OutputStream.Flush ();
  109. }
  110. public override Encoding Encoding
  111. {
  112. get { return _Encoding; }
  113. }
  114. public Stream OutputStream
  115. {
  116. get { return _ResponseStream; }
  117. }
  118. public override void Close ()
  119. {
  120. FlushBuffers ();
  121. _Response.Flush ();
  122. _Response.Close ();
  123. }
  124. public override void Flush ()
  125. {
  126. }
  127. private void CheckIfFlush ()
  128. {
  129. if (!_Response.BufferOutput) {
  130. FlushBuffers ();
  131. _Response.Flush ();
  132. }
  133. }
  134. public override void Write (char ch)
  135. {
  136. _OutputHelper.Write (ch);
  137. CheckIfFlush ();
  138. }
  139. public override void Write (object obj)
  140. {
  141. _OutputHelper.Write (obj.ToString ());
  142. CheckIfFlush ();
  143. }
  144. public override void Write (string s)
  145. {
  146. _OutputHelper.Write (s);
  147. CheckIfFlush ();
  148. }
  149. public override void Write (char [] buffer, int index, int count)
  150. {
  151. _OutputHelper.Write (buffer, index, count);
  152. CheckIfFlush ();
  153. }
  154. public void WriteBytes (byte [] buffer, int index, int count)
  155. {
  156. _OutputStream.Write (buffer, index, count);
  157. CheckIfFlush ();
  158. }
  159. public override void WriteLine ()
  160. {
  161. _OutputHelper.Write ("\r\n");
  162. CheckIfFlush ();
  163. }
  164. public void WriteString (string s, int index, int count)
  165. {
  166. _OutputHelper.Write (s.Substring (index, count));
  167. CheckIfFlush ();
  168. }
  169. }
  170. }