The Rx team had a two-part series last month demonstrating one way to use Rx on the server: asynchronous Stream and asynchronous StreamReader. In the asynchronous StreamReader example, they punt on the decoding issue, instead demonstrating how to split the text into lines.
As it turns out, decoding (and encoding) are quite simple in Rx. The code below should be clear to anyone who’s been following the Rx team blog posts:
This class defines two operators (Encode and Decode) which can be used like this:
Note that I’ve defined the Encode and Decode operators as working on “chunks” of data. As such, they don’t really “fit in” with most LINQ and Rx operators, which work on individual data elements. However, this approach makes sense any time there’s buffered reading going on. The Encode and Decode operators here will work fine with the Rx team’s example AsyncRead operator.
Also note that these simple Encode and Decode operators will not treat encoding preambles in any special way (including Unicode byte order marks). They won’t prefix the encoded output with a preamble, nor will they detect any preambles when decoding.
About Stephen Cleary
Stephen Cleary is a Christian, husband, father, and programmer living in Northern Michigan.