2 // System.Web.UI.LosFormatter
5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // Copyright (C) 2005 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.
32 using System.Security.Cryptography;
33 using System.Security.Permissions;
36 namespace System.Web.UI {
38 // CAS - no InheritanceDemand here as the class is sealed
39 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 public sealed class LosFormatter {
42 ObjectStateFormatter osf;
44 public LosFormatter ()
46 osf = new ObjectStateFormatter ();
50 public LosFormatter (bool enableMac, string macKeyModifier)
51 : this (enableMac, Convert.FromBase64String (macKeyModifier))
61 LosFormatter (bool enableMac, byte[] macKeyModifier)
64 osf = new ObjectStateFormatter (macKeyModifier);
66 osf = new ObjectStateFormatter ();
69 public object Deserialize (Stream stream)
72 throw new ArgumentNullException ("stream");
74 byte [] bytes = new byte [stream.Length >= 0 ? stream.Length : 2048];
75 MemoryStream ms = null;
76 if ((stream is MemoryStream) && stream.Position == 0) {
77 // We save allocating a new stream and reading in this case.
78 ms = (MemoryStream) stream;
80 ms = new MemoryStream ();
82 while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
83 ms.Write (bytes, 0, n);
86 string b64 = Encoding.ASCII.GetString (ms.GetBuffer (), 0, (int) ms.Length);
87 return Deserialize (b64);
90 public object Deserialize (TextReader input)
93 throw new ArgumentNullException ("input");
95 return Deserialize (input.ReadToEnd ());
98 public object Deserialize (string input)
103 return osf.Deserialize (input);
106 internal string SerializeToBase64 (object value)
108 return osf.Serialize (value);
111 public void Serialize (Stream stream, object value)
114 throw new ArgumentNullException ("stream");
116 string b64 = SerializeToBase64 (value);
117 byte [] bytes = Encoding.ASCII.GetBytes (b64);
118 stream.Write (bytes, 0, bytes.Length);
121 public void Serialize (TextWriter output, object value)
124 throw new ArgumentNullException ("output");
126 output.Write (SerializeToBase64 (value));