2 // System.Text.MLangCodePageEncoding.cs
5 // Kornél Pál <http://www.kornelpal.hu/>
7 // Copyright (C) 2006 Kornél Pál
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
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.
37 // Use SerializationInfo.SetType() in ISerializable.GetObjectData() method of
38 // serializable classes to serialize their instances using a proxy class.
40 // All of these proxy classes are non-public thus they only have to be
41 // serialization compatible with .NET Framework.
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.
51 using System.Runtime.Serialization;
56 internal sealed class MLangCodePageEncoding : ISerializable, IObjectReference
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.
67 private sealed class MLangEncoder : ISerializable, IObjectReference
69 private Encoding encoding;
70 private Encoder realObject;
72 private MLangEncoder (SerializationInfo info, StreamingContext context)
75 throw new ArgumentNullException ("info");
77 this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
80 public void GetObjectData (SerializationInfo info, StreamingContext context)
82 throw new ArgumentException ("This class cannot be serialized.");
85 public object GetRealObject (StreamingContext context)
87 if (this.realObject == null)
88 this.realObject = this.encoding.GetEncoder ();
90 return this.realObject;
94 private sealed class MLangEncoder
96 private MLangEncoder ()
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.
111 private sealed class MLangDecoder : ISerializable, IObjectReference
113 private Encoding encoding;
114 private Decoder realObject;
116 private MLangDecoder (SerializationInfo info, StreamingContext context)
119 throw new ArgumentNullException ("info");
121 this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
124 public void GetObjectData (SerializationInfo info, StreamingContext context)
126 throw new ArgumentException ("This class cannot be serialized.");
129 public object GetRealObject (StreamingContext context)
131 if (this.realObject == null)
132 this.realObject = this.encoding.GetDecoder ();
134 return this.realObject;
138 private sealed class MLangDecoder
140 private MLangDecoder ()
146 private int codePage;
148 private bool isReadOnly;
149 private EncoderFallback encoderFallback;
150 private DecoderFallback decoderFallback;
152 private Encoding realObject;
154 private MLangCodePageEncoding (SerializationInfo info, StreamingContext context)
157 throw new ArgumentNullException ("info");
159 this.codePage = (int) info.GetValue ("m_codePage", typeof (int));
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;
173 public void GetObjectData (SerializationInfo info, StreamingContext context)
175 throw new ArgumentException ("This class cannot be serialized.");
178 public object GetRealObject (StreamingContext context)
180 if (this.realObject == null) {
181 Encoding encoding = Encoding.GetEncoding (this.codePage);
184 if (!this.isReadOnly) {
185 encoding = (Encoding) encoding.Clone ();
186 encoding.EncoderFallback = this.encoderFallback;
187 encoding.DecoderFallback = this.decoderFallback;
191 this.realObject = encoding;
194 return this.realObject;