当前位置: 首页>>代码示例>>C#>>正文


C# Encoding.GetPreamble方法代码示例

本文整理汇总了C#中System.Text.Encoding.GetPreamble方法的典型用法代码示例。如果您正苦于以下问题:C# Encoding.GetPreamble方法的具体用法?C# Encoding.GetPreamble怎么用?C# Encoding.GetPreamble使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Text.Encoding的用法示例。


在下文中一共展示了Encoding.GetPreamble方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Init

        private void Init(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen)
        {
            _stream = stream;
            _encoding = encoding;
            _decoder = encoding.GetDecoder();
            if (bufferSize < MinBufferSize)
            {
                bufferSize = MinBufferSize;
            }

            _byteBuffer = new byte[bufferSize];
            _maxCharsPerBuffer = encoding.GetMaxCharCount(bufferSize);
            _charBuffer = new char[_maxCharsPerBuffer];
            _byteLen = 0;
            _bytePos = 0;
            _detectEncoding = detectEncodingFromByteOrderMarks;

            // Encoding.GetPreamble() always allocates and returns a new byte[] array for
            // encodings that have a preamble.
            // We can avoid repeated allocations for the default and commonly used Encoding.UTF8
            // encoding by using our own private cached instance of the UTF8 preamble.
            // We specifically look for Encoding.UTF8 because we know it has a preamble,
            // whereas other instances of UTF8Encoding may not have a preamble enabled, and
            // there's no public way to tell if the preamble is enabled for an instance other
            // than calling GetPreamble(), which we're trying to avoid.
            // This means that other instances of UTF8Encoding are excluded from this optimization.
            _preamble = object.ReferenceEquals(encoding, Encoding.UTF8) ?
                (s_utf8Preamble ?? (s_utf8Preamble = encoding.GetPreamble())) :
                encoding.GetPreamble();

            _checkPreamble = (_preamble.Length > 0);
            _isBlocked = false;
            _closable = !leaveOpen;
        }
开发者ID:antiufo,项目名称:Shaman.ValueString,代码行数:34,代码来源:ValueStringStreamReader.cs

示例2: ClassificationResultParser

        /// <summary>
        /// Static constructor.
        /// </summary>
        public ClassificationResultParser()
        {
            // The encoding of the string result.
            _encoding = Encoding.Unicode;

            // Byte order mark; should be removed from the result before deserializing it.
            _byteOrderMark = _encoding.GetString(_encoding.GetPreamble());
        }
开发者ID:Ogonik,项目名称:LWS,代码行数:11,代码来源:ClassificationResultParser.cs

示例3: ReadOnlyStreamWithEncodingPreamble

        public ReadOnlyStreamWithEncodingPreamble(Stream innerStream, Encoding encoding)
        {
            Contract.Assert(innerStream != null);
            Contract.Assert(innerStream.CanRead);
            Contract.Assert(encoding != null);

            _innerStream = innerStream;

            // Determine whether we even have a preamble to be concerned about
            byte[] preamble = encoding.GetPreamble();
            int preambleLength = preamble.Length;
            if (preambleLength <= 0)
            {
                return;
            }

            // Create a double sized buffer, and read enough bytes from the stream to know
            // whether we have a preamble present already or not.
            int finalBufferLength = preambleLength * 2;
            byte[] finalBuffer = new byte[finalBufferLength];
            int finalCount = preambleLength;
            preamble.CopyTo(finalBuffer, 0);

            // Read the first bytes of the stream and see if they already contain a preamble
            for (; finalCount < finalBufferLength; finalCount++)
            {
                int b = innerStream.ReadByte();
                if (b == -1)
                {
                    break;
                }
                finalBuffer[finalCount] = (byte)b;
            }

            // Did we read enough bytes to do the comparison?
            if (finalCount == finalBufferLength)
            {
                bool foundPreamble = true;
                for (int idx = 0; idx < preambleLength; idx++)
                {
                    if (finalBuffer[idx] != finalBuffer[idx + preambleLength])
                    {
                        foundPreamble = false;
                        break;
                    }
                }

                // If we found the preamble, then just exclude it from the data that we return
                if (foundPreamble)
                {
                    finalCount = preambleLength;
                }
            }

            _remainingBytes = new ArraySegment<byte>(finalBuffer, 0, finalCount);
        }
