HttpWriter.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // System.Web.HttpWriter
  3. //
  4. // Author:
  5. // Patrik Torstensson ([email protected])
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.IO;
  29. using System.Text;
  30. using System.Web.Util;
  31. namespace System.Web
  32. {
  33. public sealed class HttpWriter : TextWriter
  34. {
  35. internal const int MaxBufferSize = 32768;
  36. HttpResponse _Response;
  37. HttpResponseStream _ResponseStream;
  38. MemoryStream _OutputStream;
  39. StreamWriter _OutputHelper;
  40. Encoding _Encoding;
  41. Stream _OutputFilter;
  42. HttpResponseStreamProxy _OutputProxy;
  43. internal HttpWriter (HttpResponse Response)
  44. {
  45. _Response = Response;
  46. _OutputStream = new MemoryStream (MaxBufferSize);
  47. _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
  48. _ResponseStream = new HttpResponseStream (this);
  49. Update ();
  50. }
  51. internal void Dispose ()
  52. {
  53. _OutputHelper.Close ();
  54. _OutputStream.Close ();
  55. _OutputFilter.Close ();
  56. }
  57. internal Stream GetActiveFilter ()
  58. {
  59. if (null == _OutputFilter) {
  60. // Create a filter proxy to allow us to know if we have a valid filter
  61. if (null == _OutputProxy)
  62. _OutputProxy = new HttpResponseStreamProxy (this);
  63. return _OutputProxy;
  64. }
  65. return _OutputFilter;
  66. }
  67. internal void ActivateFilter (Stream OutputFilter)
  68. {
  69. if (null == _OutputProxy)
  70. throw new HttpException ("Invalid filter usage");
  71. _OutputFilter = OutputFilter;
  72. }
  73. internal void FilterData (bool CloseStream)
  74. {
  75. // Check if we have any filter at all
  76. if (null == _OutputFilter)
  77. return;
  78. FlushBuffers ();
  79. // Save our current data
  80. byte [] arrData = _OutputStream.GetBuffer ();
  81. int size = (int) _OutputStream.Length;
  82. // Remove our internal data
  83. Clear ();
  84. // If we have a filter then we have a proxy
  85. _OutputProxy.Active = true;
  86. try {
  87. // Call the filter (it does a callback into our HttpWriter again)
  88. _OutputFilter.Write (arrData, 0, size);
  89. _OutputFilter.Flush ();
  90. if (CloseStream)
  91. _OutputFilter.Close ();
  92. } finally {
  93. _OutputProxy.Active = false;
  94. }
  95. }
  96. internal void Clear ()
  97. {
  98. _OutputHelper.Close ();
  99. _OutputStream.Close ();
  100. _OutputStream = new MemoryStream (32768);
  101. _OutputHelper = new StreamWriter (_OutputStream, _Response.ContentEncoding);
  102. }
  103. internal void SendContent (HttpWorkerRequest Handler)
  104. {
  105. FlushBuffers();
  106. int l = (int)_OutputStream.Length;
  107. if (l > 0) {
  108. byte [] arrContent = _OutputStream.GetBuffer ();
  109. Handler.SendResponseFromMemory (arrContent, l);
  110. }
  111. }
  112. internal void Update ()
  113. {
  114. _Encoding = _Response.ContentEncoding;
  115. _OutputHelper.Flush ();
  116. _OutputHelper = new StreamWriter (_OutputStream, _Encoding);
  117. }
  118. internal long BufferSize
  119. {
  120. get {
  121. FlushBuffers ();
  122. return _OutputStream.Length;
  123. }
  124. }
  125. internal void FlushBuffers ()
  126. {
  127. _OutputHelper.Flush ();
  128. _OutputStream.Flush ();
  129. }
  130. internal byte[] GetBuffer () {
  131. return _OutputStream.GetBuffer ();
  132. }
  133. public override Encoding Encoding
  134. {
  135. get { return _Encoding; }
  136. }
  137. public Stream OutputStream
  138. {
  139. get { return _ResponseStream; }
  140. }
  141. public override void Close ()
  142. {
  143. _Response.Flush ();
  144. _Response.Close ();
  145. }
  146. public override void Flush ()
  147. {
  148. }
  149. private void CheckIfFlush ()
  150. {
  151. if (!_Response.BufferOutput) {
  152. FlushBuffers ();
  153. }
  154. }
  155. public override void Write (char ch)
  156. {
  157. _OutputHelper.Write (ch);
  158. CheckIfFlush ();
  159. }
  160. public override void Write (object obj)
  161. {
  162. if (obj != null)
  163. {
  164. _OutputHelper.Write (obj.ToString ());
  165. CheckIfFlush ();
  166. }
  167. }
  168. public override void Write (string s)
  169. {
  170. _OutputHelper.Write (s);
  171. CheckIfFlush ();
  172. }
  173. public override void Write (char [] buffer, int index, int count)
  174. {
  175. _OutputHelper.Write (buffer, index, count);
  176. CheckIfFlush ();
  177. }
  178. public void WriteBytes (byte [] buffer, int index, int count)
  179. {
  180. _OutputStream.Write (buffer, index, count);
  181. CheckIfFlush ();
  182. }
  183. public override void WriteLine ()
  184. {
  185. _OutputHelper.Write ("\r\n");
  186. CheckIfFlush ();
  187. }
  188. public void WriteString (string s, int index, int count)
  189. {
  190. _OutputHelper.Write (s.Substring (index, count));
  191. CheckIfFlush ();
  192. }
  193. }
  194. }