static void ReadText(byte[] encoded, string expected) { using (var ms = new MemoryStream(encoded)) { var utf8 = new Utf8Stream(ms, false); var charbuf = new char[expected.Length]; var read = utf8.ReadCharacters(charbuf, 0, expected.Length); var actual = new string(charbuf); Assert.AreEqual(expected.Length, read, "Actual number of characters read differed from expected."); Assert.AreEqual(expected, actual, "Decoded string differs from source string."); } }
public void TestReadMultipleChars() { var mathLowercaseEl = "abc𝓁"; using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(mathLowercaseEl))) { var charbuf = new char[5]; var utf8 = new Utf8Stream(ms, false); var read = utf8.ReadCharacters(charbuf, 0, 5); var str = new string(charbuf); Assert.AreEqual(mathLowercaseEl, str); } }
public void TestReadChar() { // euro, feh, thorn, om, respectively var charBytes = new Dictionary<char, byte[]> { { '€', new byte[] { 0xE2, 0x82, 0xAC } }, { 'Ֆ', new byte[] { 0xD5, 0x96 } }, { 'þ', new byte[] { 0xC3, 0xBE } }, { 'ॐ', new byte[] { 0xE0, 0xA5, 0x90 } } }; foreach (var kvp in charBytes) { using (var ms = new MemoryStream(kvp.Value)) { var utf8 = new Utf8Stream(ms, false); var actual = utf8.ReadCharacter(); Assert.AreEqual(kvp.Key, actual); } } }
private string ReadString() { var sb = new StringBuilder(); var finalChunk = false; do { int charCount; var chunkIndicator = input.ReadByte(); if (chunkIndicator >= 0x00 && chunkIndicator <= 0x1F) { finalChunk = true; charCount = chunkIndicator; } else if (chunkIndicator == 'S' || chunkIndicator == 's') { finalChunk = chunkIndicator == 'S'; charCount = ReadLength(); } else { throw new SerializationException("Invalid Hessian 2.0 string."); } if (charCount == 0) { // We have no further work to do in this chunk; // read the next one. If another chunk exists, this one // is malformed. Most likely, this chunk is the first and // only chunk of an empty string. Either way, I don't // think that we care. continue; } using (var stringstream = new Utf8Stream(input)) { var charbuf = new char[charCount]; var charsRead = stringstream.ReadCharacters(charbuf, 0, charCount); if (charsRead != charCount) throw new SerializationException(string.Format("Invalid string chunk - expected {0} characters, found {1} characters.", charCount, charsRead)); sb.Append(charbuf); } /* var converter = new Utf8StreamConverter(input); for (var i = 0; i < charCount; ++i) { sb.Append(converter.ReadCharacter()); }*/ } while (!finalChunk); return sb.ToString(); }