开发者ID:marojeri,项目名称:aspnetwebstack,代码行数:56,代码来源:ReadOnlyStreamWithEncodingPreamble.cs

示例4: GetBytes

        private byte[] GetBytes(Encoding encoding, string source)
        {
            var preamble = encoding.GetPreamble();
            var content = encoding.GetBytes(source);

            var bytes = new byte[preamble.Length + content.Length];
            preamble.CopyTo(bytes, 0);
            content.CopyTo(bytes, preamble.Length);

            return bytes;
        }
开发者ID:riversky,项目名称:roslyn,代码行数:11,代码来源:EncodedStringTextTests.cs

示例5: RemovePreamble

        public static string RemovePreamble(this string str, Encoding encoding)
        {
            var bytes = encoding.GetBytes(str);
            var preamble = encoding.GetPreamble();

            if (bytes.Length < preamble.Length || preamble.Where((p, i) => p != bytes[i]).Any())
            {
                return str;
            }

            return encoding.GetString(bytes.Skip(preamble.Length).ToArray());
        }
开发者ID:spapaseit,项目名称:qoam,代码行数:12,代码来源:StringExtensions.cs

示例6: GetBytes

        protected byte[] GetBytes(Encoding encoding, string source)
        {
            currentEncoding = encoding;

            var preamble = encoding.GetPreamble();
            var content = encoding.GetBytes(source);

            byte[] bytes = new byte[preamble.Length + content.Length];
            preamble.CopyTo(bytes, 0);
            content.CopyTo(bytes, preamble.Length);

            return bytes;
        }
开发者ID:riversky,项目名称:roslyn,代码行数:13,代码来源:StringTextTests_Default.cs

示例7: StringToStream

        public static Stream StringToStream(string inputContents, Encoding encoding)
        {
            //MDF Serialization requires the preamble
            var preamble = encoding.GetPreamble();
            var body = encoding.GetBytes(inputContents);

            var stream = new MemoryStream(preamble.Length + body.Length);

            stream.Write(preamble, 0, preamble.Length);
            stream.Write(body, 0, body.Length);
            stream.Position = 0;

            return stream;
        }
开发者ID:Cireson,项目名称:EntityFramework6,代码行数:14,代码来源:FileUtils.cs

示例8: WritePreamble

		/// <summary>
		/// Writes the encoded text preamble (byte order mark [BOM]) to the stream.
		/// Preamble is required for unicode strings only. BE Unicode and UTF8 should not have it.
		/// Preamble should be written only once per frame at the beginning of the encoded text.
		/// </summary>
		/// <param name="encoding">The encoding.</param>
		public void WritePreamble(Encoding encoding)
		{
			if (encoding.CodePage == 1200) // Unicode
			{
				byte[] bom = encoding.GetPreamble();

				if (bom.Length == 0)
				{
					throw new ID3TagException("Unicode encoding must provide byte order mark (BOM).");
				}

				_stream.Write(bom, 0, bom.Length);
			}
		}
开发者ID:saitodisse,项目名称:id3tag.net,代码行数:20,代码来源:FrameDataWriter.cs

示例9: ByteCount

        /// <summary>
        ///		Computes the number of bytes required to encode the specified string.
        /// </summary>
        /// <remarks>
        /// 	Use this Method to calculate the exact number of bytes that <paramref name="s" />
        ///		will have when beeing encoded.
        ///		<b>Note:</b> No error checking is performed. Make sure <paramref name="s" /> and 
        ///		<paramref name="e" /> are not null.
        /// </remarks>
        /// <param name="s">The <see cref="String" /> to be encoded.</param>
        /// <param name="e">The <see cref="Encoding" /> with which the string will be encoded.</param>
        /// <param name="terminateString">
        /// 	When <b>true</b> one or two zero bytes depending on the Encoding will be appended; else
        ///		nothing wil be appended.
        /// </param>
        /// <returns>The number of bytes required to encode <paramref name="s" />.</returns>
        internal static int ByteCount(string s, Encoding e, bool terminateString)
        {
            int count = 0;

            if (terminateString) {
                if (e == utf16LE || e == utf16BE) {
                    count = 2;
                } else {
                    count = 1;
                }
            }

            count += e.GetByteCount(s) + e.GetPreamble().Length;

            return count;
        }
