本文整理汇总了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);
});
}
示例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);
}
示例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;
}
示例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);
}
示例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");
}
示例6: PosTest1
public void PosTest1()
{
UTF8Encoding utf8 = new UTF8Encoding();
int charCount = 0;
int maxByteCount = utf8.GetMaxByteCount(charCount);
}
示例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);
}
示例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);
}
示例9: TestMaxByteCount
public void TestMaxByteCount()
{
UTF8Encoding UTF8enc = new UTF8Encoding ();
Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50));
}