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


C# UTF8Encoding.GetMaxByteCount方法代码示例

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


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

示例1: NegTest1

 public void NegTest1()
 {
     UTF8Encoding utf8 = new UTF8Encoding();
     int charCount = -1;
     Assert.Throws<ArgumentOutOfRangeException>(() =>
     {
         int maxByteCount = utf8.GetMaxByteCount(charCount);
     });
 }
开发者ID:johnhhm,项目名称:corefx,代码行数:9,代码来源:UTF8EncodingGetMaxByteCount.cs

示例2: WriteString

        /// <inheritdoc/>
        public override void WriteString(string value, UTF8Encoding encoding)
        {
            ThrowIfDisposed();
            
            var maxLength = encoding.GetMaxByteCount(value.Length) + 5;
            PrepareToWrite(maxLength);

            int actualLength;
            var segment = _buffer.AccessBackingBytes(_position);
            if (segment.Count >= maxLength)
            {
                actualLength = encoding.GetBytes(value, 0, value.Length, segment.Array, segment.Offset + 4);

                var lengthPlusOne = actualLength + 1;
                segment.Array[segment.Offset] = (byte)lengthPlusOne;
                segment.Array[segment.Offset + 1] = (byte)(lengthPlusOne >> 8);
                segment.Array[segment.Offset + 2] = (byte)(lengthPlusOne >> 16);
                segment.Array[segment.Offset + 3] = (byte)(lengthPlusOne >> 24);
                segment.Array[segment.Offset + 4 + actualLength] = 0;
            }
            else
            {
                byte[] bytes;
                if (maxLength <= _tempUtf8.Length)
                {
                    bytes = _tempUtf8;
                    actualLength = encoding.GetBytes(value, 0, value.Length, bytes, 0);
                }
                else
                {
                    bytes = encoding.GetBytes(value);
                    actualLength = bytes.Length;
                }

                var lengthPlusOneBytes = BitConverter.GetBytes(actualLength + 1);

                _buffer.SetBytes(_position, lengthPlusOneBytes, 0, 4);
                _buffer.SetBytes(_position + 4, bytes, 0, actualLength);
                _buffer.SetByte(_position + 4 + actualLength, 0);
            }

            SetPositionAfterWrite(_position + actualLength + 5);
        }
开发者ID:cihanozhan,项目名称:mongo-csharp-driver,代码行数:44,代码来源:ByteBufferStream.cs

示例3: DoRequest

        // ------------------------------------------------------------------------------------
        /// <summary>
        /// Send message to Snarl.
        /// Will UTF8 encode the message before sending.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="replyTimeout">(Optional - default = 1000)</param>
        /// <returns>Return zero or positive on succes. Negative on error.</returns>
        public static Int32 DoRequest(String request, UInt32 replyTimeout)
        {
            Int32 nReturn = -1;
            IntPtr nSendMessageResult = IntPtr.Zero;
            IntPtr ptrToUtf8Request = IntPtr.Zero;
            IntPtr ptrToCds = IntPtr.Zero;
            byte[] utf8Request = null;

            // Test if Snarl is running
            IntPtr hWnd = GetSnarlWindow();
            if (!IsWindow(hWnd))
                return -(Int32)SnarlStatus.ErrorNotRunning;

            try
            {
                // Convert to UTF8
                // utf8Request = StringToUtf8(request);
                UTF8Encoding utf8 = new UTF8Encoding();
                utf8Request = new byte[utf8.GetMaxByteCount(request.Length)];
                int convertCount = utf8.GetBytes(request, 0, request.Length, utf8Request, 0);

                // Create interop struct
                COPYDATASTRUCT cds = new COPYDATASTRUCT();
                cds.dwData = (IntPtr)0x534E4C03; // "SNL",3
                cds.cbData = convertCount;

                // Create unmanaged byte[] and copy utf8Request into it
                ptrToUtf8Request = Marshal.AllocHGlobal(convertCount);
                Marshal.Copy(utf8Request, 0, ptrToUtf8Request, convertCount);
                cds.lpData = ptrToUtf8Request;

                // Create unmanaged pointer to COPYDATASTRUCT
                ptrToCds = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(COPYDATASTRUCT)));
                Marshal.StructureToPtr(cds, ptrToCds, false);

                if (SendMessageTimeout(hWnd,
                          (uint)WindowsMessage.WM_COPYDATA,
                          (IntPtr)GetCurrentProcessId(),
                          ptrToCds,
                          SendMessageTimeoutFlags.SMTO_ABORTIFHUNG | SendMessageTimeoutFlags.SMTO_NOTIMEOUTIFNOTHUNG,
                          replyTimeout,
                          out nSendMessageResult) == IntPtr.Zero)
                {
                    // Error
                    int nError = Marshal.GetLastWin32Error();
                    if (nError == ERROR_TIMEOUT)
                        nReturn = -(Int32)SnarlStatus.ErrorTimedOut;
                    else
                        nReturn = -(Int32)SnarlStatus.ErrorFailed;
                }
                else
                    nReturn = unchecked((Int32)nSendMessageResult.ToInt64()); // Avoid aritmetic overflow error
            }
            finally
            {
                utf8Request = null;
                Marshal.FreeHGlobal(ptrToCds);
                Marshal.FreeHGlobal(ptrToUtf8Request);
            }

            return nReturn;
        }
开发者ID:TlhanGhun,项目名称:FileSystemSnarl,代码行数:70,代码来源:Snarl.cs

