/// <summary> /// Encodings the is right. /// </summary> /// <param name="encoding">The encoding.</param> /// <param name="data">The data.</param> /// <returns></returns> public static bool EncodingIsRight(Encoding encoding, byte[] data) { string text; byte[] bytes; //string text= encoding.GetString(data); //byte[] bytes = encoding.GetBytes(text); if (encoding.GetType().Name == "UnicodeEncoding") { text = ConversionHelper.GetStringUnicode(data); bytes = ConversionHelper.GetBytesUnicode(text); } else { text = ConversionHelper.GetString(data); bytes = ConversionHelper.GetBytes(text); } if (Algorithm.ArrayEqual(bytes, data)) { return true; } else { return false; } }
private static void StringToHeap (string s, Encoding e, bool mustBeEqual) { IntPtr p = UnixMarshal.StringToHeap (s, e); try { string _s = UnixMarshal.PtrToString (p, e); if (mustBeEqual) Assert.AreEqual (s, _s, "#TSTA (" + e.GetType() + ")"); } finally { UnixMarshal.FreeHeap (p); } }
private void VerifyBytesFollowedByChars(System.Text.Encoding encoding) { using (SerialPort com1 = TCSupport.InitFirstSerialPort()) using (SerialPort com2 = TCSupport.InitSecondSerialPort(com1)) { char[] xmitCharBuffer = TCSupport.GetRandomChars(numRndBytesToRead, TCSupport.CharacterOptions.Surrogates); char[] rcvCharBuffer = new char[xmitCharBuffer.Length]; byte[] xmitByteBuffer = new byte[numRndBytesToRead]; byte[] rcvByteBuffer = new byte[xmitByteBuffer.Length]; Random rndGen = new Random(-55); int numRead; Debug.WriteLine("Verifying read method does not alter stream of bytes after chars have been read with {0}", encoding.GetType()); for (int i = 0; i < xmitByteBuffer.Length; i++) { xmitByteBuffer[i] = (byte)rndGen.Next(0, 256); } com1.Encoding = encoding; com2.Encoding = encoding; com1.ReadTimeout = 500; com1.Open(); if (!com2.IsOpen) //This is necessary since com1 and com2 might be the same port if we are using a loopback { com2.Open(); } com2.Write(xmitCharBuffer, 0, xmitCharBuffer.Length); com2.Write(xmitByteBuffer, 0, xmitByteBuffer.Length); System.Threading.Thread.Sleep( (int) (((xmitByteBuffer.Length + com2.Encoding.GetByteCount(xmitCharBuffer) * 10.0) / com1.BaudRate) * 1000) + 500); System.Threading.Thread.Sleep(500); if (xmitCharBuffer.Length != (numRead = com1.Read(rcvCharBuffer, 0, rcvCharBuffer.Length))) { Fail("ERROR!!!: Expected to read {0} chars actually read {1}", xmitCharBuffer.Length, numRead); } if (encoding.EncodingName == System.Text.Encoding.UTF7.EncodingName) { //If UTF7Encoding is being used we we might leave a - in the stream if (com1.BytesToRead == xmitByteBuffer.Length + 1) { int byteRead; if ('-' != (char)(byteRead = com1.ReadByte())) { Fail("Err_29282naie Expected '-' to be left in the stream with UTF7Encoding and read {0}", byteRead); } } } if (xmitByteBuffer.Length != (numRead = com1.Read(rcvByteBuffer, 0, rcvByteBuffer.Length))) { Fail("ERROR!!!: Expected to read {0} bytes actually read {1}", xmitByteBuffer.Length, numRead); } for (int i = 0; i < xmitByteBuffer.Length; i++) { if (xmitByteBuffer[i] != rcvByteBuffer[i]) { Fail("ERROR!!!: Expected to read {0} actual read {1} at {2}", (int)xmitByteBuffer[i], (int)rcvByteBuffer[i], i); } } Assert.Equal(0, com1.BytesToRead); /*DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG * if(!retValue) { * for(int i=0; i<xmitCharBuffer.Length; ++i) { * Debug.WriteLine("(char){0}, ", (int)xmitCharBuffer[i]); * } * * for(int i=0; i<xmitCharBuffer.Length; ++i) { * Debug.WriteLine("{0}, ", (int)xmitByteBuffer[i]); * } * }*/ } }
private static int GetStringByteLength(IntPtr p, Encoding encoding) { Type encodingType = encoding.GetType (); int len = -1; // Encodings that will always end with a single null byte if (typeof(UTF8Encoding).IsAssignableFrom (encodingType) || typeof(UTF7Encoding).IsAssignableFrom (encodingType) || typeof(UnixEncoding).IsAssignableFrom (encodingType) || typeof(ASCIIEncoding).IsAssignableFrom (encodingType)) { len = checked ((int) Native.Stdlib.strlen (p)); } // Encodings that will always end with a 0x0000 16-bit word else if (typeof(UnicodeEncoding).IsAssignableFrom (encodingType)) { len = GetInt16BufferLength (p); } // Some non-public encoding, such as Latin1 or a DBCS charset. // Look for a sequence of encoding.GetMaxByteCount() bytes that are all // 0, which should be the terminating null. // This is "iffy", since it may fail for variable-width encodings; for // example, UTF8Encoding.GetMaxByteCount(1) = 4, so this would read 3 // bytes past the end of the string, possibly into garbage memory // (which is why we special case UTF above). else { len = GetRandomBufferLength (p, encoding.GetMaxByteCount(1)); } if (len == -1) throw new NotSupportedException ("Unable to determine native string buffer length"); return len; }
/// <summary> /// Safes the decode string. /// </summary> /// <param name="encoding">The encoding.</param> /// <param name="data">The data.</param> /// <returns></returns> public static object SafeDecodeString(Encoding encoding, byte[] data) { //string text = encoding.GetString(data); //byte[] bytes = encoding.GetBytes(text); //if (Algorithm.ArrayEqual(bytes, data)) //{ // return text; //} //else //{ // return data; //} string text; byte[] bytes; //string text= encoding.GetString(data); //byte[] bytes = encoding.GetBytes(text); if (encoding.GetType().Name == "UnicodeEncoding") { text = ConversionHelper.GetStringUnicode(data); bytes = ConversionHelper.GetBytesUnicode(text); } else { text = ConversionHelper.GetString(data); bytes = ConversionHelper.GetBytes(text); } if (Algorithm.ArrayEqual(bytes, data)) { return text; } else { return data; } }
public MultipleTextWriters (params TextWriter [] writers) { if (writers == null) throw new ArgumentNullException (nameof (writers)); if (writers.Length == 0) throw new ArgumentOutOfRangeException (nameof (writers)); this.writers = writers; encoding = writers [0].Encoding; foreach (var writer in writers) { if (encoding.GetType () != writer.Encoding.GetType ()) { throw new Exception ("Encodings mismatch between writers."); } } }