Merge pull request dotnet/corert#4086 from tarekgh/CleanupSerializationInEncoding
authorTarek Mahmoud Sayed <tarekms@microsoft.com>
Thu, 6 Jul 2017 19:22:02 +0000 (12:22 -0700)
committerTarek Mahmoud Sayed <tarekms@microsoft.com>
Fri, 7 Jul 2017 23:57:04 +0000 (16:57 -0700)
Clean up serialization from Encoding

Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
src/mscorlib/shared/System/Text/Encoding.cs
src/mscorlib/shared/System/Text/UTF7Encoding.cs

index ea75ace..2812240 100644 (file)
@@ -154,23 +154,16 @@ namespace System.Text
         private const int CodePageUTF32 = 12000;
         private const int CodePageUTF32BE = 12001;
 
-        internal int m_codePage = 0;
+        internal int _codePage = 0;
 
-        // dataItem should be internal (not private). otherwise it will break during the deserialization
-        // of the data came from Everett
-        internal CodePageDataItem dataItem = null;
-
-        [NonSerialized]
-        internal bool m_deserializedFromEverett = false;
+        internal CodePageDataItem _dataItem = null;
 
         // Because of encoders we may be read only
         [OptionalField(VersionAdded = 2)]
-        private bool m_isReadOnly = true;
+        private bool _isReadOnly = true;
 
         // Encoding (encoder) fallback
-        [OptionalField(VersionAdded = 2)]
         internal EncoderFallback encoderFallback = null;
-        [OptionalField(VersionAdded = 2)]
         internal DecoderFallback decoderFallback = null;
 
         protected Encoding() : this(0)
@@ -188,7 +181,7 @@ namespace System.Text
             Contract.EndContractBlock();
 
             // Remember code page
-            m_codePage = codePage;
+            _codePage = codePage;
 
             // Use default encoder/decoder fallbacks
             this.SetDefaultFallbacks();
@@ -207,10 +200,10 @@ namespace System.Text
             Contract.EndContractBlock();
 
             // Remember code page
-            m_codePage = codePage;
+            _codePage = codePage;
 
-            this.encoderFallback = encoderFallback ?? new InternalEncoderBestFitFallback(this);
-            this.decoderFallback = decoderFallback ?? new InternalDecoderBestFitFallback(this);
+            encoderFallback = encoderFallback ?? new InternalEncoderBestFitFallback(this);
+            decoderFallback = decoderFallback ?? new InternalDecoderBestFitFallback(this);
         }
 
         // Default fallback that we'll use.
@@ -218,117 +211,10 @@ namespace System.Text
         {
             // For UTF-X encodings, we use a replacement fallback with an "\xFFFD" string,
             // For ASCII we use "?" replacement fallback, etc.
-            this.encoderFallback = new InternalEncoderBestFitFallback(this);
-            this.decoderFallback = new InternalDecoderBestFitFallback(this);
-        }
-
-
-        #region Serialization
-        internal void OnDeserializing()
-        {
-            // intialize the optional Whidbey fields
-            encoderFallback = null;
-            decoderFallback = null;
-            m_isReadOnly = true;
-        }
-
-        internal void OnDeserialized()
-        {
-            if (encoderFallback == null || decoderFallback == null)
-            {
-                m_deserializedFromEverett = true;
-                SetDefaultFallbacks();
-            }
-
-            // dataItem is always recalculated from the code page #
-            dataItem = null;
-        }
-
-        [OnDeserializing]
-        private void OnDeserializing(StreamingContext ctx)
-        {
-            OnDeserializing();
-        }
-
-
-        [OnDeserialized]
-        private void OnDeserialized(StreamingContext ctx)
-        {
-            OnDeserialized();
-        }
-
-        [OnSerializing]
-        private void OnSerializing(StreamingContext ctx)
-        {
-            // to be consistent with SerializeEncoding
-            dataItem = null;
+            encoderFallback = new InternalEncoderBestFitFallback(this);
+            decoderFallback = new InternalDecoderBestFitFallback(this);
         }
 
