2009-09-22 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / corlib / System.Text / MLangCodePageEncoding.cs
1 //
2 // System.Text.MLangCodePageEncoding.cs
3 //
4 // Author:
5 //   Kornél Pál <http://www.kornelpal.hu/>
6 //
7 // Copyright (C) 2006 Kornél Pál
8 //
9
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 //
32 // These proxy classes implement IObjectReference.GetRealObject() that returns
33 // an instance of the appropriate Encoding, Encoder or Decoder class.
34 // As a result serialized objects of these types will transparently be
35 // deserialized to instances of the above described classes.
36 //
37 // Use SerializationInfo.SetType() in ISerializable.GetObjectData() method of
38 // serializable classes to serialize their instances using a proxy class.
39 //
40 // All of these proxy classes are non-public thus they only have to be
41 // serialization compatible with .NET Framework.
42 //
43
44 //
45 // .NET Framework 1.x uses this class for internal encodings and
46 // .NET Framework 2.0 serializes internal encodings using a proxy.
47 // This class supports serialization compatibility.
48 //
49
50 using System;
51 using System.Runtime.Serialization;
52
53 namespace System.Text
54 {
55         [Serializable]
56         internal sealed class MLangCodePageEncoding : ISerializable, IObjectReference
57         {
58                 //
59                 // .NET Framework 1.x uses this class for internal encoders.
60                 // .NET Framework 2.0 can deserialize them using a proxy although
61                 // this class is not serializable in .NET Framework 1.x.
62                 // This class supports serialization compatibility.
63                 //
64
65 #if NET_2_0
66                 [Serializable]
67                 private sealed class MLangEncoder : ISerializable, IObjectReference
68                 {
69                         private Encoding encoding;
70                         private Encoder realObject;
71
72                         private MLangEncoder (SerializationInfo info, StreamingContext context)
73                         {
74                                 if (info == null)
75                                         throw new ArgumentNullException ("info");
76
77                                 this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
78                         }
79
80                         public void GetObjectData (SerializationInfo info, StreamingContext context)
81                         {
82                                 throw new ArgumentException ("This class cannot be serialized.");
83                         }
84
85                         public object GetRealObject (StreamingContext context)
86                         {
87                                 if (this.realObject == null)
88                                         this.realObject = this.encoding.GetEncoder ();
89
90                                 return this.realObject;
91                         }
92                 }
93 #else
94                 private sealed class MLangEncoder
95                 {
96                         private MLangEncoder ()
97                         {
98                         }
99                 }
100 #endif
101
102                 //
103                 // .NET Framework 1.x uses this class for internal decoders.
104                 // .NET Framework 2.0 can deserialize them using a proxy although
105                 // this class is not serializable in .NET Framework 1.x.
106                 // This class supports serialization compatibility.
107                 //
108
109 #if NET_2_0
110                 [Serializable]
111                 private sealed class MLangDecoder : ISerializable, IObjectReference
112                 {
113                         private Encoding encoding;
114                         private Decoder realObject;
115
116                         private MLangDecoder (SerializationInfo info, StreamingContext context)
117                         {
118                                 if (info == null)
119                                         throw new ArgumentNullException ("info");
120
121                                 this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
122                         }
123
124                         public void GetObjectData (SerializationInfo info, StreamingContext context)
125                         {
126                                 throw new ArgumentException ("This class cannot be serialized.");
127                         }
128
129                         public object GetRealObject (StreamingContext context)
130                         {
131                                 if (this.realObject == null)
132                                         this.realObject = this.encoding.GetDecoder ();
133
134                                 return this.realObject;
135                         }
136                 }
137 #else
138                 private sealed class MLangDecoder
139                 {
140                         private MLangDecoder ()
141                         {
142                         }
143                 }
144 #endif
145
146                 private int codePage;
147 #if NET_2_0
148                 private bool isReadOnly;
149                 private EncoderFallback encoderFallback;
150                 private DecoderFallback decoderFallback;
151 #endif
152                 private Encoding realObject;
153
154                 private MLangCodePageEncoding (SerializationInfo info, StreamingContext context)
155                 {
156                         if (info == null)
157                                 throw new ArgumentNullException ("info");
158
159                         this.codePage = (int) info.GetValue ("m_codePage", typeof (int));
160
161 #if NET_2_0
162                         try {
163                                 this.isReadOnly = (bool) info.GetValue ("m_isReadOnly", typeof (bool));
164                                 this.encoderFallback = (EncoderFallback) info.GetValue ("encoderFallback", typeof (EncoderFallback));
165                                 this.decoderFallback = (DecoderFallback) info.GetValue ("decoderFallback", typeof (DecoderFallback));
166                         } catch (SerializationException) {
167                                 // .NET Framework 1.x has no fallbacks
168                                 this.isReadOnly = true;
169                         }
170 #endif
171                 }
172
173                 public void GetObjectData (SerializationInfo info, StreamingContext context)
174                 {
175                         throw new ArgumentException ("This class cannot be serialized.");
176                 }
177
178                 public object GetRealObject (StreamingContext context)
179                 {
180                         if (this.realObject == null) {
181                                 Encoding encoding = Encoding.GetEncoding (this.codePage);
182
183 #if NET_2_0
184                                 if (!this.isReadOnly) {
185                                         encoding = (Encoding) encoding.Clone ();
186                                         encoding.EncoderFallback = this.encoderFallback;
187                                         encoding.DecoderFallback = this.decoderFallback;
188                                 }
189 #endif
190
191                                 this.realObject = encoding;
192                         }
193
194                         return this.realObject;
195                 }
196         }
197 }