Parcourir la source

2005-09-23 Gonzalo Paniagua Javier <[email protected]>

	* LosFormatter.cs: the exceptions thrown have a 500 httpCode. Really
	save the allocation of new MemoryStreams when possible. Thanks to
	Sebastien again.


svn path=/trunk/mcs/; revision=50648
Gonzalo Paniagua Javier il y a 20 ans
Parent
commit
070d6e9ba9

+ 6 - 0
mcs/class/System.Web/System.Web.UI/ChangeLog

@@ -1,3 +1,9 @@
+2005-09-23 Gonzalo Paniagua Javier <[email protected]>
+
+	* LosFormatter.cs: the exceptions thrown have a 500 httpCode. Really
+	save the allocation of new MemoryStreams when possible. Thanks to
+	Sebastien again.
+
 2005-09-23  Ben Maurer  <[email protected]>
 
 	* HtmlTextWriter.cs: Initial support for escaping.

+ 15 - 8
mcs/class/System.Web/System.Web.UI/LosFormatter.cs

@@ -49,7 +49,7 @@ namespace System.Web.UI {
 
 #if NET_1_1
 		public LosFormatter (bool enableMac, string macKeyModifier)
-			: this (enableMac, Encoding.UTF8.GetBytes (macKeyModifier))
+			: this (enableMac, Convert.FromBase64String (macKeyModifier))
 		{
 		}
 #endif
@@ -70,14 +70,14 @@ namespace System.Web.UI {
 		{
 			int hash_size = algo.HashSize / 8;
 			if (size != 0 && size < hash_size)
-				throw new HttpException (400, "Unable to validate data.");
+				throw new HttpException ("Unable to validate data.");
 
 			int data_length = size - hash_size;
 			MemoryStream data_stream = new MemoryStream (data, offset, data_length, false, false);
 			byte [] hash = algo.ComputeHash (data_stream);
 			for (int i = 0; i < hash_size; i++) {
 				if (hash [i] != data [data_length + i])
-					throw new HttpException (400, "Unable to validate data.");
+					throw new HttpException ("Unable to validate data.");
 			}
 		}
 
@@ -87,10 +87,16 @@ namespace System.Web.UI {
 				return osf.Deserialize (stream);
 
 			byte [] bytes = new byte [stream.Length >= 0 ? stream.Length : 2048];
-			MemoryStream ms = new MemoryStream ();
-			int n;
-			while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
-				ms.Write (bytes, 0, n);
+			MemoryStream ms = null;
+			if ((stream is MemoryStream) && stream.Position == 0) {
+				// We save allocating a new stream and reading in this case.
+				ms = (MemoryStream) stream;
+			} else {
+				ms = new MemoryStream ();
+				int n;
+				while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
+					ms.Write (bytes, 0, n);
+			}
 
 			byte [] buffer = ms.GetBuffer ();
 			int length = (int) ms.Length;
@@ -133,8 +139,9 @@ namespace System.Web.UI {
 				return;
 			}
 
-			MemoryStream ms = new MemoryStream ();
+			MemoryStream ms = null;
 			if ((stream is MemoryStream) && stream.Position == 0) {
+				// We save allocating a new stream and reading in this case.
 				ms = (MemoryStream) stream;
 			} else {
 				ms = new MemoryStream ();