.NET Process — working with binary output
Lately we discovered an issue while encoding Mp3 files with Lame. Our client reported encoded files we garbled; playable but watery — and full of pops and clicks.
We found this was due to interpreting the binary output from Lame as text — we had mistakenly employed Process.BeginOutputReadLine and its companion event OutputDataReceived.
By observing a Process using its OutputDataReceived event, clients can make asynchronous reads on a process’s StandardOutput.
Process.StandardOutput is a TextReader: it represents a reader that can read a sequential series of characters, i.e., it interprets its underlying stream as text.
When StandardOutput is being read asynchronously, the Process class monitors it, collecting characters into a string. Once it encounters a line ending, it notifies observers (handlers of its OutputDataReceived event), with the line of text it’s been collecting.
In short, the Process‘s underlying byte stream is converted to lines of text, and clients are notified one line at a time.
In doing so, some bytes are discarded: any bytes that (in the current encoding) represent line endings.
As a result of these missing bytes, our output Mp3s were playable, but sounded terrible.