stylized
[mono.git] / mcs / class / System.Web / System.Web / HttpStaticObjectsCollection.cs
1 using System;
2 using System.IO;
3 using System.Collections;
4
5 namespace System.Web {
6         public sealed class HttpStaticObjectsCollection : ICollection, IEnumerable {
7                 private Hashtable _Objects;
8
9                 // Needs to hold object items that can be latebound and can be serialized
10                 public HttpStaticObjectsCollection ()
11                 {
12                         _Objects = new Hashtable();
13                 }
14
15                 public object GetObject (string name)
16                 {
17                         return this [name];
18                 }
19
20                 public IEnumerator GetEnumerator ()
21                 {
22                         return _Objects.GetEnumerator ();
23                 }
24
25                 public void CopyTo (Array array, int index)
26                 {
27                         _Objects.CopyTo (array, index);
28                 }   
29
30                 internal IDictionary GetObjects ()
31                 {
32                         return _Objects;
33                 }
34
35                 public object this [string name] {
36                         get { return _Objects [name]; }
37                 }
38
39                 public int Count {
40                         get { return _Objects.Count; }
41                 }
42
43                 public bool IsReadOnly {
44                         get { return true; }
45                 }
46
47                 public bool IsSynchronized {
48                         get { return false; }
49                 }
50
51                 public object SyncRoot {
52                         get { return this; }
53                 }
54
55
56                 private void Set (string name, object obj)
57                 {
58                         _Objects [name] = obj;
59                 }
60
61                 internal void Serialize (BinaryWriter w)
62                 {
63                         lock (_Objects) {
64                                 w.Write (Count);
65                                 foreach (string key in _Objects.Keys) {
66                                         w.Write (key);
67                                         object value = _Objects [key];
68                                         if (value == null) {
69                                                 w.Write (System.Web.Util.AltSerialization.NullIndex);
70                                                 continue;
71                                         }
72
73                                         System.Web.Util.AltSerialization.SerializeByType (w, value);
74                                 }
75                         }
76                 }
77
78                 internal static HttpStaticObjectsCollection Deserialize (BinaryReader r)
79                 {
80                         HttpStaticObjectsCollection result = new HttpStaticObjectsCollection ();
81                         for (int i = r.ReadInt32 (); i > 0; i--) {
82                                 string key = r.ReadString ();
83                                 int index = r.ReadInt32 ();
84                                 if (index == System.Web.Util.AltSerialization.NullIndex)
85                                         result.Set (key, null);
86                                 else
87                                         result.Set (key, System.Web.Util.AltSerialization.DeserializeFromIndex (index, r));
88                         }
89
90                         return result;
91                 }
92
93                 internal byte [] ToByteArray ()
94                 {
95                         MemoryStream stream = null;
96                         try {
97                                 stream = new MemoryStream ();
98                                 Serialize (new BinaryWriter (stream));
99                                 return stream.GetBuffer ();
100                         } catch {
101                                 throw;
102                         } finally {
103                                 if (stream != null)
104                                         stream.Close ();
105                         }
106                 }
107
108                 internal static HttpStaticObjectsCollection FromByteArray (byte [] data)
109                 {
110                         HttpStaticObjectsCollection objs = null;
111                         MemoryStream stream = null;
112                         try {
113                                 stream = new MemoryStream (data);
114                                 objs = Deserialize (new BinaryReader (stream));
115                         } catch {
116                                 throw;
117                         } finally {
118                                 if (stream != null)
119                                         stream.Close ();
120                         }
121                         return objs;
122                 }
123         }
124 }
125