title: Json HTTP Module stream issue
body: <p>I have an HTTP Module that I use to clean up the JSON returned by my web service (see <a href="http://www.codeproject.com/KB/webservices/ASPNET_JSONP.aspx?msg=3400287#xx3400287xx" rel="nofollow noreferrer">http://www.codeproject.com/KB/webservices/ASPNET_JSONP.aspx?msg=3400287#xx3400287xx</a> for an example of this.) Basically it relates to calling cross-domain JSON web services from javascript.</p>
  
  <p>There is this JsonHttpModule which uses a JsonResponseFilter Stream class to write out the JSON and the overloaded Write method is supposed to wrap the name of the callback function around the JSON, otherwise the JSON errors out as needing a label. However, if the JSON is really long, the Write method in the Stream class is called multiple times, causing the callback function to incorrectly get inserted midway through the JSON. Is there a way in the Stream class to wrap the callback function around the stream at the end or to specify that it write all of the JSON in 1 Write method instead of in chunks??</p>
  
  <p>Here's where it calls the JsonResponseFilter in the JsonHttpModule:</p>
  
  <pre><code>public void OnReleaseRequestState(object sender, EventArgs e)
          {
              HttpApplication app = (HttpApplication)sender;
  
              if (!_Apply(app.Context.Request)) return;
  
              // apply response filter to conform to JSONP
              app.Context.Response.Filter =
                  new JsonResponseFilter(app.Context.Response.Filter, app.Context);
          }
  </code></pre>
  
  <p>Here's the Write method in the JsonResponseFilter Stream class that gets called multiple times:</p>
  
  <pre><code>public override void Write(byte[] buffer, int offset, int count)
          {
              var b1 = Encoding.UTF8.GetBytes(_context.Request.Params["callback"] + "(");
              _responseStream.Write(b1, 0, b1.Length);
  
              _responseStream.Write(buffer, offset, count);
  
              var b2 = Encoding.UTF8.GetBytes(");");
              _responseStream.Write(b2, 0, b2.Length);
          }
  </code></pre>
  
  <p>Thanks for any help!
  Justin</p>