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