-        // the following two methods are used for the inherited classes which implemented ISerializable
-        // Deserialization Helper
-        internal void DeserializeEncoding(SerializationInfo info, StreamingContext context)
-        {
-            // Any info?
-            if (info == null) throw new ArgumentNullException(nameof(info));
-            Contract.EndContractBlock();
-
-            // All versions have a code page
-            this.m_codePage = (int)info.GetValue("m_codePage", typeof(int));
-
-            // We can get dataItem on the fly if needed, and the index is different between versions
-            // so ignore whatever dataItem data we get from Everett.
-            this.dataItem = null;
-
-            // See if we have a code page
-            try
-            {
-                //
-                // Try Whidbey V2.0 Fields
-                //
-
-                m_isReadOnly = (bool)info.GetValue("m_isReadOnly", typeof(bool));
-
-                this.encoderFallback = (EncoderFallback)info.GetValue("encoderFallback", typeof(EncoderFallback));
-                this.decoderFallback = (DecoderFallback)info.GetValue("decoderFallback", typeof(DecoderFallback));
-            }
-            catch (SerializationException)
-            {
-                //
-                // Didn't have Whidbey things, must be Everett
-                //
-                this.m_deserializedFromEverett = true;
-
-                // May as well be read only
-                m_isReadOnly = true;
-                SetDefaultFallbacks();
-            }
-        }
-
-        // Serialization Helper
-        internal void SerializeEncoding(SerializationInfo info, StreamingContext context)
-        {
-            // Any Info?
-            if (info == null) throw new ArgumentNullException(nameof(info));
-            Contract.EndContractBlock();
-
-            // These are new V2.0 Whidbey stuff
-            info.AddValue("m_isReadOnly", m_isReadOnly);
-            info.AddValue("encoderFallback", this.EncoderFallback);
-            info.AddValue("decoderFallback", this.DecoderFallback);
-
-            // These were in Everett V1.1 as well
-            info.AddValue("m_codePage", this.m_codePage);
-
-            // This was unique to Everett V1.1
-            info.AddValue("dataItem", null);
-
-            // Everett duplicated these fields, so these are needed for portability
-            info.AddValue("Encoding+m_codePage", this.m_codePage);
-            info.AddValue("Encoding+dataItem", null);
-        }
-
-        #endregion Serialization
-
         // Converts a byte array from one encoding to another. The bytes in the
         // bytes array are converted from srcEncoding to
         // dstEncoding, and the returned value is a new byte array
@@ -498,13 +384,12 @@ namespace System.Text
 
         private void GetDataItem()
         {
-            if (dataItem == null)
+            if (_dataItem == null)
             {
-                dataItem = EncodingTable.GetCodePageDataItem(m_codePage);
-                if (dataItem == null)
+                _dataItem = EncodingTable.GetCodePageDataItem(_codePage);
+                if (_dataItem == null)
                 {
-                    throw new NotSupportedException(
-                        SR.Format(SR.NotSupported_NoCodepageData, m_codePage));
+                    throw new NotSupportedException(SR.Format(SR.NotSupported_NoCodepageData, _codePage));
                 }
             }
         }
@@ -516,11 +401,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return (dataItem.BodyName);
+                return (_dataItem.BodyName);
             }
         }
 
@@ -573,7 +458,7 @@ namespace System.Text
         {
             get
             {
-                return SR.GetResourceString("Globalization_cp_" + m_codePage.ToString());
+                return SR.GetResourceString("Globalization_cp_" + _codePage.ToString());
             }
         }
 #endif
@@ -584,11 +469,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return (dataItem.HeaderName);
+                return (_dataItem.HeaderName);
             }
         }
 
@@ -597,11 +482,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return (dataItem.WebName);
+                return (_dataItem.WebName);
             }
         }
 
@@ -611,11 +496,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return (dataItem.UIFamilyCodePage);
+                return (_dataItem.UIFamilyCodePage);
             }
         }
 
@@ -626,11 +511,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return ((dataItem.Flags & MIMECONTF_BROWSER) != 0);
+                return ((_dataItem.Flags & MIMECONTF_BROWSER) != 0);
             }
         }
 
@@ -640,11 +525,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return ((dataItem.Flags & MIMECONTF_SAVABLE_BROWSER) != 0);
+                return ((_dataItem.Flags & MIMECONTF_SAVABLE_BROWSER) != 0);
             }
         }
 
@@ -654,11 +539,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return ((dataItem.Flags & MIMECONTF_MAILNEWS) != 0);
+                return ((_dataItem.Flags & MIMECONTF_MAILNEWS) != 0);
             }
         }
 
