CodePageEncoding.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // System.Text.CodePageEncoding.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 CodePageEncoding : ISerializable, IObjectReference
  52. {
  53. //
  54. // .NET Framework 1.x uses this class for internal decoders and
  55. // .NET Framework 2.0 can deserialize them using a proxy.
  56. // This class supports serialization compatibility.
  57. //
  58. [Serializable]
  59. private sealed class Decoder : ISerializable, IObjectReference
  60. {
  61. private Encoding encoding;
  62. private System.Text.Decoder realObject;
  63. private Decoder (SerializationInfo info, StreamingContext context)
  64. {
  65. if (info == null)
  66. throw new ArgumentNullException ("info");
  67. this.encoding = (Encoding) info.GetValue ("encoding", typeof (Encoding));
  68. }
  69. public void GetObjectData (SerializationInfo info, StreamingContext context)
  70. {
  71. throw new ArgumentException ("This class cannot be serialized.");
  72. }
  73. public object GetRealObject (StreamingContext context)
  74. {
  75. if (this.realObject == null)
  76. this.realObject = this.encoding.GetDecoder ();
  77. return this.realObject;
  78. }
  79. }
  80. private int codePage;
  81. private bool isReadOnly;
  82. private EncoderFallback encoderFallback;
  83. private DecoderFallback decoderFallback;
  84. private Encoding realObject;
  85. private CodePageEncoding (SerializationInfo info, StreamingContext context)
  86. {
  87. if (info == null)
  88. throw new ArgumentNullException ("info");
  89. this.codePage = (int) info.GetValue ("m_codePage", typeof (int));
  90. try {
  91. this.isReadOnly = (bool) info.GetValue ("m_isReadOnly", typeof (bool));
  92. this.encoderFallback = (EncoderFallback) info.GetValue ("encoderFallback", typeof (EncoderFallback));
  93. this.decoderFallback = (DecoderFallback) info.GetValue ("decoderFallback", typeof (DecoderFallback));
  94. } catch (SerializationException) {
  95. // .NET Framework 1.x has no fallbacks
  96. this.isReadOnly = true;
  97. }
  98. }
  99. public void GetObjectData (SerializationInfo info, StreamingContext context)
  100. {
  101. throw new ArgumentException ("This class cannot be serialized.");
  102. }
  103. public object GetRealObject (StreamingContext context)
  104. {
  105. if (this.realObject == null) {
  106. Encoding encoding = Encoding.GetEncoding (this.codePage);
  107. if (!this.isReadOnly) {
  108. encoding = (Encoding) encoding.Clone ();
  109. encoding.EncoderFallback = this.encoderFallback;
  110. encoding.DecoderFallback = this.decoderFallback;
  111. }
  112. this.realObject = encoding;
  113. }
  114. return this.realObject;
  115. }
  116. }
  117. }