MLangCodePageEncoding.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. //
  30. // These proxy classes implement IObjectReference.GetRealObject() that returns
  31. // an instance of the appropriate Encoding, Encoder or Decoder class.
  32. // As a result serialized objects of these types will transparently be
  33. // deserialized to instances of the above described classes.
  34. //
  35. // Use SerializationInfo.SetType() in ISerializable.GetObjectData() method of
  36. // serializable classes to serialize their instances using a proxy class.
  37. //
  38. // All of these proxy classes are non-public thus they only have to be
  39. // serialization compatible with .NET Framework.
  40. //
  41. //
  42. // .NET Framework 1.x uses this class for internal encodings and
  43. // .NET Framework 2.0 serializes internal encodings using a proxy.
  44. // This class supports serialization compatibility.
  45. //
  46. using System;
  47. using System.Runtime.Serialization;
  48. namespace System.Text
  49. {
  50. [Serializable]
  51. internal sealed class MLangCodePageEncoding : ISerializable, IObjectReference
  52. {
  53. //
  54. // .NET Framework 1.x uses this class for internal encoders.
  55. // .NET Framework 2.0 can deserialize them using a proxy although
  56. // this class is not serializable in .NET Framework 1.x.
  57. // This class supports serialization compatibility.
  58. //
  59. #if NET_2_0
  60. [Serializable]
  61. private sealed class MLangEncoder : ISerializable, IObjectReference
  62. {
  63. private Encoding encoding;
  64. private Encoder realObject;
  65. private MLangEncoder (SerializationInfo info, StreamingContext context)
  66. {
  67. if (info == null)
  68. throw new ArgumentNullException ("info");
  69. this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
  70. }
  71. public void GetObjectData (SerializationInfo info, StreamingContext context)
  72. {
  73. throw new ArgumentException ("This class cannot be serialized.");
  74. }
  75. public object GetRealObject (StreamingContext context)
  76. {
  77. if (this.realObject == null)
  78. this.realObject = this.encoding.GetEncoder ();
  79. return this.realObject;
  80. }
  81. }
  82. #else
  83. private sealed class MLangEncoder
  84. {
  85. private MLangEncoder ()
  86. {
  87. }
  88. }
  89. #endif
  90. //
  91. // .NET Framework 1.x uses this class for internal decoders.
  92. // .NET Framework 2.0 can deserialize them using a proxy although
  93. // this class is not serializable in .NET Framework 1.x.
  94. // This class supports serialization compatibility.
  95. //
  96. #if NET_2_0
  97. [Serializable]
  98. private sealed class MLangDecoder : ISerializable, IObjectReference
  99. {
  100. private Encoding encoding;
  101. private Decoder realObject;
  102. private MLangDecoder (SerializationInfo info, StreamingContext context)
  103. {
  104. if (info == null)
  105. throw new ArgumentNullException ("info");
  106. this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
  107. }
  108. public void GetObjectData (SerializationInfo info, StreamingContext context)
  109. {
  110. throw new ArgumentException ("This class cannot be serialized.");
  111. }
  112. public object GetRealObject (StreamingContext context)
  113. {
  114. if (this.realObject == null)
  115. this.realObject = this.encoding.GetDecoder ();
  116. return this.realObject;
  117. }
  118. }
  119. #else
  120. private sealed class MLangDecoder
  121. {
  122. private MLangDecoder ()
  123. {
  124. }
  125. }
  126. #endif
  127. private int codePage;
  128. #if NET_2_0
  129. private bool isReadOnly;
  130. private EncoderFallback encoderFallback;
  131. private DecoderFallback decoderFallback;
  132. #endif
  133. private Encoding realObject;
  134. private MLangCodePageEncoding (SerializationInfo info, StreamingContext context)
  135. {
  136. if (info == null)
  137. throw new ArgumentNullException ("info");
  138. this.codePage = (int) info.GetValue ("m_codePage", typeof (int));
  139. #if NET_2_0
  140. try {
  141. this.isReadOnly = (bool) info.GetValue ("m_isReadOnly", typeof (bool));
  142. this.encoderFallback = (EncoderFallback) info.GetValue ("encoderFallback", typeof (EncoderFallback));
  143. this.decoderFallback = (DecoderFallback) info.GetValue ("decoderFallback", typeof (DecoderFallback));
  144. } catch (SerializationException) {
  145. // .NET Framework 1.x has no fallbacks
  146. this.isReadOnly = true;
  147. }
  148. #endif
  149. }
  150. public void GetObjectData (SerializationInfo info, StreamingContext context)
  151. {
  152. throw new ArgumentException ("This class cannot be serialized.");
  153. }
  154. public object GetRealObject (StreamingContext context)
  155. {
  156. if (this.realObject == null) {
  157. Encoding encoding = Encoding.GetEncoding (this.codePage);
  158. #if NET_2_0
  159. if (!this.isReadOnly) {
  160. encoding = (Encoding) encoding.Clone ();
  161. encoding.EncoderFallback = this.encoderFallback;
  162. encoding.DecoderFallback = this.decoderFallback;
  163. }
  164. #endif
  165. this.realObject = encoding;
  166. }
  167. return this.realObject;
  168. }
  169. }
  170. }