Facilitate the merge
[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-2010 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                 public LosFormatter (bool enableMac, string macKeyModifier) : this (enableMac, Convert.FromBase64String (macKeyModifier))
50                 {
51                 }
52
53                 [MonoTODO]
54                 public LosFormatter (bool enableMac, byte[] macKeyModifier)
55                 {
56                         if (enableMac)
57                                 osf = new ObjectStateFormatter (macKeyModifier);
58                         else
59                                 osf = new ObjectStateFormatter ();
60                 }
61
62                 public object Deserialize (Stream stream)
63                 {
64                         if (stream == null)
65                                 throw new ArgumentNullException ("stream");
66                         long streamLength = -1;
67                         if (stream.CanSeek)
68                                 streamLength = stream.Length;
69                         byte [] bytes = new byte [streamLength >= 0 ? streamLength : 2048];
70                         MemoryStream ms = null;
71                         if (streamLength  != -1 && (stream is MemoryStream) && stream.Position == 0) {
72                                 // We save allocating a new stream and reading in this case.
73                                 ms = (MemoryStream) stream;
74                         } else {
75                                 ms = new MemoryStream ();
76                                 int n;
77                                 while ((n = stream.Read (bytes, 0, bytes.Length)) > 0)
78                                         ms.Write (bytes, 0, n);
79                                 streamLength = ms.Length;
80                         }
81
82                         string b64 = Encoding.ASCII.GetString (ms.GetBuffer (),
83                                 0, (int) streamLength);
84                         return Deserialize (b64);
85                 }
86
87                 public object Deserialize (TextReader input)
88                 {
89                         if (input == null)
90                                 throw new ArgumentNullException ("input");
91
92                         return Deserialize (input.ReadToEnd ());
93                 }
94
95                 public object Deserialize (string input)
96                 {
97                         if (input == null)
98                                 return null;
99
100                         return osf.Deserialize (input);
101                 }
102
103                 internal string SerializeToBase64 (object value)
104                 {
105                         return osf.Serialize (value);
106                 }
107
108                 public void Serialize (Stream stream, object value)
109                 {
110                         if (stream == null)
111                                 throw new ArgumentNullException ("stream");
112
113                         string b64 = SerializeToBase64 (value);
114                         byte [] bytes = Encoding.ASCII.GetBytes (b64);
115                         stream.Write (bytes, 0, bytes.Length);
116                 }
117
118                 public void Serialize (TextWriter output, object value)
119                 {
120                         if (output == null)
121                                 throw new ArgumentNullException ("output");
122
123                         output.Write (SerializeToBase64 (value));
124                 }       
125         }
126 }
127