@@ -670,11 +555,11 @@ namespace System.Text
         {
             get
             {
-                if (dataItem == null)
+                if (_dataItem == null)
                 {
                     GetDataItem();
                 }
-                return ((dataItem.Flags & MIMECONTF_SAVABLE_MAILNEWS) != 0);
+                return ((_dataItem.Flags & MIMECONTF_SAVABLE_MAILNEWS) != 0);
             }
         }
 
@@ -736,7 +621,7 @@ namespace System.Text
             Encoding newEncoding = (Encoding)this.MemberwiseClone();
 
             // New one should be readable
-            newEncoding.m_isReadOnly = false;
+            newEncoding._isReadOnly = false;
             return newEncoding;
         }
 
@@ -745,7 +630,7 @@ namespace System.Text
         {
             get
             {
-                return (m_isReadOnly);
+                return (_isReadOnly);
             }
         }
 
@@ -1217,7 +1102,7 @@ namespace System.Text
         {
             get
             {
-                return m_codePage;
+                return _codePage;
             }
         }
 
@@ -1366,7 +1251,7 @@ namespace System.Text
         {
             Encoding that = value as Encoding;
             if (that != null)
-                return (m_codePage == that.m_codePage) &&
+                return (_codePage == that._codePage) &&
                        (EncoderFallback.Equals(that.EncoderFallback)) &&
                        (DecoderFallback.Equals(that.DecoderFallback));
             return (false);
@@ -1375,7 +1260,7 @@ namespace System.Text
 
         public override int GetHashCode()
         {
-            return m_codePage + this.EncoderFallback.GetHashCode() + this.DecoderFallback.GetHashCode();
+            return _codePage + this.EncoderFallback.GetHashCode() + this.DecoderFallback.GetHashCode();
         }
 
         internal virtual char[] GetBestFitUnicodeToBytesData()
index 0ac3b66..141c03c 100644 (file)
@@ -33,18 +33,17 @@ namespace System.Text
         internal static readonly UTF7Encoding s_default = new UTF7Encoding();
 
         // The set of base 64 characters.
-        private byte[] base64Bytes;
+        private byte[] _base64Bytes;
         // The decoded bits for every base64 values. This array has a size of 128 elements.
         // The index is the code point value of the base 64 characters.  The value is -1 if
         // the code point is not a valid base 64 character.  Otherwise, the value is a value
         // from 0 ~ 63.
-        private sbyte[] base64Values;
+        private sbyte[] _base64Values;
         // The array to decide if a Unicode code point below 0x80 can be directly encoded in UTF7.
         // This array has a size of 128.
-        private bool[] directEncode;
+        private bool[] _directEncode;
 
-        [OptionalField(VersionAdded = 2)]
-        private bool m_allowOptionals;
+        private bool _allowOptionals;
 
         private const int UTF7_CODEPAGE = 65000;
 
@@ -58,7 +57,7 @@ namespace System.Text
             : base(UTF7_CODEPAGE) //Set the data item.
         {
             // Allowing optionals?
-            m_allowOptionals = allowOptionals;
+            _allowOptionals = allowOptionals;
 
             // Make our tables
             MakeTables();
@@ -67,24 +66,24 @@ namespace System.Text
         private void MakeTables()
         {
             // Build our tables
-            base64Bytes = new byte[64];
-            for (int i = 0; i < 64; i++) base64Bytes[i] = (byte)base64Chars[i];
-            base64Values = new sbyte[128];
-            for (int i = 0; i < 128; i++) base64Values[i] = -1;
-            for (int i = 0; i < 64; i++) base64Values[base64Bytes[i]] = (sbyte)i;
-            directEncode = new bool[128];
+            _base64Bytes = new byte[64];
+            for (int i = 0; i < 64; i++) _base64Bytes[i] = (byte)base64Chars[i];
+            _base64Values = new sbyte[128];
+            for (int i = 0; i < 128; i++) _base64Values[i] = -1;
+            for (int i = 0; i < 64; i++) _base64Values[_base64Bytes[i]] = (sbyte)i;
+            _directEncode = new bool[128];
             int count = directChars.Length;
             for (int i = 0; i < count; i++)
             {
-                directEncode[directChars[i]] = true;
+                _directEncode[directChars[i]] = true;
             }
 
-            if (m_allowOptionals)
+            if (_allowOptionals)
             {
                 count = optionalChars.Length;
                 for (int i = 0; i < count; i++)
                 {
-                    directEncode[optionalChars[i]] = true;
+                    _directEncode[optionalChars[i]] = true;
                 }
             }
         }
@@ -99,36 +98,12 @@ namespace System.Text
             this.decoderFallback = new DecoderUTF7Fallback();
         }
 
-
-        [OnDeserializing]
-        private void OnDeserializing(StreamingContext ctx)
-        {
-            // make sure the optional fields initialized correctly.
-            base.OnDeserializing();
-        }
-
-        [OnDeserialized]
-        private void OnDeserialized(StreamingContext ctx)
-        {
-            base.OnDeserialized();
-
-            if (m_deserializedFromEverett)
-            {
-                // If 1st optional char is encoded we're allowing optionals
-                m_allowOptionals = directEncode[optionalChars[0]];
-            }
-
-            MakeTables();
-        }
-
-
-
         public override bool Equals(Object value)
         {
             UTF7Encoding that = value as UTF7Encoding;
             if (that != null)
             {
-                return (m_allowOptionals == that.m_allowOptionals) &&
+                return (_allowOptionals == that._allowOptionals) &&
                        (EncoderFallback.Equals(that.EncoderFallback)) &&
                        (DecoderFallback.Equals(that.DecoderFallback));
             }
@@ -482,7 +457,7 @@ namespace System.Text
                 {
                     bitCount -= 6;
                     // If we fail we'll never really have enough room
-                    if (!buffer.AddByte(base64Bytes[(bits >> bitCount) & 0x3F]))
+                    if (!buffer.AddByte(_base64Bytes[(bits >> bitCount) & 0x3F]))
                         ThrowBytesOverflow(encoder, buffer.Count == 0);
                 }
             }
@@ -491,14 +466,14 @@ namespace System.Text
             {
                 char currentChar = buffer.GetNextChar();
 
-                if (currentChar < 0x80 && directEncode[currentChar])
+                if (currentChar < 0x80 && _directEncode[currentChar])
                 {
                     if (bitCount >= 0)
                     {
                         if (bitCount > 0)
                         {
                             // Try to add the next byte
-                            if (!buffer.AddByte(base64Bytes[bits << 6 - bitCount & 0x3F]))
+                            if (!buffer.AddByte(_base64Bytes[bits << 6 - bitCount & 0x3F]))
                                 break;                                          // Stop here, didn't throw
 
                             bitCount = 0;
@@ -540,7 +515,7 @@ namespace System.Text
                     while (bitCount >= 6)
                     {
                         bitCount -= 6;
-                        if (!buffer.AddByte(base64Bytes[(bits >> bitCount) & 0x3F]))
+                        if (!buffer.AddByte(_base64Bytes[(bits >> bitCount) & 0x3F]))
                         {
                             bitCount += 6;                              // We didn't use these bits
                             currentChar = buffer.GetNextChar();              // We're processing this char still, but AddByte
@@ -561,7 +536,7 @@ namespace System.Text
                 // Do we have bits we have to stick in?
                 if (bitCount > 0)
                 {
-                    if (buffer.AddByte(base64Bytes[(bits << (6 - bitCount)) & 0x3F]))
+                    if (buffer.AddByte(_base64Bytes[(bits << (6 - bitCount)) & 0x3F]))
                     {
                         // Emitted spare bits, 0 bits left
                         bitCount = 0;
@@ -654,7 +629,7 @@ namespace System.Text
                     // Modified base 64 encoding.
                     //
                     sbyte v;
-                    if (currentByte < 0x80 && ((v = base64Values[currentByte]) >= 0))
+                    if (currentByte < 0x80 && ((v = _base64Values[currentByte]) >= 0))
                     {
                         firstByte = false;
                         bits = (bits << 6) | ((byte)v);
@@ -675,7 +650,7 @@ namespace System.Text
                         if (currentByte != '-')
                         {
                             // >= 0x80 (because of 1st if statemtn)
-                            // We need this check since the base64Values[b] check below need b <= 0x7f.
+                            // We need this check since the _base64Values[b] check below need b <= 0x7f.
                             // This is not a valid base 64 byte.  Terminate the shifted-sequence and
                             // emit this byte.