public unsafe void CharPeek() { byte[] data; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(ms)) for (int count = 0; count < 0xD800; count++) { writer.Write((char)count); } data = ms.ToArray(); } fixed(byte *pData = data) { BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length); for (int count = 0; count < 0xD800; count++) { Assert.AreEqual(reader.PeekChar(), (char)count, "BinaryMemoryReader Char incompatible to BinaryWriter."); reader.ReadChar(); } } }
public unsafe void CharRead() { byte[] data; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(ms)) for (int count = 0; count < 0xD800; count++) { writer.Write((char)count); } data = ms.ToArray(); } fixed(byte *pData = data) { BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length); for (int count = 0; count < 0xD800; count++) { char c = reader.ReadChar(); Assert.AreEqual(c, (char)count, $"BinaryMemoryReader Char incompatible to BinaryWriter: 0x{count.ToString("X04")} != 0x{((int)c).ToString("X04")}."); } } }
public unsafe void CharLimits() { byte[] data; using (MemoryStream ms = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(ms)) writer.Write('Ä'); data = ms.ToArray(); } fixed(byte *pData = data) { BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length - 1); try { reader.ReadChar(); Assert.Fail("Should have thrown an OutOfMemoryException."); } catch (OutOfMemoryException) { } catch (Exception) { Assert.Fail("Should have thrown an OutOfMemoryException."); } BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length - 1); try { writer.Write('Ä'); Assert.Fail("Should have thrown an OutOfMemoryException."); } catch (OutOfMemoryException) { } catch (Exception) { Assert.Fail("Should have thrown an OutOfMemoryException."); } } }
/// <summary> /// Reads from the stream up to the specified delimiter character. /// </summary> /// <param name="delimiter">The character that signals the end of the string.</param> /// <returns>A string formed from characters up to the first instance of the specified delimeter.</returns> protected string ReadString(BinaryMemoryReader reader, char delimiter) { StringBuilder sb = new StringBuilder(); char c; // sift through each character until we hit the delimiter while((c = reader.ReadChar()) != delimiter) { sb.Append(c); } // return the accumulated string return sb.ToString(); }