2 // System.Web.HttpWriter.cs
6 // Miguel de Icaza (miguel@novell.com)
9 // Copyright (C) 2005-2009 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Threading;
34 using System.Globalization;
35 using System.Runtime.InteropServices;
36 using System.Security.Permissions;
40 // CAS - no InheritanceDemand here as the class is sealed
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 public sealed class HttpWriter : TextWriter
44 const long MAX_TOTAL_BUFFERS_SIZE = 4 * 1024 * 1024;
45 const uint SINGLE_BUFFER_SIZE = 128 * 1024;
46 const uint MIN_SINGLE_BUFFER_SIZE = 32 * 1024;
48 HttpResponseStream output_stream;
49 HttpResponse response;
53 static byte [] _bytebuffer;
54 static readonly uint byteBufferSize;
58 int workerThreads, completionPortThreads;
60 ThreadPool.GetMinThreads (out workerThreads, out completionPortThreads);
63 uint bufferSize = (uint)(MAX_TOTAL_BUFFERS_SIZE / workerThreads);
64 byteBufferSize = Math.Min (SINGLE_BUFFER_SIZE, bufferSize);
65 if (byteBufferSize < MIN_SINGLE_BUFFER_SIZE)
66 byteBufferSize = MIN_SINGLE_BUFFER_SIZE;
69 internal HttpWriter (HttpResponse response)
71 this.response = response;
72 encoding = response.ContentEncoding;
73 output_stream = response.output_stream;
76 byte [] GetByteBuffer (int length)
78 if (_bytebuffer == null)
79 _bytebuffer = new byte [byteBufferSize];
81 // We will reuse the buffer if its size is < 32K
82 if (byteBufferSize >= length)
85 return new byte [length];
88 public override Encoding Encoding {
94 internal void SetEncoding (Encoding new_encoding)
96 encoding = new_encoding;
99 public Stream OutputStream {
101 return output_stream;
105 internal HttpResponse Response {
106 get { return response; }
109 // Flush data, and closes socket
111 public override void Close ()
113 output_stream.Close ();
116 public override void Flush ()
118 output_stream.Flush ();
121 char [] chars = new char [1];
122 public override void Write (char ch)
128 public override void Write (object obj)
133 Write (obj.ToString ());
136 public override void Write (string s)
139 WriteString (s, 0, s.Length);
142 public override void Write (char [] buffer, int index, int count)
144 if (buffer == null || index < 0 || count < 0 || (buffer.Length - index) < count)
145 throw new ArgumentOutOfRangeException ();
147 output_stream.Write (buffer, index, count);
149 int length = encoding.GetMaxByteCount (count);
150 byte [] bytebuffer = GetByteBuffer (length);
151 int realLength = encoding.GetBytes (buffer, index, count, bytebuffer, 0);
152 output_stream.Write (bytebuffer, 0, realLength);
160 static char [] newline = new char [2] { '\r', '\n' };
162 public override void WriteLine ()
164 Write (newline, 0, 2);
167 public void WriteString (string s, int index, int count)
172 if (index < 0 || count < 0 || ((index + count > s.Length)))
173 throw new ArgumentOutOfRangeException ();
175 output_stream.Write (s, index, count);
177 int length = encoding.GetMaxByteCount (count);
178 byte [] bytebuffer = GetByteBuffer (length);
179 int realLength = encoding.GetBytes (s, index, count, bytebuffer, 0);
180 output_stream.Write (bytebuffer, 0, realLength);
188 internal void WriteUTF8Ptr (IntPtr ptr, int length)
190 output_stream.WritePtr (ptr, length);
193 public void WriteBytes (byte [] buffer, int index, int count)
195 output_stream.Write (buffer, index, count);