Framework limitations

It really sucks when you’re working away on something and it’s all going really well, and then you run into a limitation in something you can’t control which throws a wrench in things…

As you may have guessed from the uudecode code I posted yesterday, I’m playing with code that grabs articles from Usenet.  I’m trying to decode binary postings, and the other (and these days, more common) format is called yEnc.  yEnc is a pretty reasonable compromise between the ASCII nature of Usenet and efficiency. 

The way yEnc works is instead of encoding each 8 bit character as 6 bits (like UUEncode), it uses all 8 bits, and only escapes certain characters.  In a nutshell, you take the source byte, add 42 to it, and then if it’s a null, carriage return, linefeed or equal sign, you escape it by prefixing it with an equal sign and the escaped character with 64 added to it.

Except for this little detour into binary land, NNTP is an ASCII protocol, with plain English commands.  Each line ends with a carriage return / linefeed pair. 

This makes the StreamReader and StreamWriter classes perfect for working with it.  You can write something like:

  writer.WriteLine(”ARTICLE 12334”);
  string statusLine = reader.ReadLine();

Problem is, there’s no way to switch a StreamReader into “binary mode”.  yEnc is writing 128 bytes of data per line, and my ReadLine is returning strings that are 120, 126, 121, etc., bytes long, as the binary is confusing whatever text encoding I’m using.

I can’t just switch to using the StreamReader.BaseStream directly, because the StreamReader has data buffered.

It seems like fairly often I start down the road to something that should be easy, and run into a reason why the whole thing’s a lot more difficult than it should be.  I ran into the same thing trying to use the XmlReader to process XMPP:  The .NET “pull model” XML parser won’t return a node until it has one entity AFTER the element it’s parsing, and that breaks the ability to use it with XMPP (since it sends you just enough data and expects you to parse it). 

I can fix the NNTP problem by writing my own stream that can switch between ASCII and binary modes.. just though I’d come rant about it a bit first.  :)