示例4: WriteString

        /// <inheritdoc/>
        public override void WriteString(string value, UTF8Encoding encoding)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (encoding == null)
            {
                throw new ArgumentNullException("encoding");
            }
            ThrowIfDisposed();

            byte[] bytes;
            int length;

            if (encoding.GetMaxByteCount(value.Length) <= _tempUtf8.Length)
            {
                bytes = _tempUtf8;
                length = encoding.GetBytes(value, 0, value.Length, _tempUtf8, 0);
            }
            else
            {
                bytes = encoding.GetBytes(value);
                length = bytes.Length;
            }

            WriteInt32(length + 1);
            _stream.Write(bytes, 0, length);
            _stream.WriteByte(0);
        }
开发者ID:ghovander,项目名称:mongo-csharp-driver,代码行数:31,代码来源:BsonStreamAdapter.cs

示例5: TestMaxByteCount

		public void TestMaxByteCount()
		{
			UTF8Encoding UTF8enc = new UTF8Encoding ();
			Encoding UTF8encWithBOM = new UTF8Encoding(true);

			Assert.AreEqual (153, UTF8enc.GetMaxByteCount(50), "UTF #1");
			Assert.AreEqual (UTF8enc.GetMaxByteCount(50), UTF8encWithBOM.GetMaxByteCount(50), "UTF #2");
		}
开发者ID:KonajuGames,项目名称:SharpLang,代码行数:8,代码来源:UTF8EncodingTest.cs

示例6: PosTest1

 public void PosTest1()
 {
     UTF8Encoding utf8 = new UTF8Encoding();
     int charCount = 0;
     int maxByteCount = utf8.GetMaxByteCount(charCount);
 }
开发者ID:johnhhm,项目名称:corefx,代码行数:6,代码来源:UTF8EncodingGetMaxByteCount.cs

示例7: ArgumentException

        /// <summary>
        /// Writes a BSON string to the stream.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="encoding">The encoding.</param>
        /// <exception cref="System.ArgumentException">
        /// UTF8 representation of a CString cannot contain null bytes.
        /// or
        /// UTF8 representation of a CString cannot contain null bytes.
        /// </exception>
        void IBsonStream.WriteBsonString(string value, UTF8Encoding encoding)
        {
            var maxLength = encoding.GetMaxByteCount(value.Length) + 5;
            PrepareToWrite(maxLength);

            int actualLength;
            var segment = _byteBuffer.AccessBackingBytes(_position);
            if (segment.Count >= maxLength)
            {
                actualLength = encoding.GetBytes(value, 0, value.Length, segment.Array, segment.Offset + 4);
                if (Array.IndexOf<byte>(segment.Array, 0, segment.Offset, actualLength) != -1)
                {
                    throw new ArgumentException("UTF8 representation of a CString cannot contain null bytes.");
                }

                var lengthPlusOne = actualLength + 1;
                segment.Array[segment.Offset] = (byte)lengthPlusOne;
                segment.Array[segment.Offset + 1] = (byte)(lengthPlusOne >> 8);
                segment.Array[segment.Offset + 2] = (byte)(lengthPlusOne >> 16);
                segment.Array[segment.Offset + 3] = (byte)(lengthPlusOne >> 24);
                segment.Array[segment.Offset + 4 + actualLength] = 0;
            }
            else
            {
                var bytes = encoding.GetBytes(value);
                if (bytes.Contains<byte>(0))
                {
                    throw new ArgumentException("UTF8 representation of a CString cannot contain null bytes.");
                }
                actualLength = bytes.Length;
                var lengthPlusOneBytes = BitConverter.GetBytes(actualLength + 1);

                _byteBuffer.WriteBytes(_position, lengthPlusOneBytes, 0, 4);
                _byteBuffer.WriteBytes(_position, bytes, 4, actualLength);
                _byteBuffer.WriteByte(_position + actualLength, 0);
            }

            SetPositionAfterWrite(_position + actualLength + 5);
        }
开发者ID:annikulin,项目名称:code-classifier,代码行数:49,代码来源:ByteBufferStream.cs

示例8: StreamIsClosed

        /// <summary>
        /// Writes a BSON string to the stream.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="encoding">The encoding.</param>
        void IBsonStream.WriteBsonString(string value, UTF8Encoding encoding)
        {
            if (!_isOpen)
            {
                StreamIsClosed();
            }
            EnsureWriteable();

            var maxLength = encoding.GetMaxByteCount(value.Length) + 5;
            var maxNewPosition = _position + maxLength;
            EnsurePosition(maxNewPosition);

            var length = encoding.GetBytes(value, 0, value.Length, _buffer, _position + 4);
            var lengthPlusOne = length + 1;
            _buffer[_position] = (byte)lengthPlusOne;
            _buffer[_position + 1] = (byte)(lengthPlusOne >> 8);
            _buffer[_position + 2] = (byte)(lengthPlusOne >> 16);
            _buffer[_position + 3] = (byte)(lengthPlusOne >> 24);
            _buffer[_position + 4 + length] = 0;

            SetPositionAfterWrite(length + 5);
        }
开发者ID:annikulin,项目名称:code-classifier,代码行数:27,代码来源:BsonMemoryStream.cs

示例9: TestMaxByteCount

 public void TestMaxByteCount()
 {
         UTF8Encoding UTF8enc = new UTF8Encoding ();
         Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
 }
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:5,代码来源:UTF8EncodingTest.cs


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