merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / mcs / class / System.Web / System.Web.UI / LosFormatter.cs
1 //
2 // System.Web.UI.LosFormatter
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (C) 2003 Ben Maurer
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.IO;
32 using System.Security.Cryptography;
33 using System.Security.Permissions;
34 using System.Text;
35
36 namespace System.Web.UI {
37
38         // CAS - no InheritanceDemand here as the class is sealed
39         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         public sealed class LosFormatter {
41
42                 ObjectStateFormatter osf;
43                 
44                 public LosFormatter ()
45                 {
46                         osf = new ObjectStateFormatter ();
47                 }
48
49 #if NET_1_1
50                 public LosFormatter (bool enableMac, string macKeyModifier)
51                         : this (enableMac, Convert.FromBase64String (macKeyModifier))
52                 {
53                 }
54 #endif
55                 [MonoTODO]
56 #if NET_2_0
57                 public
58 #else
59                 internal
60 #endif
61                 LosFormatter (bool enableMac, byte[] macKeyModifier)
62                 {
63                         if (enableMac)
64                                 osf = new ObjectStateFormatter (macKeyModifier);
65                         else
66                                 osf = new ObjectStateFormatter ();
67                 }
68
69                 public object Deserialize (Stream stream)
70                 {
71                         if (stream == null)
72                                 throw new ArgumentNullException ("stream");
73
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;
79                         } else {
80                                 ms = new MemoryStream ();
81                                 int n;
82                                 while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
83                                         ms.Write (bytes, 0, n);
84                         }
85
86                         string b64 = Encoding.ASCII.GetString (ms.GetBuffer (), 0, (int) ms.Length);
87                         return Deserialize (b64);
88                 }
89
90                 public object Deserialize (TextReader input)
91                 {
92                         if (input == null)
93                                 throw new ArgumentNullException ("input");
94
95                         return Deserialize (input.ReadToEnd ());
96                 }
97
98                 public object Deserialize (string input)
99                 {
100                         if (input == null)
101                                 return null;
102
103                         return osf.Deserialize (input);
104                 }
105
106                 internal string SerializeToBase64 (object value)
107                 {
108                         return osf.Serialize (value);
109                 }
110
111                 public void Serialize (Stream stream, object value)
112                 {
113                         if (stream == null)
114                                 throw new ArgumentNullException ("stream");
115
116                         string b64 = SerializeToBase64 (value);
117                         byte [] bytes = Encoding.ASCII.GetBytes (b64);
118                         stream.Write (bytes, 0, bytes.Length);
119                 }
120
121                 public void Serialize (TextWriter output, object value)
122                 {
123                         if (output == null)
124                                 throw new ArgumentNullException ("output");
125
126                         output.Write (SerializeToBase64 (value));
127                 }       
128         }
129 }
130