public void TestByteStreamReading() { // Test bytes using (ByteStream stream = new ByteStream(testData)) { for (int i = 0; i < testData.Length; i++) { Assert.AreEqual(testData[i], stream.ReadByte(), "ReadByte returned incorrect value"); } } // Test ushorts using (ByteStream stream = new ByteStream(testData)) { Assert.AreEqual(0x0841, stream.ReadUshort()); Assert.AreEqual(0x6472, stream.ReadUshort()); Assert.AreEqual(0x6965, stream.ReadUshort()); Assert.AreEqual(0x6e6e, stream.ReadUshort()); } // Test uints using (ByteStream stream = new ByteStream(testData)) { Assert.AreEqual(0x08416472, stream.ReadUint()); Assert.AreEqual(0x69656e6e, stream.ReadUint()); } // Test string using (ByteStream stream = new ByteStream(testData)) { Assert.AreEqual("Adrienne", stream.ReadString(stream.ReadByte(), Encoding.ASCII)); } }
public void TestByteStreamNumberWriting() { ByteStream stream = new ByteStream(); stream.WriteByte(0x08); stream.WriteUshort(0x4164); stream.WriteUint(0x7269656e); stream.WriteByteArray(new byte[] {0x6e, 0x65}); Assert.AreEqual(testData.Length, stream.GetByteCount()); ByteStream testStream = new ByteStream(stream.GetBytes()); Assert.AreEqual("Adrienne", testStream.ReadString(testStream.ReadByte(), Encoding.ASCII)); }
/// <summary> /// Parses a byte buffer into an <see cref="BartID"/> object /// </summary> private void ReadIconInfo(byte[] buffer, UserInfo userinfo) { using (ByteStream iconStream = new ByteStream(buffer)) { int iconStreamSize = iconStream.GetByteCount(); while (iconStream.CurrentPosition + 4 <= iconStreamSize) { BartID item = new BartID(iconStream); // Find the end of the current data item in the stream int endDataPosition = iconStream.CurrentPosition + item.Data.Length; switch (item.Type) { case BartTypeId.BuddyIcon: if (!GraphicsManager.IsBlankIcon(item.Data)) { userinfo.Icon = item; } break; case BartTypeId.StatusString: // Available message using (ByteStream messageStream = new ByteStream(item.Data)) { Encoding encoding = Encoding.UTF8; byte[] amessage = new byte[0]; if (messageStream.HasMoreData) { // Pull the message to a byte array, assume at first that the encoding // is UTF-8. If existing encoding information exists, use that instead amessage = messageStream.ReadByteArray(messageStream.ReadByte()); // Check if there's encoding information available if (messageStream.HasMoreData) { // Check to see if the encoding's been specified if (messageStream.ReadUshort() == 0x0001) { messageStream.AdvanceOffset(2); string encodingStr = messageStream.ReadString(messageStream.ReadUshort(), Encoding.ASCII); // Try to use the encoding from the byte stream try { encoding = Encoding.GetEncoding(encodingStr); } catch (ArgumentException) { Logging.WriteString( "ReadIconInfo: Got unknown encoding for available message (" + encodingStr + "), falling back to UTF-8"); encoding = Encoding.UTF8; } } } } userinfo.AvailableMessage = Encoding.Unicode.GetString( Encoding.Convert(encoding, Encoding.Unicode, amessage)); } break; default: break; } } } }