public void NegTest1() { UTF8Encoding utf8 = new UTF8Encoding(); int charCount = -1; Assert.Throws<ArgumentOutOfRangeException>(() => { int maxByteCount = utf8.GetMaxByteCount(charCount); }); }
static public int GetMaxByteCount(IntPtr l) { try { System.Text.UTF8Encoding self = (System.Text.UTF8Encoding)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); var ret = self.GetMaxByteCount(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
/// <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); }
// ------------------------------------------------------------------------------------ /// <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; }
public void TestMaxByteCount() { UTF8Encoding UTF8enc = new UTF8Encoding (); #if NET_2_0 // maybe under .NET 2.0 insufficient surrogate pair is // just not handled, and 3 is Preamble size. Assert.AreEqual (153, UTF8enc.GetMaxByteCount(50), "UTF #1"); #else Assert.AreEqual (200, UTF8enc.GetMaxByteCount(50), "UTF #1"); #endif }
/// <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); }
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"); }
public void PosTest1() { UTF8Encoding utf8 = new UTF8Encoding(); int charCount = 0; int maxByteCount = utf8.GetMaxByteCount(charCount); }
/// <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); }
/// <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); }
public void TestMaxByteCount() { UTF8Encoding UTF8enc = new UTF8Encoding (); Assertion.AssertEquals ("UTF #1", 200, UTF8enc.GetMaxByteCount(50)); }