I have following piece of code:
byte[] snapthotBytes, snapthotBytes2;
string stringOutput, stringOutput2;
IInvestigationDump investigationDump = new HtmlSnapshotDump();
using (TextWriter writer = new StringWriter())
{
investigationDump.Dump(investigation, writer);
snapthotBytes = Encoding.UTF8.GetBytes(writer.ToString());
stringOutput = writer.ToString();
} // end of first implementation
using (MemoryStream stream = new MemoryStream())
using (TextWriter writer = new StreamWriter(stream))
{
investigationDump.Dump(investigation, writer);
stream.Seek(0, SeekOrigin.Begin);
var reader = new StreamReader(stream);
stringOutput2 = reader.ReadToEnd();
snapthotBytes2 = stream.ToArray();
} // end of second implementation
// stringOutput != stringOutput2 - content wise
// snapthotBytes != snapthotBytes2 - content wise
Some introduction:
- Dump method just traverses investigation object and renders an HTML report (by writing to a writer object).
- Appart from the fact that StringWriter uses UTF-16 encoding and XML declaration will differ, both stringOutput & stringOutput2 should have the same content. Dump methods signature is:
void Dump(IInvestigation investigation, TextWriter writer);
- Dump method just writes to a writer, without any conditions etc.
At first I used MemoryStream code snippet, it was easier as I received byte[] straightaway. But soon I realized a bug. Amazingly it turned out that stringOutput2 (produced by MemoryStream solution) is trimmed, it is shorter! It just ends with trimmed HTML content:
<tr>
<td>Certificate or License No</td>
<td class="value">Friuan</td>
<td>Place of Issue</td>
<td class="value">Foruan</td>
</tr>
<tr>
<td>Award Date</td>
<td
How about
before trying to read the stream?