2008-04-23 Marek Habersack <mhabersack@novell.com>
[mono.git] / mcs / class / System.Web / System.Web / HttpWriter.cs
index d85eb8c092ae6ee40f1d81f6ea1f2d9d7f4d7c34..4b608d16ef7f78c6eb2b6a38154173be56ecee28 100644 (file)
@@ -42,6 +42,7 @@ namespace System.Web {
                HttpResponseStream output_stream;
                HttpResponse response;
                Encoding encoding;
+               byte [] _bytebuffer = new byte [1024];
 
                internal HttpWriter (HttpResponse response)
                {
@@ -50,6 +51,19 @@ namespace System.Web {
                        output_stream = response.output_stream;
                }
 
+               byte [] GetByteBuffer (int length)
+               {
+                       // We will reuse the buffer if its size is < 32K
+                       if (_bytebuffer.Length >= length)
+                               return _bytebuffer;
+
+                       if (length > 32 * 1024)
+                               return new byte [length];
+
+                       _bytebuffer = new byte [length];
+                       return _bytebuffer;
+               }
+
                public override Encoding Encoding {
                        get {
                                return encoding;
@@ -80,9 +94,11 @@ namespace System.Web {
                        output_stream.Flush ();
                }
 
+               char [] chars = new char [1];
                public override void Write (char ch)
                {
-                       Write (new string (ch, 1));
+                       chars [0] = ch;
+                       Write (chars, 0, 1);
                }
 
                public override void Write (object obj)
@@ -95,50 +111,50 @@ namespace System.Web {
                
                public override void Write (string s)
                {
-                       if (s == null)
-                               return;
-                       
-                       byte [] xx = encoding.GetBytes (s);
-
-                       output_stream.Write (xx, 0, xx.Length);
-                       
-                       if (response.buffer)
-                               return;
-
-                       response.Flush ();
+                       if (s != null)
+                               WriteString (s, 0, s.Length);
                }
                
                public override void Write (char [] buffer, int index, int count)
                {
-                       byte [] xx = encoding.GetBytes (buffer, index, count);
-                       output_stream.Write (xx, 0, xx.Length);
-
+                       if (buffer == null || index < 0 || count < 0 || (buffer.Length - index) < count)
+                               throw new ArgumentOutOfRangeException ();
+#if TARGET_JVM
+                       output_stream.Write (buffer, index, count);
+#else
+                       int length = encoding.GetMaxByteCount (count);
+                       byte [] bytebuffer = GetByteBuffer (length);
+                       int realLength = encoding.GetBytes (buffer, index, count, bytebuffer, 0);
+                       output_stream.Write (bytebuffer, 0, realLength);
+#endif
                        if (response.buffer)
                                return;
 
                        response.Flush ();
                }
 
-               static byte [] newline = new byte [2] { 13, 10 };
+               static char [] newline = new char [2] { '\r', '\n' };
                
                public override void WriteLine ()
                {
-                       output_stream.Write (newline, 0, 2);
-                       
-                       if (response.buffer)
-                               return;
-
-                       response.Flush ();
+                       Write (newline, 0, 2);
                }
 
                public void WriteString (string s, int index, int count)
                {
-                       char [] a = s.ToCharArray (index, count);
-
-                       byte [] xx = encoding.GetBytes (a, 0, count);
-                       
-                       output_stream.Write (xx, 0, xx.Length);
+                       if (s == null)
+                               return;
 
+                       if (index < 0 || count < 0 || ((index + count > s.Length)))
+                               throw new ArgumentOutOfRangeException ();
+#if TARGET_JVM
+                       output_stream.Write (s, index, count);
+#else
+                       int length = encoding.GetMaxByteCount (count);
+                       byte [] bytebuffer = GetByteBuffer (length);
+                       int realLength = encoding.GetBytes (s, index, count, bytebuffer, 0);
+                       output_stream.Write (bytebuffer, 0, realLength);
+#endif
                        if (response.buffer)
                                return;