Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new instance of <see cref="UTF32EncodingInfo"/>.
        /// </summary>
        /// <param name="encoding">The optional <see cref="UTF32Encoding"/> to use.</param>
        public UTF32EncodingInfo(
            UTF32Encoding encoding = null
            )
        {
            var isBigEndian = encoding != null && UTF16EncodingInfo.IsBigEndian(encoding);

            this.IsBigEndian = isBigEndian;
            this.Encoding    = encoding ?? new UTF32Encoding(isBigEndian, false, false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates the default encoding information for given <see cref="Encoding"/>.
        /// </summary>
        /// <param name="encoding">This <see cref="Encoding"/>.</param>
        /// <returns>A new instance of <see cref="IEncodingInfo"/>.</returns>
        /// <exception cref="InvalidOperationException">If this method could not automatically deduct which <see cref="IEncodingInfo"/> to create.</exception>
        public static IEncodingInfo CreateDefaultEncodingInfo(this Encoding encoding)
        {
            ArgumentValidator.ValidateNotNullReference(encoding);

            switch (encoding)
            {
#if !NETSTANDARD1_0 && !NETSTANDARD1_1
            case ASCIIEncoding ascii:
                return(ASCIIEncodingInfo.Instance);
#endif
            case UTF8Encoding utf8:
                return(new UTF8EncodingInfo(utf8));

            case UnicodeEncoding utf16:
                if (ReferenceEquals(utf16, Encoding.Unicode))
                {
                    return(new UTF16LEEncodingInfo(utf16));
                }
                else if (ReferenceEquals(utf16, Encoding.BigEndianUnicode) || UTF16EncodingInfo.IsBigEndian(utf16))
                {
                    return(new UTF16BEEncodingInfo(utf16));
                }
                else
                {
                    return(new UTF16LEEncodingInfo(utf16));
                }

#if NET_40 || NETSTANDARD1_5 || NET_45
            case UTF32Encoding utf32:
                return(new UTF32EncodingInfo(utf32));
#endif
            case null:
                return(null);

            default:
                throw new InvalidOperationException($"Could not create default encoding info for {encoding}.");
            }
        }