encoding.cs

来自「没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没的 没」· CS 代码 · 共 971 行 · 第 1/2 页

CS
971
字号
				// Build a web encoding class name.				String encName = "System.Text.ENC" +								 name.ToLower(CultureInfo.InvariantCulture)								 	.Replace('-', '_');				// Look for a code page converter in this assembly.				Assembly assembly = Assembly.GetExecutingAssembly();				Type type = assembly.GetType(encName);				if(type != null)				{					return (Encoding)(Activator.CreateInstance(type));				}				// Look in any assembly, in case the application				// has provided its own code page handler.				type = Type.GetType(encName);				if(type != null)				{					return (Encoding)(Activator.CreateInstance(type));				}#endif				// We have no idea how to handle this encoding name.				throw new NotSupportedException					(String.Format(_("NotSupp_EncodingName"), name));			}#endif // !ECMA_COMPAT	// Get a hash code for this instance.	public override int GetHashCode()			{				return codePage;			}	// Get the maximum number of bytes needed to encode a	// specified number of characters.	public abstract int GetMaxByteCount(int charCount);	// Get the maximum number of characters needed to decode a	// specified number of bytes.	public abstract int GetMaxCharCount(int byteCount);	// Get the identifying preamble for this encoding.	public virtual byte[] GetPreamble()			{				return new byte [0];			}	// Decode a buffer of bytes into a string.	public virtual String GetString(byte[] bytes, int index, int count)			{				return new String(GetChars(bytes, index, count));			}	public virtual String GetString(byte[] bytes)			{				return new String(GetChars(bytes));			}#if !ECMA_COMPAT	// Get the mail body name for this encoding.	public virtual String BodyName			{				get				{					return InternalBodyName;				}			}	// Get the code page represented by this object.	public virtual int CodePage			{				get				{					return InternalCodePage;				}			}	// Get the human-readable name for this encoding.	public virtual String EncodingName			{				get				{					return InternalEncodingName;				}			}	// Get the mail agent header name for this encoding.	public virtual String HeaderName			{				get				{					return InternalHeaderName;				}			}	// Determine if this encoding can be displayed in a Web browser.	public virtual bool IsBrowserDisplay			{				get				{					return InternalIsBrowserDisplay;				}			}	// Determine if this encoding can be saved from a Web browser.	public virtual bool IsBrowserSave			{				get				{					return InternalIsBrowserSave;				}			}	// Determine if this encoding can be displayed in a mail/news agent.	public virtual bool IsMailNewsDisplay			{				get				{					return InternalIsMailNewsDisplay;				}			}	// Determine if this encoding can be saved from a mail/news agent.	public virtual bool IsMailNewsSave			{				get				{					return InternalIsMailNewsSave;				}			}	// Get the IANA-preferred Web name for this encoding.	public virtual String WebName			{				get				{					return InternalWebName;				}			}	// Get the Windows code page represented by this object.	public virtual int WindowsCodePage			{				get				{					return InternalWindowsCodePage;				}			}	// Get the mail body name for this encoding.	internal virtual String InternalBodyName			{				get				{					return null;				}			}	// Get the code page represented by this object.	internal virtual int InternalCodePage			{				get				{					return codePage;				}			}	// Get the human-readable name for this encoding.	internal virtual String InternalEncodingName			{				get				{					return null;				}			}	// Get the mail agent header name for this encoding.	internal virtual String InternalHeaderName			{				get				{					return null;				}			}	// Determine if this encoding can be displayed in a Web browser.	internal virtual bool InternalIsBrowserDisplay			{				get				{					return false;				}			}	// Determine if this encoding can be saved from a Web browser.	internal virtual bool InternalIsBrowserSave			{				get				{					return false;				}			}	// Determine if this encoding can be displayed in a mail/news agent.	internal virtual bool InternalIsMailNewsDisplay			{				get				{					return false;				}			}	// Determine if this encoding can be saved from a mail/news agent.	internal virtual bool InternalIsMailNewsSave			{				get				{					return false;				}			}	// Get the IANA-preferred Web name for this encoding.	internal virtual String InternalWebName			{				get				{					return null;				}			}	// Get the Windows code page represented by this object.	internal virtual int InternalWindowsCodePage			{				get				{					// We make no distinction between normal and					// Windows code pages in this implementation.					return codePage;				}			}#endif // !ECMA_COMPAT	// Storage for standard encoding objects.	private static Encoding asciiEncoding = null;	private static Encoding bigEndianEncoding = null;	private static Encoding defaultEncoding = null;	private static Encoding utf7Encoding = null;	private static Encoding utf8Encoding = null;	private static Encoding unicodeEncoding = null;	private static Encoding isoLatin1Encoding = null;	private static Encoding utf32Encoding = null;	private static Encoding bigEndianUtf32Encoding = null;	// Get the standard ASCII encoding object.	public static Encoding ASCII			{				get				{					lock(typeof(Encoding))					{						if(asciiEncoding == null)						{							asciiEncoding = new ASCIIEncoding();						}						return asciiEncoding;					}				}			}	// Get the standard big-endian Unicode encoding object.	public static Encoding BigEndianUnicode			{				get				{					lock(typeof(Encoding))					{						if(bigEndianEncoding == null)						{							bigEndianEncoding = new UnicodeEncoding(true, true);						}						return bigEndianEncoding;					}				}			}	// Get the default encoding object.	public static Encoding Default			{				get				{					lock(typeof(Encoding))					{						if(defaultEncoding == null)						{							// See if the underlying system knows what							// code page handler we should be using.							int codePage = DefaultEncoding.InternalCodePage();							if(codePage != 0)							{								try								{									defaultEncoding = GetEncoding(codePage);								}								catch(NotSupportedException)								{									defaultEncoding = new DefaultEncoding();								}							}							else							{								defaultEncoding = new DefaultEncoding();							}						}						return defaultEncoding;					}				}			}	// Get the ISO Latin1 encoding object.	private static Encoding ISOLatin1			{				get				{					lock(typeof(Encoding))					{						if(isoLatin1Encoding == null)						{							isoLatin1Encoding = new Latin1Encoding();						}						return isoLatin1Encoding;					}				}			}	// Get the standard UTF-7 encoding object.#if ECMA_COMPAT	private#else	public#endif	static Encoding UTF7			{				get				{					lock(typeof(Encoding))					{						if(utf7Encoding == null)						{							utf7Encoding = new UTF7Encoding();						}						return utf7Encoding;					}				}			}	// Get the standard UTF-8 encoding object.	public static Encoding UTF8			{				get				{					lock(typeof(Encoding))					{						if(utf8Encoding == null)						{							utf8Encoding = new UTF8Encoding(true);						}						return utf8Encoding;					}				}			}	// Get the standard little-endian Unicode encoding object.	public static Encoding Unicode			{				get				{					lock(typeof(Encoding))					{						if(unicodeEncoding == null)						{							unicodeEncoding = new UnicodeEncoding();						}						return unicodeEncoding;					}				}			}	// Get the standard UTF-32 encoding object.#if !ECMA_COMPAT && CONFIG_FRAMEWORK_1_2	public#else	internal#endif	static Encoding UTF32			{				get				{					lock(typeof(Encoding))					{						if(utf32Encoding == null)						{							utf32Encoding = new UTF32Encoding();						}						return utf32Encoding;					}				}			}	// Get the big-endian UTF-32 encoding object.	private static Encoding BigEndianUTF32			{				get				{					lock(typeof(Encoding))					{						if(bigEndianUtf32Encoding == null)						{							bigEndianUtf32Encoding = new UTF32Encoding								(true, true);						}						return bigEndianUtf32Encoding;					}				}			}	// Forwarding decoder implementation.	private sealed class ForwardingDecoder : Decoder	{		private Encoding encoding;		// Constructor.		public ForwardingDecoder(Encoding enc)				{					encoding = enc;				}		// Override inherited methods.		public override int GetCharCount(byte[] bytes, int index, int count)				{					return encoding.GetCharCount(bytes, index, count);				}		public override int GetChars(byte[] bytes, int byteIndex,									 int byteCount, char[] chars,									 int charIndex)				{					return encoding.GetChars(bytes, byteIndex, byteCount,											 chars, charIndex);				}	} // class ForwardingDecoder	// Forwarding encoder implementation.	private sealed class ForwardingEncoder : Encoder	{		private Encoding encoding;		// Constructor.		public ForwardingEncoder(Encoding enc)				{					encoding = enc;				}		// Override inherited methods.		public override int GetByteCount(char[] chars, int index,										 int count, bool flush)				{					return encoding.GetByteCount(chars, index, count);				}		public override int GetBytes(char[] chars, int charIndex,									 int charCount, byte[] bytes,									 int byteCount, bool flush)				{					return encoding.GetBytes(chars, charIndex, charCount,											 bytes, byteCount);				}	} // class ForwardingEncoder}; // class Encoding}; // namespace System.Text

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?