MLangCodePageEncoding.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. [Serializable]
  60. private sealed class MLangEncoder : ISerializable, IObjectReference
  61. {
  62. private Encoding encoding;
  63. private Encoder realObject;
  64. private MLangEncoder (SerializationInfo info, StreamingContext context)
  65. {
  66. if (info == null)
  67. throw new ArgumentNullException ("info");
  68. this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
  69. }
  70. public void GetObjectData (SerializationInfo info, StreamingContext context)
  71. {
  72. throw new ArgumentException ("This class cannot be serialized.");
  73. }
  74. public object GetRealObject (StreamingContext context)
  75. {
  76. if (this.realObject == null)
  77. this.realObject = this.encoding.GetEncoder ();
  78. return this.realObject;
  79. }
  80. }
  81. //
  82. // .NET Framework 1.x uses this class for internal decoders.
  83. // .NET Framework 2.0 can deserialize them using a proxy although
  84. // this class is not serializable in .NET Framework 1.x.
  85. // This class supports serialization compatibility.
  86. //
  87. [Serializable]
  88. private sealed class MLangDecoder : ISerializable, IObjectReference
  89. {
  90. private Encoding encoding;
  91. private Decoder realObject;
  92. private MLangDecoder (SerializationInfo info, StreamingContext context)
  93. {
  94. if (info == null)
  95. throw new ArgumentNullException ("info");
  96. this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
  97. }
  98. public void GetObjectData (SerializationInfo info, StreamingContext context)
  99. {
  100. throw new ArgumentException ("This class cannot be serialized.");
  101. }
  102. public object GetRealObject (StreamingContext context)
  103. {
  104. if (this.realObject == null)
  105. this.realObject = this.encoding.GetDecoder ();
  106. return this.realObject;
  107. }
  108. }
  109. private int codePage;
  110. private bool isReadOnly;
  111. private EncoderFallback encoderFallback;
  112. private DecoderFallback decoderFallback;
  113. private Encoding realObject;
  114. private MLangCodePageEncoding (SerializationInfo info, StreamingContext context)
  115. {
  116. if (info == null)
  117. throw new ArgumentNullException ("info");
  118. this.codePage = (int) info.GetValue ("m_codePage", typeof (int));
  119. try {
  120. this.isReadOnly = (bool) info.GetValue ("m_isReadOnly", typeof (bool));
  121. this.encoderFallback = (EncoderFallback) info.GetValue ("encoderFallback", typeof (EncoderFallback));
  122. this.decoderFallback = (DecoderFallback) info.GetValue ("decoderFallback", typeof (DecoderFallback));
  123. } catch (SerializationException) {
  124. // .NET Framework 1.x has no fallbacks
  125. this.isReadOnly = true;
  126. }
  127. }
  128. public void GetObjectData (SerializationInfo info, StreamingContext context)
  129. {
  130. throw new ArgumentException ("This class cannot be serialized.");
  131. }
  132. public object GetRealObject (StreamingContext context)
  133. {
  134. if (this.realObject == null) {
  135. Encoding encoding = Encoding.GetEncoding (this.codePage);
  136. if (!this.isReadOnly) {
  137. encoding = (Encoding) encoding.Clone ();
  138. encoding.EncoderFallback = this.encoderFallback;
  139. encoding.DecoderFallback = this.decoderFallback;
  140. }
  141. this.realObject = encoding;
  142. }
  143. return this.realObject;
  144. }
  145. }
  146. }