diff options
Diffstat (limited to 'src/mscorlib/src/System/Text/Encoding.cs')
-rw-r--r-- | src/mscorlib/src/System/Text/Encoding.cs | 428 |
1 files changed, 216 insertions, 212 deletions
diff --git a/src/mscorlib/src/System/Text/Encoding.cs b/src/mscorlib/src/System/Text/Encoding.cs index 8cb01e41fa..159123495b 100644 --- a/src/mscorlib/src/System/Text/Encoding.cs +++ b/src/mscorlib/src/System/Text/Encoding.cs @@ -85,79 +85,83 @@ namespace System.Text [Serializable] public abstract class Encoding : ICloneable { - private static Encoding defaultEncoding; + // For netcore we use UTF8 as default encoding since ANSI isn't available + private static readonly UTF8Encoding.UTF8EncodingSealed s_defaultEncoding = new UTF8Encoding.UTF8EncodingSealed(encoderShouldEmitUTF8Identifier: false); + + // Returns an encoding for the system's current ANSI code page. + public static Encoding Default => s_defaultEncoding; // // The following values are from mlang.idl. These values // should be in sync with those in mlang.idl. // - internal const int MIMECONTF_MAILNEWS = 0x00000001; - internal const int MIMECONTF_BROWSER = 0x00000002; - internal const int MIMECONTF_SAVABLE_MAILNEWS = 0x00000100; - internal const int MIMECONTF_SAVABLE_BROWSER = 0x00000200; + internal const int MIMECONTF_MAILNEWS = 0x00000001; + internal const int MIMECONTF_BROWSER = 0x00000002; + internal const int MIMECONTF_SAVABLE_MAILNEWS = 0x00000100; + internal const int MIMECONTF_SAVABLE_BROWSER = 0x00000200; // Special Case Code Pages - private const int CodePageDefault = 0; - private const int CodePageNoOEM = 1; // OEM Code page not supported - private const int CodePageNoMac = 2; // MAC code page not supported - private const int CodePageNoThread = 3; // Thread code page not supported - private const int CodePageNoSymbol = 42; // Symbol code page not supported - private const int CodePageUnicode = 1200; // Unicode - private const int CodePageBigEndian = 1201; // Big Endian Unicode - private const int CodePageWindows1252 = 1252; // Windows 1252 code page + private const int CodePageDefault = 0; + private const int CodePageNoOEM = 1; // OEM Code page not supported + private const int CodePageNoMac = 2; // MAC code page not supported + private const int CodePageNoThread = 3; // Thread code page not supported + private const int CodePageNoSymbol = 42; // Symbol code page not supported + private const int CodePageUnicode = 1200; // Unicode + private const int CodePageBigEndian = 1201; // Big Endian Unicode + private const int CodePageWindows1252 = 1252; // Windows 1252 code page // 20936 has same code page as 10008, so we'll special case it private const int CodePageMacGB2312 = 10008; - private const int CodePageGB2312 = 20936; + private const int CodePageGB2312 = 20936; private const int CodePageMacKorean = 10003; private const int CodePageDLLKorean = 20949; // ISO 2022 Code Pages - private const int ISO2022JP = 50220; - private const int ISO2022JPESC = 50221; - private const int ISO2022JPSISO = 50222; - private const int ISOKorean = 50225; - private const int ISOSimplifiedCN = 50227; - private const int EUCJP = 51932; - private const int ChineseHZ = 52936; // HZ has ~}~{~~ sequences + private const int ISO2022JP = 50220; + private const int ISO2022JPESC = 50221; + private const int ISO2022JPSISO = 50222; + private const int ISOKorean = 50225; + private const int ISOSimplifiedCN = 50227; + private const int EUCJP = 51932; + private const int ChineseHZ = 52936; // HZ has ~}~{~~ sequences // 51936 is the same as 936 - private const int DuplicateEUCCN = 51936; - private const int EUCCN = 936; + private const int DuplicateEUCCN = 51936; + private const int EUCCN = 936; - private const int EUCKR = 51949; + private const int EUCKR = 51949; // Latin 1 & ASCII Code Pages - internal const int CodePageASCII = 20127; // ASCII - internal const int ISO_8859_1 = 28591; // Latin1 + internal const int CodePageASCII = 20127; // ASCII + internal const int ISO_8859_1 = 28591; // Latin1 // ISCII - private const int ISCIIAssemese = 57006; - private const int ISCIIBengali = 57003; - private const int ISCIIDevanagari = 57002; - private const int ISCIIGujarathi = 57010; - private const int ISCIIKannada = 57008; - private const int ISCIIMalayalam = 57009; - private const int ISCIIOriya = 57007; - private const int ISCIIPanjabi = 57011; - private const int ISCIITamil = 57004; - private const int ISCIITelugu = 57005; + private const int ISCIIAssemese = 57006; + private const int ISCIIBengali = 57003; + private const int ISCIIDevanagari = 57002; + private const int ISCIIGujarathi = 57010; + private const int ISCIIKannada = 57008; + private const int ISCIIMalayalam = 57009; + private const int ISCIIOriya = 57007; + private const int ISCIIPanjabi = 57011; + private const int ISCIITamil = 57004; + private const int ISCIITelugu = 57005; // GB18030 - private const int GB18030 = 54936; + private const int GB18030 = 54936; // Other - private const int ISO_8859_8I = 38598; + private const int ISO_8859_8I = 38598; private const int ISO_8859_8_Visual = 28598; // 50229 is currently unsupported // "Chinese Traditional (ISO-2022)" - private const int ENC50229 = 50229; + private const int ENC50229 = 50229; // Special code pages - private const int CodePageUTF7 = 65000; - private const int CodePageUTF8 = 65001; - private const int CodePageUTF32 = 12000; - private const int CodePageUTF32BE = 12001; + private const int CodePageUTF7 = 65000; + private const int CodePageUTF8 = 65001; + private const int CodePageUTF32 = 12000; + private const int CodePageUTF32BE = 12001; internal int m_codePage = 0; @@ -228,13 +232,13 @@ namespace System.Text } -#region Serialization + #region Serialization internal void OnDeserializing() { // intialize the optional Whidbey fields encoderFallback = null; decoderFallback = null; - m_isReadOnly = true; + m_isReadOnly = true; } internal void OnDeserialized() @@ -246,7 +250,7 @@ namespace System.Text } // dataItem is always recalculated from the code page # - dataItem = null; + dataItem = null; } [OnDeserializing] @@ -274,7 +278,7 @@ namespace System.Text internal void DeserializeEncoding(SerializationInfo info, StreamingContext context) { // Any info? - if (info==null) throw new ArgumentNullException(nameof(info)); + if (info == null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // All versions have a code page @@ -282,7 +286,7 @@ namespace System.Text // 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; + this.dataItem = null; // See if we have a code page try @@ -291,7 +295,7 @@ namespace System.Text // Try Whidbey V2.0 Fields // - this.m_isReadOnly = (bool)info.GetValue("m_isReadOnly", typeof(bool)); + 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)); @@ -304,7 +308,7 @@ namespace System.Text this.m_deserializedFromEverett = true; // May as well be read only - this.m_isReadOnly = true; + m_isReadOnly = true; SetDefaultFallbacks(); } } @@ -313,11 +317,11 @@ namespace System.Text internal void SerializeEncoding(SerializationInfo info, StreamingContext context) { // Any Info? - if (info==null) throw new ArgumentNullException(nameof(info)); + if (info == null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // These are new V2.0 Whidbey stuff - info.AddValue("m_isReadOnly", this.m_isReadOnly); + info.AddValue("m_isReadOnly", m_isReadOnly); info.AddValue("encoderFallback", this.EncoderFallback); info.AddValue("decoderFallback", this.DecoderFallback); @@ -332,7 +336,7 @@ namespace System.Text info.AddValue("Encoding+dataItem", null); } -#endregion Serialization + #endregion Serialization // Converts a byte array from one encoding to another. The bytes in the // bytes array are converted from srcEncoding to @@ -341,11 +345,12 @@ namespace System.Text // [Pure] public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding, - byte[] bytes) { - if (bytes==null) + byte[] bytes) + { + if (bytes == null) throw new ArgumentNullException(nameof(bytes)); Contract.Ensures(Contract.Result<byte[]>() != null); - + return Convert(srcEncoding, dstEncoding, bytes, 0, bytes.Length); } @@ -356,22 +361,25 @@ namespace System.Text // [Pure] public static byte[] Convert(Encoding srcEncoding, Encoding dstEncoding, - byte[] bytes, int index, int count) { - if (srcEncoding == null || dstEncoding == null) { + byte[] bytes, int index, int count) + { + if (srcEncoding == null || dstEncoding == null) + { throw new ArgumentNullException((srcEncoding == null ? nameof(srcEncoding) : nameof(dstEncoding)), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); } - if (bytes == null) { + if (bytes == null) + { throw new ArgumentNullException(nameof(bytes), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); } Contract.Ensures(Contract.Result<byte[]>() != null); - + return dstEncoding.GetBytes(srcEncoding.GetChars(bytes, index, count)); } - public static void RegisterProvider(EncodingProvider provider) + public static void RegisterProvider(EncodingProvider provider) { // Parameters validated inside EncodingProvider EncodingProvider.AddProvider(provider); @@ -390,10 +398,10 @@ namespace System.Text // Otherwise, the code below will throw exception when trying to call // EncodingTable.GetDataItem(). // - if (codepage < 0 || codepage > 65535) { + if (codepage < 0 || codepage > 65535) + { throw new ArgumentOutOfRangeException( - nameof(codepage), Environment.GetResourceString("ArgumentOutOfRange_Range", - 0, 65535)); + nameof(codepage), SR.Format(SR.ArgumentOutOfRange_Range, 0, 65535)); } Contract.EndContractBlock(); @@ -418,15 +426,14 @@ namespace System.Text case CodePageNoMac: // 2 CP_MACCP case CodePageNoThread: // 3 CP_THREAD_ACP case CodePageNoSymbol: // 42 CP_SYMBOL - throw new ArgumentException(Environment.GetResourceString( - "Argument_CodepageNotSupported", codepage), nameof(codepage)); + throw new ArgumentException(SR.Format(SR.Argument_CodepageNotSupported, codepage), nameof(codepage)); } // Is it a valid code page? if (EncodingTable.GetCodePageDataItem(codepage) == null) { throw new NotSupportedException( - Environment.GetResourceString("NotSupported_NoCodepageData", codepage)); + SR.Format(SR.NotSupported_NoCodepageData, codepage)); } return UTF8; @@ -498,15 +505,18 @@ namespace System.Text [Pure] public virtual byte[] GetPreamble() { - return EmptyArray<Byte>.Value; + return Array.Empty<Byte>(); } - private void GetDataItem() { - if (dataItem==null) { + private void GetDataItem() + { + if (dataItem == null) + { dataItem = EncodingTable.GetCodePageDataItem(m_codePage); - if(dataItem==null) { + if (dataItem == null) + { throw new NotSupportedException( - Environment.GetResourceString("NotSupported_NoCodepageData", m_codePage)); + SR.Format(SR.NotSupported_NoCodepageData, m_codePage)); } } } @@ -518,7 +528,8 @@ namespace System.Text { get { - if (dataItem==null) { + if (dataItem == null) + { GetDataItem(); } return (dataItem.BodyName); @@ -531,7 +542,7 @@ namespace System.Text { get { - return Environment.GetResourceString("Globalization.cp_" + m_codePage.ToString()); + return SR.GetResourceString("Globalization_cp_" + m_codePage.ToString()); } } @@ -542,7 +553,8 @@ namespace System.Text { get { - if (dataItem==null) { + if (dataItem == null) + { GetDataItem(); } return (dataItem.HeaderName); @@ -556,7 +568,8 @@ namespace System.Text { get { - if (dataItem==null) { + if (dataItem == null) + { GetDataItem(); } return (dataItem.WebName); @@ -569,7 +582,8 @@ namespace System.Text { get { - if (dataItem==null) { + if (dataItem == null) + { GetDataItem(); } return (dataItem.UIFamilyCodePage); @@ -579,9 +593,12 @@ namespace System.Text // True if and only if the encoding is used for display by browsers clients. - public virtual bool IsBrowserDisplay { - get { - if (dataItem==null) { + public virtual bool IsBrowserDisplay + { + get + { + if (dataItem == null) + { GetDataItem(); } return ((dataItem.Flags & MIMECONTF_BROWSER) != 0); @@ -590,9 +607,12 @@ namespace System.Text // True if and only if the encoding is used for saving by browsers clients. - public virtual bool IsBrowserSave { - get { - if (dataItem==null) { + public virtual bool IsBrowserSave + { + get + { + if (dataItem == null) + { GetDataItem(); } return ((dataItem.Flags & MIMECONTF_SAVABLE_BROWSER) != 0); @@ -601,9 +621,12 @@ namespace System.Text // True if and only if the encoding is used for display by mail and news clients. - public virtual bool IsMailNewsDisplay { - get { - if (dataItem==null) { + public virtual bool IsMailNewsDisplay + { + get + { + if (dataItem == null) + { GetDataItem(); } return ((dataItem.Flags & MIMECONTF_MAILNEWS) != 0); @@ -614,9 +637,12 @@ namespace System.Text // True if and only if the encoding is used for saving documents by mail and // news clients - public virtual bool IsMailNewsSave { - get { - if (dataItem==null) { + public virtual bool IsMailNewsSave + { + get + { + if (dataItem == null) + { GetDataItem(); } return ((dataItem.Flags & MIMECONTF_SAVABLE_MAILNEWS) != 0); @@ -644,7 +670,7 @@ namespace System.Text set { if (this.IsReadOnly) - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ReadOnly")); + throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) throw new ArgumentNullException(nameof(value)); @@ -665,7 +691,7 @@ namespace System.Text set { if (this.IsReadOnly) - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_ReadOnly")); + throw new InvalidOperationException(SR.InvalidOperation_ReadOnly); if (value == null) throw new ArgumentNullException(nameof(value)); @@ -715,7 +741,7 @@ namespace System.Text if (chars == null) { throw new ArgumentNullException(nameof(chars), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); } Contract.EndContractBlock(); @@ -731,7 +757,6 @@ namespace System.Text char[] chars = s.ToCharArray(); return GetByteCount(chars, 0, chars.Length); - } // Returns the number of bytes required to encode a range of characters in @@ -746,17 +771,17 @@ namespace System.Text public int GetByteCount(string s, int index, int count) { if (s == null) - throw new ArgumentNullException(nameof(s), - Environment.GetResourceString("ArgumentNull_String")); + throw new ArgumentNullException(nameof(s), + SR.ArgumentNull_String); if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + SR.ArgumentOutOfRange_NeedNonNegNum); if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + SR.ArgumentOutOfRange_NeedNonNegNum); if (index > s.Length - count) throw new ArgumentOutOfRangeException(nameof(index), - Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); + SR.ArgumentOutOfRange_IndexCount); Contract.EndContractBlock(); unsafe @@ -779,11 +804,11 @@ namespace System.Text // Validate input parameters if (chars == null) throw new ArgumentNullException(nameof(chars), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); char[] arrChar = new char[count]; @@ -814,7 +839,7 @@ namespace System.Text if (chars == null) { throw new ArgumentNullException(nameof(chars), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); } Contract.EndContractBlock(); return GetBytes(chars, 0, chars.Length); @@ -851,7 +876,7 @@ namespace System.Text { if (s == null) throw new ArgumentNullException(nameof(s), - Environment.GetResourceString("ArgumentNull_String")); + SR.ArgumentNull_String); Contract.EndContractBlock(); int byteCount = GetByteCount(s); @@ -869,16 +894,16 @@ namespace System.Text { if (s == null) throw new ArgumentNullException(nameof(s), - Environment.GetResourceString("ArgumentNull_String")); + SR.ArgumentNull_String); if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + SR.ArgumentOutOfRange_NeedNonNegNum); if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + SR.ArgumentOutOfRange_NeedNonNegNum); if (index > s.Length - count) throw new ArgumentOutOfRangeException(nameof(index), - Environment.GetResourceString("ArgumentOutOfRange_IndexCount")); + SR.ArgumentOutOfRange_IndexCount); Contract.EndContractBlock(); unsafe @@ -941,11 +966,11 @@ namespace System.Text // Validate input parameters if (bytes == null || chars == null) throw new ArgumentNullException(bytes == null ? nameof(bytes) : nameof(chars), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); if (charCount < 0 || byteCount < 0) - throw new ArgumentOutOfRangeException((charCount<0 ? nameof(charCount) : nameof(byteCount)), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException((charCount < 0 ? nameof(charCount) : nameof(byteCount)), + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); // Get the char array to convert @@ -987,7 +1012,7 @@ namespace System.Text if (bytes == null) { throw new ArgumentNullException(nameof(bytes), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); } Contract.EndContractBlock(); return GetCharCount(bytes, 0, bytes.Length); @@ -1008,11 +1033,11 @@ namespace System.Text // Validate input parameters if (bytes == null) throw new ArgumentNullException(nameof(bytes), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); byte[] arrbyte = new byte[count]; @@ -1040,7 +1065,7 @@ namespace System.Text if (bytes == null) { throw new ArgumentNullException(nameof(bytes), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); } Contract.EndContractBlock(); return GetChars(bytes, 0, bytes.Length); @@ -1095,11 +1120,11 @@ namespace System.Text // Validate input parameters if (chars == null || bytes == null) throw new ArgumentNullException(chars == null ? nameof(chars) : nameof(bytes), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); if (byteCount < 0 || charCount < 0) - throw new ArgumentOutOfRangeException((byteCount<0 ? nameof(byteCount) : nameof(charCount)), - Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException((byteCount < 0 ? nameof(byteCount) : nameof(charCount)), + SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); // Get the byte array to convert @@ -1146,10 +1171,10 @@ namespace System.Text public unsafe string GetString(byte* bytes, int byteCount) { if (bytes == null) - throw new ArgumentNullException(nameof(bytes), Environment.GetResourceString("ArgumentNull_Array")); + throw new ArgumentNullException(nameof(bytes), SR.ArgumentNull_Array); if (byteCount < 0) - throw new ArgumentOutOfRangeException(nameof(byteCount), Environment.GetResourceString("ArgumentOutOfRange_NeedNonNegNum")); + throw new ArgumentOutOfRangeException(nameof(byteCount), SR.ArgumentOutOfRange_NeedNonNegNum); Contract.EndContractBlock(); return String.CreateStringFromEncoding(bytes, byteCount, this); @@ -1202,26 +1227,6 @@ namespace System.Text return new DefaultDecoder(this); } - private static Encoding CreateDefaultEncoding() - { - // defaultEncoding should be null if we get here, but we can't - // assert that in case another thread beat us to the initialization - - Encoding enc; - - - // For silverlight we use UTF8 since ANSI isn't available - enc = UTF8; - - - // This method should only ever return one Encoding instance - return Interlocked.CompareExchange(ref defaultEncoding, enc, null) ?? enc; - } - - // Returns an encoding for the system's current ANSI code page. - - public static Encoding Default => defaultEncoding ?? CreateDefaultEncoding(); - // Returns an Encoder object for this encoding. The returned object // can be used to encode a sequence of characters into a sequence of bytes. // Contrary to the GetBytes family of methods, an Encoder can @@ -1274,7 +1279,7 @@ namespace System.Text { if (bytes == null) throw new ArgumentNullException(nameof(bytes), - Environment.GetResourceString("ArgumentNull_Array")); + SR.ArgumentNull_Array); Contract.EndContractBlock(); return GetString(bytes, 0, bytes.Length); @@ -1311,7 +1316,7 @@ namespace System.Text // an instance of the UTF7Encoding class. public static Encoding UTF7 => UTF7Encoding.s_default; - + // Returns an encoding for the UTF-8 format. The returned encoding will be // an instance of the UTF8Encoding class. @@ -1329,7 +1334,8 @@ namespace System.Text private static Encoding BigEndianUTF32 => UTF32Encoding.s_bigEndianDefault; - public override bool Equals(Object value) { + public override bool Equals(Object value) + { Encoding that = value as Encoding; if (that != null) return (m_codePage == that.m_codePage) && @@ -1339,20 +1345,21 @@ namespace System.Text } - public override int GetHashCode() { + public override int GetHashCode() + { return m_codePage + this.EncoderFallback.GetHashCode() + this.DecoderFallback.GetHashCode(); } internal virtual char[] GetBestFitUnicodeToBytesData() { // Normally we don't have any best fit data. - return EmptyArray<Char>.Value; + return Array.Empty<Char>(); } internal virtual char[] GetBestFitBytesToUnicodeData() { // Normally we don't have any best fit data. - return EmptyArray<Char>.Value; + return Array.Empty<Char>(); } internal void ThrowBytesOverflow() @@ -1360,8 +1367,7 @@ namespace System.Text // Special message to include fallback type in case fallback's GetMaxCharCount is broken // This happens if user has implimented an encoder fallback with a broken GetMaxCharCount throw new ArgumentException( - Environment.GetResourceString("Argument_EncodingConversionOverflowBytes", - EncodingName, EncoderFallback.GetType()), "bytes"); + SR.Format(SR.Argument_EncodingConversionOverflowBytes, EncodingName, EncoderFallback.GetType()), "bytes"); } internal void ThrowBytesOverflow(EncoderNLS encoder, bool nothingEncoded) @@ -1384,8 +1390,7 @@ namespace System.Text // Special message to include fallback type in case fallback's GetMaxCharCount is broken // This happens if user has implimented a decoder fallback with a broken GetMaxCharCount throw new ArgumentException( - Environment.GetResourceString("Argument_EncodingConversionOverflowChars", - EncodingName, DecoderFallback.GetType()), "chars"); + SR.Format(SR.Argument_EncodingConversionOverflowChars, EncodingName, DecoderFallback.GetType()), "chars"); } internal void ThrowCharsOverflow(DecoderNLS decoder, bool nothingDecoded) @@ -1405,7 +1410,7 @@ namespace System.Text } [Serializable] - internal class DefaultEncoder : Encoder, IObjectReference, ISerializable + internal sealed class DefaultEncoder : Encoder, IObjectReference, ISerializable { private Encoding m_encoding; [NonSerialized] private bool m_hasInitializedEncoding; @@ -1421,16 +1426,16 @@ namespace System.Text // Constructor called by serialization, have to handle deserializing from Everett internal DefaultEncoder(SerializationInfo info, StreamingContext context) { - if (info==null) throw new ArgumentNullException(nameof(info)); + if (info == null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // All we have is our encoding - this.m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding)); + m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding)); - try + try { - this.m_fallback = (EncoderFallback) info.GetValue("m_fallback", typeof(EncoderFallback)); - this.charLeftOver = (Char) info.GetValue("charLeftOver", typeof(Char)); + this.m_fallback = (EncoderFallback)info.GetValue("m_fallback", typeof(EncoderFallback)); + this.charLeftOver = (Char)info.GetValue("charLeftOver", typeof(Char)); } catch (SerializationException) { @@ -1453,7 +1458,7 @@ namespace System.Text Encoder encoder = m_encoding.GetEncoder(); if (m_fallback != null) encoder.m_fallback = m_fallback; - if (charLeftOver != (char) 0) + if (charLeftOver != (char)0) { EncoderNLS encoderNls = encoder as EncoderNLS; if (encoderNls != null) @@ -1466,11 +1471,11 @@ namespace System.Text void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { // Any info? - if (info==null) throw new ArgumentNullException(nameof(info)); + if (info == null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // All we have is our encoding - info.AddValue("encoding", this.m_encoding); + info.AddValue("encoding", m_encoding); } // Returns the number of bytes the next call to GetBytes will @@ -1527,7 +1532,7 @@ namespace System.Text } [Serializable] - internal class DefaultDecoder : Decoder, IObjectReference, ISerializable + internal sealed class DefaultDecoder : Decoder, IObjectReference, ISerializable { private Encoding m_encoding; [NonSerialized] @@ -1537,21 +1542,21 @@ namespace System.Text { m_encoding = encoding; m_hasInitializedEncoding = true; - } + } // Constructor called by serialization, have to handle deserializing from Everett internal DefaultDecoder(SerializationInfo info, StreamingContext context) { // Any info? - if (info==null) throw new ArgumentNullException(nameof(info)); + if (info == null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // All we have is our encoding - this.m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding)); - - try + m_encoding = (Encoding)info.GetValue("encoding", typeof(Encoding)); + + try { - this.m_fallback = (DecoderFallback) info.GetValue("m_fallback", typeof(DecoderFallback)); + this.m_fallback = (DecoderFallback)info.GetValue("m_fallback", typeof(DecoderFallback)); } catch (SerializationException) { @@ -1583,11 +1588,11 @@ namespace System.Text void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { // Any info? - if (info==null) throw new ArgumentNullException(nameof(info)); + if (info == null) throw new ArgumentNullException(nameof(info)); Contract.EndContractBlock(); // All we have is our encoding - info.AddValue("encoding", this.m_encoding); + info.AddValue("encoding", m_encoding); } // Returns the number of characters the next call to GetChars will @@ -1654,16 +1659,16 @@ namespace System.Text internal class EncodingCharBuffer { - unsafe char* chars; - unsafe char* charStart; - unsafe char* charEnd; - int charCountResult = 0; - Encoding enc; - DecoderNLS decoder; - unsafe byte* byteStart; - unsafe byte* byteEnd; - unsafe byte* bytes; - DecoderFallbackBuffer fallbackBuffer; + private unsafe char* chars; + private unsafe char* charStart; + private unsafe char* charEnd; + private int charCountResult = 0; + private Encoding enc; + private DecoderNLS decoder; + private unsafe byte* byteStart; + private unsafe byte* byteEnd; + private unsafe byte* bytes; + private DecoderFallbackBuffer fallbackBuffer; internal unsafe EncodingCharBuffer(Encoding enc, DecoderNLS decoder, char* charStart, int charCount, byte* byteStart, int byteCount) @@ -1671,18 +1676,18 @@ namespace System.Text this.enc = enc; this.decoder = decoder; - this.chars = charStart; + chars = charStart; this.charStart = charStart; - this.charEnd = charStart + charCount; + charEnd = charStart + charCount; this.byteStart = byteStart; - this.bytes = byteStart; - this.byteEnd = byteStart + byteCount; + bytes = byteStart; + byteEnd = byteStart + byteCount; if (this.decoder == null) - this.fallbackBuffer = enc.DecoderFallback.CreateFallbackBuffer(); + fallbackBuffer = enc.DecoderFallback.CreateFallbackBuffer(); else - this.fallbackBuffer = this.decoder.FallbackBuffer; + fallbackBuffer = this.decoder.FallbackBuffer; // If we're getting chars or getting char count we don't expect to have // to remember fallbacks between calls (so it should be empty) @@ -1698,7 +1703,7 @@ namespace System.Text if (chars >= charEnd) { // Throw maybe - bytes-=numBytes; // Didn't encode these bytes + bytes -= numBytes; // Didn't encode these bytes enc.ThrowCharsOverflow(decoder, bytes <= byteStart); // Throw? return false; // No throw, but no store either } @@ -1711,7 +1716,7 @@ namespace System.Text internal unsafe bool AddChar(char ch) { - return AddChar(ch,1); + return AddChar(ch, 1); } internal unsafe void AdjustBytes(int count) @@ -1789,41 +1794,40 @@ namespace System.Text internal class EncodingByteBuffer { - unsafe byte* bytes; - unsafe byte* byteStart; - unsafe byte* byteEnd; - unsafe char* chars; - unsafe char* charStart; - unsafe char* charEnd; - int byteCountResult = 0; - Encoding enc; - EncoderNLS encoder; + private unsafe byte* bytes; + private unsafe byte* byteStart; + private unsafe byte* byteEnd; + private unsafe char* chars; + private unsafe char* charStart; + private unsafe char* charEnd; + private int byteCountResult = 0; + private Encoding enc; + private EncoderNLS encoder; internal EncoderFallbackBuffer fallbackBuffer; internal unsafe EncodingByteBuffer(Encoding inEncoding, EncoderNLS inEncoder, byte* inByteStart, int inByteCount, char* inCharStart, int inCharCount) { - this.enc = inEncoding; - this.encoder = inEncoder; + enc = inEncoding; + encoder = inEncoder; - this.charStart = inCharStart; - this.chars = inCharStart; - this.charEnd = inCharStart + inCharCount; + charStart = inCharStart; + chars = inCharStart; + charEnd = inCharStart + inCharCount; - this.bytes = inByteStart; - this.byteStart = inByteStart; - this.byteEnd = inByteStart + inByteCount; + bytes = inByteStart; + byteStart = inByteStart; + byteEnd = inByteStart + inByteCount; - if (this.encoder == null) + if (encoder == null) this.fallbackBuffer = enc.EncoderFallback.CreateFallbackBuffer(); else { - this.fallbackBuffer = this.encoder.FallbackBuffer; + this.fallbackBuffer = encoder.FallbackBuffer; // If we're not converting we must not have data in our fallback buffer if (encoder.m_throwOnOverflow && encoder.InternalHasFallbackBuffer && this.fallbackBuffer.Remaining > 0) - throw new ArgumentException(Environment.GetResourceString("Argument_EncoderFallbackNotEmpty", - encoder.Encoding.EncodingName, encoder.Fallback.GetType())); + throw new ArgumentException(SR.Format(SR.Argument_EncoderFallbackNotEmpty, encoder.Encoding.EncodingName, encoder.Fallback.GetType())); } fallbackBuffer.InternalInitialize(chars, charEnd, encoder, bytes != null); } @@ -1867,8 +1871,8 @@ namespace System.Text fallbackBuffer.MovePrevious(); // don't use last fallback else { - Debug.Assert(chars > charStart || - ((bThrow == true) && (bytes == byteStart)), + Debug.Assert(chars > charStart || + ((bThrow == true) && (bytes == byteStart)), "[EncodingByteBuffer.MovePrevious]expected previous data or throw"); if (chars > charStart) chars--; // don't use last char @@ -1889,7 +1893,7 @@ namespace System.Text internal unsafe char GetNextChar() { - // See if there's something in our fallback buffer + // See if there's something in our fallback buffer char cReturn = fallbackBuffer.InternalGetNextChar(); // Nothing in the fallback buffer, return our normal data. @@ -1898,9 +1902,9 @@ namespace System.Text if (chars < charEnd) cReturn = *(chars++); } - + return cReturn; - } + } internal unsafe int CharsUsed { |