.NET Data URI

My brain often follows the following path when writing code to operate on a File:

  1. I need to get data from this file
  2. I should pass around a Stream, it's more general.
  3. Who opens the stream? Who closes it? What if I try to read from stream that's already closed? ...
  4. I'll pass around a Stream factory so the stream management is all in one method
  5. I need to write another factory class like I need a hole in the head
  6. I'll use a URI

And so I end up passing around instances of URI which is actually a pretty good solution. When I need to unit test, I use this library to pass self-contained bits of data around without needing to write temp files and then use file: URIs or similar.

Data URIs are defined by RFC 2397. Ian Hixson maintains the Data URI kitchen wherein you can play with Data URIs.

Usage:

DataWebRequest.DataWebRequest.Register(); //only needs to be called once

//now we can use the data: uri scheme
System.Net.WebClient wc = new System.Net.WebClient();
String s = wc.DownloadString("data:text/plain,data data");
//s is now "data data"

The release dll has no dependencies. You will need NUnit to run the unit tests. This library has been placed in the public domain.