开发者ID:IljaN,项目名称:BPRename,代码行数:32,代码来源:StringEncoder.cs

示例10: StreamWithoutPreamble

        public void StreamWithoutPreamble(Encoding encoding, bool includePreambleInInputStream)
        {
            using (MemoryStream inputStream = new MemoryStream())
            {
                // Arrange
                string message = "Hello, world" + Environment.NewLine     // English
                               + "こんにちは、世界" + Environment.NewLine  // Japanese
                               + "مرحبا، العالم";                       // Arabic

                byte[] preamble = encoding.GetPreamble();
                byte[] encodedMessage = encoding.GetBytes(message);

                if (includePreambleInInputStream)
                {
                    inputStream.Write(preamble, 0, preamble.Length);
                }

                inputStream.Write(encodedMessage, 0, encodedMessage.Length);

                byte[] expectedBytes = new byte[preamble.Length + encodedMessage.Length];
                preamble.CopyTo(expectedBytes, 0);
                encodedMessage.CopyTo(expectedBytes, preamble.Length);

                inputStream.Seek(0, SeekOrigin.Begin);

                using (ReadOnlyStreamWithEncodingPreamble wrapperStream = new ReadOnlyStreamWithEncodingPreamble(inputStream, encoding))
                {
                    // Act
                    int totalRead = 0;
                    byte[] readBuffer = new byte[expectedBytes.Length];

                    while (totalRead < readBuffer.Length)
                    {
                        int read = wrapperStream.Read(readBuffer, totalRead, readBuffer.Length - totalRead);
                        totalRead += read;

                        if (read == 0)
                            break;
                    }

                    // Assert
                    Assert.Equal(expectedBytes.Length, totalRead);
                    Assert.Equal(expectedBytes, readBuffer);
                    Assert.Equal(0, wrapperStream.Read(readBuffer, 0, 1));  // Make sure there are no stray bytes left in the stream
                }
            }
        }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:47,代码来源:ReadOnlyStreamWithEncodingPreambleTest.cs

示例11: AsStream

        public static Stream AsStream(this string value, Encoding encoding)
        {
            var memoryStream = new MemoryStream();

            try
            {
                var bytes = encoding.GetPreamble();
                memoryStream.Write(bytes, 0, bytes.Length);
                bytes = encoding.GetBytes(value);
                memoryStream.Write(bytes, 0, bytes.Length);
                memoryStream.Seek(0, SeekOrigin.Begin);
                return memoryStream;
            }
            catch
            {
                memoryStream.Dispose();
                throw;
            }
        }
开发者ID:Berzeger,项目名称:NuGet,代码行数:19,代码来源:StreamExtensions.cs

示例12: GetBuffer

		/// <summary>
		/// Returns a byte array containing the text encoded by a specified encoding &amp; bom.
		/// </summary>
		/// <param name="text">The text to encode.</param>
		/// <param name="encoding">The encoding.</param>
		/// <param name="hadBom">If set to <c>true</c> a bom will be prepended.</param>
		public static byte[] GetBuffer (string text, Encoding encoding, bool hadBom)
		{
			using (var stream = new MemoryStream ()) {
				if (hadBom) {
					var bom = encoding.GetPreamble ();
					if (bom != null && bom.Length > 0)
						stream.Write (bom, 0, bom.Length);
				}
				byte[] bytes = encoding.GetBytes (text);
				stream.Write (bytes, 0, bytes.Length);
				return stream.GetBuffer ();
			}
		}
