public string Decompress(string filename) { StringBuilder result = null; try { Stream s = new GZipInputStream(File.OpenRead(filename)); result = new StringBuilder(8192); UTF7Encoding encoding = new UTF7Encoding(true); int size = 2048; byte[] writeData = new byte[2048]; while (true) { size = s.Read(writeData, 0, size); if (size > 0) { result.Append(encoding.GetString(writeData,0,size)); } else { break; } } s.Close(); } // end try catch (GZipException) { throw new Exception("Error: The file being read contains invalid data."); } catch (FileNotFoundException) { throw new Exception("Error:The file specified was not found."); } catch (ArgumentException) { throw new Exception("Error: path is a zero-length string, contains only white space, or contains one or more invalid characters"); } catch (PathTooLongException) { throw new Exception("Error: The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters."); } catch (DirectoryNotFoundException) { throw new Exception("Error: The specified path is invalid, such as being on an unmapped drive."); } catch (IOException) { throw new Exception("Error: An I/O error occurred while opening the file."); } catch (UnauthorizedAccessException) { throw new Exception("Error: path specified a file that is read-only, the path is a directory, or caller does not have the required permissions."); } return result.ToString(); }
public void NegTest1() { Byte[] bytes = null; UTF7Encoding utf7 = new UTF7Encoding(); Assert.Throws<ArgumentNullException>(() => { string str = utf7.GetString(bytes, 0, 2); }); }
public void PosTest1() { Byte[] bytes = new Byte[] { 85, 84, 70, 56, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101}; UTF7Encoding utf7 = new UTF7Encoding(); string str = utf7.GetString(bytes, 0, bytes.Length); }
public void NegTest3() { Byte[] bytes = new Byte[] { 85, 84, 70, 56, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101}; UTF7Encoding utf7 = new UTF7Encoding(); Assert.Throws<ArgumentOutOfRangeException>(() => { string str = utf7.GetString(bytes, 0, -1); }); }
public void PosTest2() { int startIndex = 0; int count = 0; Byte[] bytes = new Byte[] { 85, 84, 70, 56, 32, 69, 110, 99, 111, 100, 105, 110, 103, 32, 69, 120, 97, 109, 112, 108, 101}; startIndex = _generator.GetInt32(-55) % bytes.Length; count = _generator.GetInt32(-55) % (bytes.Length - startIndex) + 1; UTF7Encoding utf7 = new UTF7Encoding(); string str = utf7.GetString(bytes, startIndex, count); }
public string ReadUDTString(string tagName) { lock (mSyncLock) { System.Text.UTF7Encoding utf = new System.Text.UTF7Encoding(); Logix.Tag lengthTag = new Logix.Tag(tagName + ".LEN"); Logix.Tag dataTag = new Logix.Tag(tagName + ".DATA[0]"); ReadTag(lengthTag); dataTag.Length = (int)lengthTag.Value; ReadTag(dataTag); if (dataTag.Value is sbyte) { return Convert.ToChar(dataTag.Value).ToString(); } else if (dataTag.Value is System.Array) { byte[] b = (byte[])dataTag.Value; return utf.GetString(b, 0, dataTag.Length); } else { throw new LogixTagException("Invalid tag type.", dataTag, 0); } } }
public void RFC1642_Example4 () { string UniCodeString = "\u0049\u0074\u0065\u006D\u0020\u0033\u0020\u0069\u0073\u0020\u00A3\u0031\u002E"; char[] expected = UniCodeString.ToCharArray (); byte[] UTF7Bytes = new byte[] { 0x49, 0x74, 0x65, 0x6D, 0x20, 0x33, 0x20, 0x69, 0x73, 0x20, 0x2B, 0x41, 0x4B, 0x4D, 0x2D, 0x31, 0x2E }; UTF7Encoding UTF7enc = new UTF7Encoding (); char[] actual = UTF7enc.GetChars (UTF7Bytes); // "Item 3 is +AKM-1." is decoded as "Item 3 is <POUND SIGN>1." AssertEquals ("UTF #1", expected [0], actual [0]); AssertEquals ("UTF #2", expected [1], actual [1]); AssertEquals ("UTF #3", expected [2], actual [2]); AssertEquals ("UTF #4", expected [3], actual [3]); AssertEquals ("UTF #5", expected [4], actual [4]); AssertEquals ("UTF #6", expected [5], actual [5]); AssertEquals ("UTF #7", expected [6], actual [6]); AssertEquals ("UTF #8", expected [7], actual [7]); AssertEquals ("UTF #9", expected [8], actual [8]); AssertEquals ("UTF #10", expected [9], actual [9]); AssertEquals ("UTF #11", expected [10], actual [10]); AssertEquals ("UTF #12", expected [11], actual [11]); AssertEquals ("UTF #13", expected [12], actual [12]); AssertEquals ("GetString", UniCodeString, UTF7enc.GetString (UTF7Bytes)); }
public void RFC1642_Example3 () { string UniCodeString = "\u65E5\u672C\u8A9E"; char[] expected = UniCodeString.ToCharArray (); byte[] UTF7Bytes = new byte[] { 0x2B, 0x5A, 0x65, 0x56, 0x6E, 0x4C, 0x49, 0x71, 0x65, 0x2D }; UTF7Encoding UTF7enc = new UTF7Encoding (); char[] actual = UTF7enc.GetChars (UTF7Bytes); // "+ZeVnLIqe-" is decoded as Japanese "nihongo" AssertEquals ("UTF #1", expected [0], actual [0]); AssertEquals ("UTF #2", expected [1], actual [1]); AssertEquals ("UTF #3", expected [2], actual [2]); AssertEquals ("GetString", UniCodeString, UTF7enc.GetString (UTF7Bytes)); }
public void RFC1642_Example2 () { string UniCodeString = "\u0048\u0069\u0020\u004D\u006F\u004D\u0020\u263A\u0021"; char[] expected = UniCodeString.ToCharArray (); byte[] UTF7Bytes = new byte[] { 0x48, 0x69, 0x20, 0x4D, 0x6F, 0x4D, 0x20, 0x2B, 0x4A, 0x6A, 0x6F, 0x41, 0x49, 0x51, 0x2D }; UTF7Encoding UTF7enc = new UTF7Encoding (); char[] actual = UTF7enc.GetChars (UTF7Bytes); // "Hi Mom +Jjo-!" is decoded as "Hi Mom <WHITE SMILING FACE>!" AssertEquals ("UTF #1", expected [0], actual [0]); AssertEquals ("UTF #2", expected [1], actual [1]); AssertEquals ("UTF #3", expected [2], actual [2]); AssertEquals ("UTF #4", expected [3], actual [3]); AssertEquals ("UTF #5", expected [4], actual [4]); AssertEquals ("UTF #6", expected [5], actual [5]); AssertEquals ("UTF #7", expected [6], actual [6]); AssertEquals ("UTF #8", expected [7], actual [7]); AssertEquals ("UTF #9", expected [8], actual [8]); AssertEquals ("GetString", UniCodeString, UTF7enc.GetString (UTF7Bytes)); }
public void RFC1642_Example1 () { string UniCodeString = "\u0041\u2262\u0391\u002E"; char[] expected = UniCodeString.ToCharArray (); byte[] UTF7Bytes = new byte [] {0x41, 0x2B, 0x49, 0x6D, 0x49, 0x44, 0x6B, 0x51, 0x2D, 0x2E}; UTF7Encoding UTF7enc = new UTF7Encoding (); char[] actual = UTF7enc.GetChars (UTF7Bytes); // "A+ImIDkQ-." is decoded as "A<NOT IDENTICAL TO><ALPHA>." see RFC 1642 AssertEquals ("UTF #1", expected [0], actual [0]); AssertEquals ("UTF #2", expected [1], actual [1]); AssertEquals ("UTF #3", expected [2], actual [2]); AssertEquals ("UTF #4", expected [3], actual [3]); AssertEquals ("GetString", UniCodeString, UTF7enc.GetString (UTF7Bytes)); }
public string Scan() { if (dispositivo_aberto && !Ocupado()) { string dadoss = string.Empty; UTF7Encoding enc = new UTF7Encoding(); int tamanho=155; byte[] dados = new byte[tamanho]; dispositivo.SendControlMessage(descritor.VendorId | (1 << 7), (int)Comando.USB_FUNC_SCAN, tamanho, dados); dadoss += enc.GetString(dados); return dadoss; } else { return string.Empty; } }
/// <summary>Recebe dados do dispositivo. /// </summary> public string Receber() { if (dispositivo_aberto && !Ocupado()) { string dadoss = string.Empty; UTF7Encoding enc = new UTF7Encoding(); byte[] dados = new byte[8]; dispositivo.SendControlMessage(descritor.VendorId | (1 << 7), 4, 3, dados); int tamanho = dados[1]; if (tamanho > 0) { Array.Resize(ref dados, tamanho); dispositivo.SendControlMessage(descritor.VendorId | (1 << 7), 5, tamanho, dados); dadoss += enc.GetString(dados); return dadoss; } else { // throw new Exception("Dispositivo não inicializado."); return string.Empty; } } else { // throw new Exception("Dispositivo não inicializado."); return string.Empty; } }
protected string Encode7Bit(byte[] data) { try { UTF7Encoding utf7 = new UTF7Encoding(); return utf7.GetString(data); } catch { return null; } }