开发者ID:kdubau,项目名称:monodevelop,代码行数:19,代码来源:TextFileUtility.cs

示例13: DetermineEncoding

        private int DetermineEncoding(byte[] srcBuffer, int srcOffset) {
            _Encoding = null;
            switch(AutoDetectEncoding(srcBuffer, srcOffset))
            {
            case UniCodeBE://2:
                _Encoding = Encoding.BigEndianUnicode;
                break;
            case UniCode: //3:
                _Encoding = Encoding.Unicode;
                break;
            case UCS4BE: //4:
            case UCS4BEB: // 5:
                _Encoding = Ucs4Encoding.UCS4_Bigendian;
                break;

            case UCS4: //6:
            case UCS4B: //7:
                _Encoding = Ucs4Encoding.UCS4_Littleendian;
                break;

            case UCS434://8:
            case UCS434B: //9:
                _Encoding = Ucs4Encoding.UCS4_3412;
                break;
            case UCS421: //10:
            case UCS421B://11:
                _Encoding = Ucs4Encoding.UCS4_2143;
                break;

            case EBCDIC: //12: ebcdic
                throw new XmlException(Res.Xml_UnknownEncoding, "ebcdic", LineNum, LinePos);
                //break;
            case UTF8: //13:
                _EncodingSetByDefault = true;
                _Encoding = new UTF8Encoding(true);
                break;
            default:
                _Encoding = new UTF8Encoding(true, true);

                break;
            }

            _Decoder = _Encoding.GetDecoder();
            _PermitEncodingChange = true;

            if(_Encoding != null)
                _nCodePage = _Encoding.CodePage;
            //_achText = new char[_nSize+1];

            int startDecodingIndex = 0;
            int preambleLength = 0;
            try {
                byte[] preamble = _Encoding.GetPreamble();
                preambleLength = preamble.Length;
                bool hasByteOrderMark = true;
                for (int i = srcOffset; i < srcOffset + preambleLength && hasByteOrderMark; i++) {
                    hasByteOrderMark &= (srcBuffer[i] == preamble[i - srcOffset]);
                }
                if (hasByteOrderMark) {
                    startDecodingIndex = preambleLength;
                }
            }
            catch (Exception) {
            }

            return startDecodingIndex;

        }
开发者ID:ArildF,项目名称:masters,代码行数:68,代码来源:xmlscanner.cs

示例14: WriteText

		public static void WriteText (string fileName, string text, Encoding encoding, bool hadBom)
		{
			var tmpPath = WriteTextInit (fileName, text, encoding);
			using (var stream = new FileStream (tmpPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write)) {
				if (hadBom) {
					var bom = encoding.GetPreamble ();
					if (bom != null && bom.Length > 0)
						stream.Write (bom, 0, bom.Length);
				}
				byte[] bytes = encoding.GetBytes (text);
				stream.Write (bytes, 0, bytes.Length);
			}
			WriteTextFinal (tmpPath, fileName);
		}
开发者ID:kdubau,项目名称:monodevelop,代码行数:14,代码来源:TextFileUtility.cs

示例15: WriteTextAsync

		public static async Task WriteTextAsync (string fileName, string text, Encoding encoding, bool hadBom)
		{
			var tmpPath = WriteTextInit (fileName, text, encoding);
			using (var stream = new FileStream (tmpPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write, bufferSize: DefaultBufferSize, options: FileOptions.Asynchronous)) {
				if (hadBom) {
					var bom = encoding.GetPreamble ();
					if (bom != null && bom.Length > 0)
						await stream.WriteAsync (bom, 0, bom.Length).ConfigureAwait (false);
				}
				byte[] bytes = encoding.GetBytes (text);
				await stream.WriteAsync (bytes, 0, bytes.Length).ConfigureAwait (false);
			}
			WriteTextFinal (tmpPath, fileName);
		}
开发者ID:kdubau,项目名称:monodevelop,代码行数:14,代码来源:TextFileUtility.cs


注:本文中的System.Text.Encoding.GetPreamble方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。