<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>All Things IT Blog &#187; C#</title>
	<atom:link href="http://www.enusbaum.com/blog/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.enusbaum.com/blog</link>
	<description>My little nerded out corner of the Internets!</description>
	<lastBuildDate>Tue, 18 Oct 2011 20:22:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>.NET StringBuilder &#8212; Fast, but not as fast as you think!</title>
		<link>http://www.enusbaum.com/blog/2009/05/net-stringbuilder-fast-but-not-as-fast-as-you-think/</link>
		<comments>http://www.enusbaum.com/blog/2009/05/net-stringbuilder-fast-but-not-as-fast-as-you-think/#comments</comments>
		<pubDate>Thu, 28 May 2009 20:18:47 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Microsoft .NET 3.0 / WinFX]]></category>
		<category><![CDATA[Reverse Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[.NET Optimization]]></category>
		<category><![CDATA[.NET Profiler]]></category>
		<category><![CDATA[.NET Profiling]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# Optimization]]></category>
		<category><![CDATA[C# String Concatenation]]></category>
		<category><![CDATA[C# String Manipulation]]></category>
		<category><![CDATA[C# Strings]]></category>
		<category><![CDATA[StringBuilder]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/?p=294</guid>
		<description><![CDATA[StringBuilder: Friend or Foe?]]></description>
			<content:encoded><![CDATA[<p>I recently ran into a situation where I was tasked to profile some .NET code and do some optimizations anywhere hot spots popped up. I was amazed to find out that one of the BIGGEST offenders in our code block was a simple call to <strong>StringBuilder.Append(char)</strong>. I had to take a step back and scratch my head and wonder if my profiler was confused.</p>
<p>I re-ran some tests using the <strong>StopWatch</strong> class to hard code some metrics into the application and they also confirmed the findings. What&#8217;s up? How could a class that everyone says you can use to your hearts content when it came to string concatenation was failing me?</p>
<p>Turns out, it was a mix of misuse and a common misconception about the <a title="MSDN Documentation -- StringBuilder Class" href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx" target="_blank">StringBuilder Class</a>.</p>
<p><span id="more-294"></span></p>
<p>One of the first things you learn while picking up .NET is that the <a title="MSDN Documentation -- StringBuilder Class" href="http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx" target="_blank">StringBuilder Class</a> is your friend when it comes to concatenating large strings in memory. It beats the pants off of <a title="MSDN Documentation -- String.Concat Method" href="http://msdn.microsoft.com/en-us/library/system.string.concat.aspx" target="_blank">String.Concat</a> and <a title="MSDN Documentation -- String.Format Method" href="http://msdn.microsoft.com/en-us/library/system.string.format.aspx" target="_blank">String.Format</a>, while also being a mutable object in the Framework utilizing an in-memory buffer.</p>
<p>I used <a title="Homepage -- JetBrains dotTrace Profiler" href="http://www.jetbrains.com/profiler/" target="_blank">JetBrains dotTrace</a> to help profile the application and it was very evident from the get-go that StringBuilder was causing the whole process to slow down.</p>
<p>The nature of my application was basically reading in a text buffer 1 character as a time, and using the <strong>StringBuilder</strong> as an output buffer. So for a 1k file, The method <strong>Append(char)</strong> would be called 1024 times. A 600k file would call <strong>Append(char)</strong> 614,400 times.</p>
<p>So why was I getting burned in execution time? The issue turned out to be two fold.</p>
<p>First, there&#8217;s overhead cost to the call. I don&#8217;t care how lightweight your method is, if you&#8217;re calling it <span style="text-decoration: underline;"><strong>SIX HUNDRED THOUSAND TIMES</strong></span>, it&#8217;s going to take a bit. Let alone a method who handles a string buffer in memory and string manipulation. So basically, no matter how fast StringBuilder actually is, it&#8217;s not a free call and you should consider the fact that the call still has overhead when architecting your solution.</p>
<p>Architecture brings me to my second point. While writing each character individually made sense initally, it seems that it was just lazy <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  The optimized route would have been calling Append with a SUBSTRING of the input buffer, this way we avoid the overhead of multiple calls by writing all the neccisary data in one big blob.</p>
<p>So 600,000 calls to <strong>StringBuilder.Append(char)</strong> becomes only a few hundred calls to <strong>StringBuilder.Append(string.Substring(start, count))</strong>. Sure, the Substring Virtual Method itself has overhead, but it&#8217;s still less than the thousands of calls to <strong>Append(char)</strong> that we&#8217;re saving ourself <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Conclusion?</p>
<p>StringBuilder is fast, but it&#8217;s not free. Take this into consideration when utilizing it while appending large data sets in small chunks. <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Cheers!</p>
<div class="su-linkbox" id="post-294-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2009/05/net-stringbuilder-fast-but-not-as-fast-as-you-think/&quot;&gt;.NET StringBuilder &#8212; Fast, but not as fast as you think!&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2009/05/net-stringbuilder-fast-but-not-as-fast-as-you-think/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Example Huffman Compression Routine in C#</title>
		<link>http://www.enusbaum.com/blog/2009/05/example-huffman-compression-routine-in-c/</link>
		<comments>http://www.enusbaum.com/blog/2009/05/example-huffman-compression-routine-in-c/#comments</comments>
		<pubDate>Fri, 22 May 2009 21:17:04 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Data Compression]]></category>
		<category><![CDATA[Deflate]]></category>
		<category><![CDATA[GZip]]></category>
		<category><![CDATA[Huffman Coding]]></category>
		<category><![CDATA[Huffman Compression]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/?p=285</guid>
		<description><![CDATA[This last week I decided to sit down and hash out a simple Huffman compression routine using C#. I&#8217;d never created a compression routine before from scratch (my past implementations were static for the sake of time savings), so I fleshed one out. I know that many examples exist elsewhere on the net&#8230;. but they [...]]]></description>
			<content:encoded><![CDATA[<p>This last week I decided to sit down and hash out a simple Huffman compression routine using C#. I&#8217;d never created a compression routine before from scratch (my past implementations were static for the sake of time savings), so I fleshed one out. I know that many examples exist elsewhere on the net&#8230;. but they all seemed overly complicated and up their own ass <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>I had a couple goals in mind while creating my routine:</p>
<p><strong>1. KEEP IT SIMPLE</strong> &#8212; A lot of routines out there WORK, but their code is too overly complicated for their own good. This over complication leads to slowness which brings me to my next goal <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  It should be a simple class that accepts input data, with simple public accessors that are easy to understand even for the novice developer (sorry folks, no asynchronous delegates). <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p><strong>2. MAKE IT FAST</strong> &#8212; When dealing with large amounts of data in C#, especially when running it through an algorithm, it&#8217;s all too easy to use all the handy built in virtual methods or using other build in tools which make coding easier with speed being the sacrifice. Die hard C++ developers will point to these routines as C#&#8217;s downfall as a legitimate language when it comes to data intensive tasks.</p>
<p><span id="more-285"></span></p>
<p>The class I came up with is pretty simple. I use a Generic List to store a collection of &#8220;Leaf&#8221; objects, which have several basic attributes that help not only identify its value but also its place in the tree. Using this method, I was able to utilize the built in methods of the List object (I know, for shame&#8230;. but it&#8217;s easier in this instance) by firing off anonymous delegates for searches and comparators. I&#8217;m not terribly worried about using these virtual methods here only because the creation and encoding of the tree is usually the smallest task in the process <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>The encoding and decoding is where I decided to focus on optimizations since this is where the BULK of the work is done. The .NET Framework has several methods that make working with binary data easy. You can use the Convert.ToString() method which allows you to pass in a BASE option, thus allowing you to convert any character to it&#8217;s binary representation. My original implementation used that method and the end result as embarrassingly slow <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
<p>I went back to the drawing board and thought to myself, &#8220;If I had to re-write this in C++, how would I handle the encoding?&#8221; Duh, I&#8217;d be using bitwise operators up the wazoo! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>After some recoding and pulling my hair out for a couple of hours, I was able to re-write the routine using bit operations and it works! On top of all that, it&#8217;s fast as all get out! My current benchmarks had it encoding a 1MB data set in under 1 second with ~50% compression. Not too shabby! Of course, compression ratios will vary depending on how normalized the input data set is.</p>
<p>Now I&#8217;m sure there&#8217;s some question you have on your mind and I&#8217;ll try to address them now:</p>
<p><strong>Q: Does it use a lot of memory?</strong></p>
<p><strong>A:</strong> You bet your sweet ass it does! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Seriously though, it&#8217;s only the method in which I setup the class that requires the memory. I establish an input buffer within the class that you can write the &#8216;raw&#8217; data to, which is then read from during encoding. In addition, during encoding I create an output buffer in memory where the &#8216;encoded&#8217; data is written. So it stands to reason that if you&#8217;re encoding 100MB of data, this routine can easily gobble up 200MB of RAM or more. The rule of thumb I found was File Size * 4 would be the memory requirement. There&#8217;s optimizations you can make that would lower the memory footprint (like, build the frequency table without buffering then read the input 1 byte at a time, say from a file), but I felt that would over complicate the solution and make it too focused for one specific instance. The current implementation is kept general for a reason <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Q: Could this be done faster in C++?</strong></p>
<p><strong>A:</strong> Yes, probably&#8230;. but not much faster. Although the code is written in C#, at run time the IL is compiled to x86. The bit operations we&#8217;re using would compile the exact same as a C++ routine (XOR is XOR, I don&#8217;t care what language you&#8217;re using). In addition, the encoding of the data itself is only using primitive native types which limits any cross language differences. In fact, you can paste the encode and decode routines into C++ and they work! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Your only speed improvement might be in the generation of the tree itself&#8230; but even then, that&#8217;s super small overhead when compared with the amount of data you&#8217;re probably compressing.</p>
<p>Of course, all that applied to the Encoding (Compression) side of the house, the decompression routine is pretty slow (about 3 seconds per 1MB of decompressed data) and could probably use some more optimizations.</p>
<p><strong>Q: Is this any better than using the built in GZip or Deflate classes available in System.IO.Compression?</strong></p>
<p><strong>A:</strong> It&#8217;s not even close to being in competition with those routines <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' />  This is really just a proof of concept for BASIC Huffman Coding, which doesn&#8217;t take into account advanced features of modern compression routines such as pattern or content mapping. This routine is slower (especially in decompression), so I wouldn&#8217;t go making anything like this your #1 choice for a compression routine if others are available <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  So use this for educational purposes only.</p>
<p><strong>Q: What version of the .NET Framework will this work with?</strong></p>
<p><strong>A:</strong> The code here was written in Visual Studio 2008 targeting .NET 3.5. I use some Framework 3.5 specific things (such as object initializers), but nothing that would make conversion difficult. I avoided LINQ only because I&#8217;m not entirely sold on the idea and I still like using anonymous delegates. <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  This code could be converted to Framework 2.0 with minor changes and possibly Framework 1.1, but that might require a little more effort.</p>
<p><strong>Q: What is the format used to store the compressed data?</strong></p>
<p><strong>A:</strong> I encode the decompression information within the final output stream. The output format is like this:</p>
<p>Bytes 0 &#8211; 8: Final Output Size (Not used, but there as a checksum if needed in the future)</p>
<p>Byte 9: Number of Bytes in the Decode Dictionary</p>
<p>Bytes 10 &#8211; n: Decode Dictionary</p>
<p>Between the Decode Dictionary and the actual data I add the characters &#8220;BCD&#8221; (which stands for <strong>B</strong>inary <strong>C</strong>oded <strong>D</strong>ata). This lets me know where the dictionary ends and the actual coded data begins. It helped during debugging and I figure it&#8217;d help anyone else out there as well while working with this routine <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Q: What&#8217;s with the essay? Just give me the code!</strong></p>
<p><strong>A:</strong> Fine! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  Seriously though, the only reason I&#8217;m doing such a long write-up on the code is to help people who are perhaps beginning to look into this sort of code for the first time and might have questions on why I did things a certain way. Understanding WHY the code was written helps understand how it operates.</p>
<p>So that&#8217;s the high and low of it! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope this helps someone out and if you have any questions, please feel free to leave a comment!</p>
<p>Cheers! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Huffman.zip</strong> &#8211; <a title="Huffman Coding in C#" href="http://www.enusbaum.com/blog/wp-content/uploads/Huffman.zip">Download</a> (3k)</p>
<div class="su-linkbox" id="post-285-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2009/05/example-huffman-compression-routine-in-c/&quot;&gt;Example Huffman Compression Routine in C#&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2009/05/example-huffman-compression-routine-in-c/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>2D WndrPong! using the Microsoft XNA Game Studio v2.0!</title>
		<link>http://www.enusbaum.com/blog/2007/12/2d-wndrpong-using-the-microsoft-xna-game-studio-v20/</link>
		<comments>http://www.enusbaum.com/blog/2007/12/2d-wndrpong-using-the-microsoft-xna-game-studio-v20/#comments</comments>
		<pubDate>Sat, 29 Dec 2007 00:28:04 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[Gaming]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Microsoft XNA]]></category>
		<category><![CDATA[2D Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Game Programming]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Pong!]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/2007/12/28/2d-wndrpong-using-the-microsoft-xna-game-studio-v20/</guid>
		<description><![CDATA[I decided to take some time this weekend to sit down and learn what I could about the latest release of Microsoft&#8217;s XNA Game Studio. I started out with a book I purchased called Microsoft XNA Game Studio Creators Guide, which turned out to be a terrible book. Most of the examples in this book [...]]]></description>
			<content:encoded><![CDATA[<p>I decided to take some time this weekend to sit down and learn what I could about the latest release of Microsoft&#8217;s <a href="http://www.xna.com/" title="Link -- Microsoft XNA Game Studio Website" target="_blank">XNA Game Studio</a>. I started out with a book I purchased called <a href="http://www.amazon.com/Microsoft%C2%AE-Game-Studio-Creators-Guide/dp/007149071X/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1198886266&amp;sr=8-1" title="Link to the worst book on XNA ever printed, ever." target="_blank">Microsoft XNA Game Studio Creators Guide</a>, which turned out to be a terrible book. Most of the examples in this book assume that you&#8217;re starting with a project the book provides on a Website, which already has hundreds of lines of code, custom shaders and everything built in&#8230; without even explaining how the code is working in the background.</p>
<p>After fumbling around with that for an hour or so an only succeeding in creating a small square on the screen, I headed over to Microsoft.com to see if any MSDN articles might exist to help me along in my &#8216;ground up&#8217; learning of XNA.  I was pleasantly surprised when I found a great article titled &#8220;<a href="http://msdn2.microsoft.com/en-us/library/bb203893.aspx" title="Best Starter Article ever for XNA :)" target="_blank">Your First Game: Microsoft XNA Game Studio in 2D</a>&#8220;. This was EXACTLY what I was looking for as it starts from the ground up, assuming the reader has never done game programming before, let alone 3D game programming. <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The example provided my Microsoft in this article is a simple 2D Texture of a cat that bounces around the window. I was so pleased with the ease of coding this, I thought to myself, &#8220;Heck, how hard could it be to recreate Pong?&#8221; <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So I set upon my task.</p>
<p>Several hours and many Coca-Cola bottles later I had not only my first XNA game <a href="http://www.enusbaum.com/blog//blog/wp-content/uploads/2007/12/wndrpong.thumbnail.gif" title="2D WndrPong! running in Windows" target="_blank">running in Windows</a>, but after purchasing the XNA Creators Club annual subscription from the XBox Live! marketplace for $99, I had it <a href="http://www.enusbaum.com/blog//blog/wp-content/uploads/2007/12/wndrpong_tv.jpg" title="2D WndrPong! running on the XBox 360!" target="_blank">running on my XBox 360</a> as well <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I decided to take a little extra time and add a debug information screen as well as a small welcome screen <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>Controls (PC):</strong></p>
<p>Up/Down for Player 1 Paddle (Left): Q/A</p>
<p>Up/Down for Player 2 Paddle (Right): Up Arrow/Down Arrow</p>
<p>Debug Information: F1</p>
<p><strong>Controls (XBox 360):</strong></p>
<p>Up/Down for Player 1 Paddle (Left): Left Thumbstick on Player 1 Remote</p>
<p>Up/Down for Player 2 Paddle (Right): Left Thumbstick on Player 2 Remote</p>
<p>I did run into a couple of &#8216;gotchas!&#8217; while working with Game Studio. The major one I had trouble with was when you&#8217;re developing for the XBox 360 you have to account for <a href="http://en.wikipedia.org/wiki/Overscan" title="Link -- Wikipedia Article on Overscan" target="_blank">overscan</a> on the Television and have your game render within the &#8216;<a href="http://en.wikipedia.org/wiki/Safe_area" title="Link -- Wikipedia Article on Safe Area" target="_blank">Safe Area</a>&#8216;. I noticed that when I was playing my Pong! game on my LCD TV at 720p, the edges of the game were cut off and it looked like it was stretched past the borders of my TV. After asking the fine folks in #XNA on IRC about this issue, they were able to help me out. Now I have my Pong! game account for this by setting up an XBox 360 macro which pads the edges of the play area by 50 pixels.</p>
<p>I&#8217;m including the Source Code for my version of Pong! which I&#8217;ve titled, &#8220;2D WndrPong!&#8221;. You can work in XNA Game Studio for free using <a href="http://msdn2.microsoft.com/en-us/express/aa975050.aspx" title="Link -- Microsoft.com Visual C# 2005 Express Edition" target="_blank">Visual C# 2005 Express Edition</a> along with <a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=DF80D533-BA87-40B4-ABE2-1EF12EA506B7&amp;displaylang=en" title="Link -- Microsoft.com XNA Game Studio 2.0 Install" target="_blank">XNA Game Studio 2.0</a> <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  To deploy your games to an XBox 360, you must pair your XBox with your PC  (using the &#8220;XNA Game Studio Device Center&#8221; tool)  then you must purchase an XNA Creators Club Membership from the XBox 360 Marketplace. I believe the prices are $49.99 for 3 Months, $99.99 for a year. I opted for the entire year since I know it&#8217;s going to take me some time to learn <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>Baby Steps <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><strong>2D WndrPong! Source Code</strong> &#8211; <a href="http://www.enusbaum.com/blog//blog/wp-content/uploads/2007/12/2d_wndrpong.zip" title="Link -- Download Link to 2D WndrPong!" target="_blank">Download</a> (36k)</p>
<div class="su-linkbox" id="post-69-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2007/12/2d-wndrpong-using-the-microsoft-xna-game-studio-v20/&quot;&gt;2D WndrPong! using the Microsoft XNA Game Studio v2.0!&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/12/2d-wndrpong-using-the-microsoft-xna-game-studio-v20/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WWWinamp is being converted to .NET Framework v3.5!</title>
		<link>http://www.enusbaum.com/blog/2007/12/wwwinamp-is-being-converted-to-net-framework-v35/</link>
		<comments>http://www.enusbaum.com/blog/2007/12/wwwinamp-is-being-converted-to-net-framework-v35/#comments</comments>
		<pubDate>Thu, 06 Dec 2007 05:17:32 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[WWWinamp]]></category>
		<category><![CDATA[.NET 2008]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Team Foundation Server 2008]]></category>
		<category><![CDATA[Visual Studio 2008]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/2007/12/05/wwwinamp-is-being-converted-to-net-framework-v35/</guid>
		<description><![CDATA[Yep, it&#8217;s true! I&#8217;ve spent the last week or so playing around with the .NET Framework v3.5 and I really like what I see. Microsoft has also put a lot of work into Team Foundation Server 2008 as well. I&#8217;ve setup a Virtual Machine using Virtual PC here running Team Foundation Server 2008 on Windows [...]]]></description>
			<content:encoded><![CDATA[<p>Yep, it&#8217;s true!</p>
<p>I&#8217;ve spent the last week or so playing around with the .NET Framework v3.5 and I really like what I see. Microsoft has also put a lot of work into Team Foundation Server 2008 as well. I&#8217;ve setup a Virtual Machine using Virtual PC here running Team Foundation Server 2008 on Windows 2003 Server R2 and was able to get WWWinamp imported + converted without issue!</p>
<p>[zoomer]66|450|0|WWWinamp in Team Foundation Server 2008|1|0[/zoomer]</p>
<p>One thing I have noticed is that TFS does take up quite a bit of resources. I had to kick my VM up to 2GB of RAM in order for it to handle and build WWWinamp without hitting the swap file. Something to keep in mind moving forward I suppose. I&#8217;d imagine a large installation of TFS would require something along the lines of a Quad Core CPU with at least 4GB RAM.</p>
<p>There are several features in .NET 3.5 that I plan on implementing in WWWinamp right off the bat:</p>
<ul>
<li><a href="http://msdn2.microsoft.com/en-us/library/bb384062(VS.90).aspx" title="MSDN -- Article on C# Object Initializers in .NET 3.5" target="_blank">Object Initializers</a> &#8211; This will really help clean up the already massive code base. It&#8217;ll also speed things up a bit <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li><a href="http://msdn2.microsoft.com/en-us/library/bb545961(VS.90).aspx" title="MSDN Article -- Embedded Manifests in C# .NET 3.5" target="_blank">Embedded Manifest</a> &#8211; A feature I&#8217;m surprised they didn&#8217;t implement with the release of .NET 3.0. Now the .manifest file will be embedded into WWWinamp, so you won&#8217;t have the extra file handing around if you run Windows Vista <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li><a href="http://msdn2.microsoft.com/en-us/library/bb383977(VS.90).aspx" title="MSDN Article -- Extension Methods in C# .NET 3.5" target="_blank">Extension Methods</a> &#8211; Again, this will help clean up the code base and make things faster. I imagine it&#8217;ll take up a little more memory, but these days RAM is abundant <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
</ul>
<p>Now, I see LinQ and I&#8217;m trying to figure out it&#8217;s role in everything. I think it&#8217;ll make DB communication easier but I&#8217;m not a fan of the anonymous data types and implicit local variables. I think it could lead to some sloppy coding methodology if not kept in check.</p>
<p>I think it&#8217;s handy that you can now run a SQL like statement on a generic, but I&#8217;m wondering what sets this apart from the <a href="http://msdn2.microsoft.com/en-us/library/fh1w7y8z.aspx" title="MSDN Article -- FindAll method in C# .NET 2.0" target="_blank">FindAll</a> method that already exists in <a href="http://msdn2.microsoft.com/en-us/library/system.collections.generic.aspx" title="MSDN Article -- System.Collections.Generics in C# .NET 2.0" target="_blank">System.Collection.Generics</a>. It does make it easier though to search through a Generic Collection for a developer who doesn&#8217;t have a solid grasp on delegates.</p>
<p>So, all that said, things are moving along. I&#8217;ll keep you all updated on the progress and please, send in those feature requests! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="su-linkbox" id="post-67-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2007/12/wwwinamp-is-being-converted-to-net-framework-v35/&quot;&gt;WWWinamp is being converted to .NET Framework v3.5!&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/12/wwwinamp-is-being-converted-to-net-framework-v35/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WWWinamp Feedback Requested</title>
		<link>http://www.enusbaum.com/blog/2007/12/wwwinamp-feedback-requested/</link>
		<comments>http://www.enusbaum.com/blog/2007/12/wwwinamp-feedback-requested/#comments</comments>
		<pubDate>Mon, 03 Dec 2007 05:04:26 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[WWWinamp]]></category>
		<category><![CDATA[.NET 3.5]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Feedback]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/2007/12/02/wwwinamp-feedback-requested/</guid>
		<description><![CDATA[I started tapping away at the WWWinamp source the other night and was curious if there are any features you guys, the users, would like to see added? I&#8217;ve been trying to think up new ideas but I&#8217;m not sure if they&#8217;d even be helpful. So basically, I&#8217;m asking help for what to put in [...]]]></description>
			<content:encoded><![CDATA[<p>I started tapping away at the WWWinamp source the other night and was curious if there are any features you guys, the users, would like to see added? I&#8217;ve been trying to think up new ideas but I&#8217;m not sure if they&#8217;d even be helpful.</p>
<p>So basically, I&#8217;m asking help for what to put in the next version. If you could leave a comment with any of the following, that&#8217;d REALLY help out! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<ul>
<li>What feature would you like to see added to WWWinamp?</li>
<li>Is there any nagging bug or weird behavior you would like fixed?</li>
</ul>
<p>I&#8217;m currently fiddling around with Visual Studio 2008 right now and am looking into the benefits C# 3.5 offers over 2.0, which the current release of WWWinamp is based upon.</p>
<p>Thanks for your feedback and support everyone! I look forward to your responses! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<div class="su-linkbox" id="post-64-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2007/12/wwwinamp-feedback-requested/&quot;&gt;WWWinamp Feedback Requested&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/12/wwwinamp-feedback-requested/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Discogs.com API Assembly for .NET Applications v1.0 Build 2871</title>
		<link>http://www.enusbaum.com/blog/2007/11/discogscom-api-assembly-for-net-applications-v10-build-2871/</link>
		<comments>http://www.enusbaum.com/blog/2007/11/discogscom-api-assembly-for-net-applications-v10-build-2871/#comments</comments>
		<pubDate>Tue, 13 Nov 2007 01:20:48 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[Discogs API]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[General Software]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# Assembly]]></category>
		<category><![CDATA[C# Example]]></category>
		<category><![CDATA[Cover Art]]></category>
		<category><![CDATA[Discogs]]></category>
		<category><![CDATA[Discogs.com]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/2007/11/12/discogscom-api-assembly-for-net-applications-v10-build-2871/</guid>
		<description><![CDATA[Greetings Everyone! I&#8217;ve been working on the Discogs.com API Assembly for .NET Applications now for a couple days and have been able to make some progress. It&#8217;s now a bit more stable as well a a tiny bit easier to use. I took some time and added a DEBUG class. This class allows you to [...]]]></description>
			<content:encoded><![CDATA[<p>Greetings Everyone!</p>
<p>I&#8217;ve been working on the Discogs.com API Assembly for  .NET Applications now for a couple days and have been able to make some progress. It&#8217;s now a bit more stable as well a a tiny bit easier to use.</p>
<p>I took some time and added a DEBUG class. This class allows you to find out what&#8217;s happening within the Discogs.com Assembly if you start to have issues! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  This new class has two properties:</p>
<ul>
<li><strong>Verbose (bool)</strong> &#8211; If set to TRUE, Verbose logging will be enabled allowing you to get more precise detail on what is going on within the Assembly. Otherwise, only exceptions will be logged.</li>
<li><strong>Log (string)</strong> &#8211; This is a string containing the current debug log.</li>
</ul>
<p>Also, it has one Method:</p>
<ul>
<li><strong>LogEvent (string sEvent)</strong> &#8211; Logs the value passed in to the debug log. This way you can use the same debug log from your own applications <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Should help make things a little easier.</li>
</ul>
<p>New in this version as well is better error handling in the event of 404&#8242;s or an Artist/Release isn&#8217;t found. Before if you requested something that didn&#8217;t exist, the Assembly kinda crapped out while trying to deserialize the (non-existent) XML <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  This has all been fixed.</p>
<p>I&#8217;m also including a small example program (with source code) on how to use the Discogs.com API Assembly. I&#8217;ve coded the example in C#, so sorry to all those VB.NET developers out there! <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  If one of you guys would like to translate it to VB.NET, I&#8217;d be more than happy to post it here as well.</p>
<p>If you do not already have Microsoft Visual Studio installed, no worries! Microsoft provides a free version for C# development called Visual C# Express and you can get it <a href="http://msdn2.microsoft.com/en-us/express/aa700756.aspx" title="Download Microsoft Visual C# Express" target="_blank">here</a> over at Microsoft.com. <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>You will need to update the Reference to the Discogs.com API Assembly. It currently points to where I had it setup on my local machine. <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
Any and all feedback is appreciated!</p>
<p>Cheers!</p>
<p><strong>Discogs.com API Assembly for .NET Applications v1.0 Build 2871</strong> &#8211; <a href="http://www.enusbaum.com/Discogs/DiscogsAPI_10b2871.zip" title="Download Discogs.com API Assembly for .NET Applications!" target="_blank">Download</a> (9k)</p>
<p><strong>Discogs.com API Assembly Example Application (with Source)</strong> &#8211; <a href="http://www.enusbaum.com/Discogs/DiscogsAPI_Example.zip" title="Download Discogs.com API Assembly Example Application" target="_blank">Download</a> (10k)</p>
<div class="su-linkbox" id="post-62-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2007/11/discogscom-api-assembly-for-net-applications-v10-build-2871/&quot;&gt;Discogs.com API Assembly for .NET Applications v1.0 Build 2871&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/11/discogscom-api-assembly-for-net-applications-v10-build-2871/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Creating a Custom Listener for your WCF application in C#</title>
		<link>http://www.enusbaum.com/blog/2007/05/creating-a-custom-listener-for-your-wcf-application-in-c/</link>
		<comments>http://www.enusbaum.com/blog/2007/05/creating-a-custom-listener-for-your-wcf-application-in-c/#comments</comments>
		<pubDate>Sun, 20 May 2007 00:37:54 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[Microsoft .NET 3.0 / WinFX]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Listener]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[SOAP]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[WCF Debugging]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/2007/05/19/creating-a-custom-listener-for-your-wcf-application-in-c/</guid>
		<description><![CDATA[Microsoft already provides a couple great listeners that are great for debugging. The two most commonly used are XmlWriterTraceListener and TextWriterTraceListener, which both dump the diagnostic messages to the file you specify in the configuration options. Microsoft has a great article on how to use these trace listeners for message logging within a WCF application [...]]]></description>
			<content:encoded><![CDATA[<p>Microsoft already provides a couple great listeners that are great for debugging.  The two most commonly used are XmlWriterTraceListener and TextWriterTraceListener, which both dump the diagnostic messages to the file you specify in the configuration options. Microsoft has a great article on how to use these trace listeners for message logging within a WCF application <a title="Microsoft Article on using Trace Listeners in WCF Applications" href="http://msdn2.microsoft.com/en-us/library/ms730064.aspx" target="_blank">here</a>.</p>
<p>The issue that I ran into was  I wanted to log these messages not to the file system, but to the database. Microsoft provides for this in allowing people to create custom Trace Listeners. After some heavy Googling I was able to find <a title="MSDN Article on Creating a Custom Trace Listener" href="http://msdn.microsoft.com/msdnmag/issues/06/04/CLRInsideOut/default.aspx" target="_blank">this</a> article on MSDN which describes a method in which you would be able to create a custom Trace Listener. Using the code from this article, I was able to boil it down to a simple code shell which anyone could take and use within their WCF application.</p>
<p>This is very helpful for people looking to capture and log the incoming and outgoing SOAP messages to their WCF application without having to create a custom dispatcher. On top of that, creating a Listener provides a drag-and-drop assembly you can use on any future project you might create.</p>
<p>The code for this project can be found <a title="Custom Trace Listener for a WCF Application" href="http://www.enusbaum.com/blog/wp-content/uploads/2007/05/customlistener.zip" target="_blank">here</a> (ZIP, 5.4k). The solution was created using Visual Studio 2005.</p>
<p>Cheers!</p>
<div class="su-linkbox" id="post-19-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2007/05/creating-a-custom-listener-for-your-wcf-application-in-c/&quot;&gt;Creating a Custom Listener for your WCF application in C#&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/05/creating-a-custom-listener-for-your-wcf-application-in-c/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Reading ID3 Tags using C#</title>
		<link>http://www.enusbaum.com/blog/2007/02/reading-id3-tags-using-c/</link>
		<comments>http://www.enusbaum.com/blog/2007/02/reading-id3-tags-using-c/#comments</comments>
		<pubDate>Wed, 21 Feb 2007 21:55:09 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ID3]]></category>
		<category><![CDATA[MP3]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/2007/02/21/reading-id3-tags-using-c/</guid>
		<description><![CDATA[I&#8217;m currently working on an application which will read through a directory full of MP3&#8242;s downloaded from USENET and then sort them into sub-folders titled after the &#8220;Artists &#8211; Album&#8221; stored within the ID3 tag. I currently only have code that can read ID3v1 tags but am working on updated code which will allow for [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working on an application which will read through a directory full of MP3&#8242;s downloaded from USENET and then sort them into sub-folders titled after the &#8220;Artists &#8211; Album&#8221; stored within the ID3 tag.</p>
<p>I currently only have code that can read ID3v1 tags but am working on updated code which will allow for ID3v2+ tags to be read as well.</p>
<p>I use a STRUCT to hold the ID3 data and then use a quick little routine to parse the raw byte[] array containing the ID3 data read from the file into my STRUCT.</p>
<p>The STRUCT:</p>
<p>[csharp]</p>
<p>private struct ID3Tag<br />
{<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]<br />
public byte[] bTagHeader; //0-2<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]<br />
public byte[] bTrackName; //3-32<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]<br />
public byte[] bArtistsName; //33-62<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]<br />
public byte[] bAlbumName; //63-92<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]<br />
public byte[] bYear; //93-96<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]<br />
public byte[] bComment; //97-126<br />
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]<br />
public byte[] bGenres; //127<br />
}</p>
<p>[/csharp]</p>
<p>Reading the last 128 bytes of the MP3 file into a byte[] array:</p>
<p>[csharp]</p>
<p>private byte[] ReadID3FromFileToByte(string sLocalFile)<br />
{<br />
using (FileStream oFS = new FileStream(sLocalFile, FileMode.Open, FileAccess.Read))<br />
{<br />
using (BinaryReader oBR = new BinaryReader(oFS))<br />
{<br />
oBR.BaseStream.Position = (oFS.Length &#8211; 128);<br />
return oBR.ReadBytes((int)oFS.Length);<br />
}<br />
}<br />
}</p>
<p>[/csharp]</p>
<p>The routine used to convert by the byte[] array into the STRUCT:</p>
<p>[csharp]</p>
<p>private ID3Tag ID3BytesToID3Struct(byte[] bRawID3)<br />
{<br />
GCHandle hRawID3 = GCHandle.Alloc(bRawID3, GCHandleType.Pinned);<br />
ID3Tag id3NewTag = (ID3Tag)Marshal.PtrToStructure((hRawID3.AddrOfPinnedObject()), typeof(ID3Tag));<br />
hRawID3.Free();<br />
return id3NewTag;<br />
}</p>
<p>[/csharp]</p>
<p>And the code which brings it all together:</p>
<p>[csharp]</p>
<p>byte[] bFileData = ReadID3FromFileToByte(sMyInputFile);<br />
ID3Tag myID3 = ID3BytesToID3Struct(bFileData);</p>
<p>[/csharp]</p>
<p>I hope this is able to help someone else out in their efforts to read an ID3 tag using C#.</p>
<p>Cheers!</p>
<div class="su-linkbox" id="post-15-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2007/02/reading-id3-tags-using-c/&quot;&gt;Reading ID3 Tags using C#&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/02/reading-id3-tags-using-c/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Why does the Microsoft .NET implementation of GZip compression suck?</title>
		<link>http://www.enusbaum.com/blog/2007/01/why-does-the-microsoft-net-implementation-of-gzip-compression-suck/</link>
		<comments>http://www.enusbaum.com/blog/2007/01/why-does-the-microsoft-net-implementation-of-gzip-compression-suck/#comments</comments>
		<pubDate>Mon, 22 Jan 2007 23:11:29 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[General Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[7Zip]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[GZip]]></category>
		<category><![CDATA[GZipStream]]></category>
		<category><![CDATA[WinZip]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/2007/01/22/why-does-the-microsoft-net-implementation-of-gzip-compression-suck/</guid>
		<description><![CDATA[I&#8217;ve been working on a self contained patch generator using C#. For the patch data payload, I wanted to use GZip (or deflate) to compress the payload, this way if you generated a patch for a 1MB file, the patch file wouldn&#8217;t be +1MB (5 bytes per change). I did some quick tests to compare [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a self contained patch generator using C#. For the patch data payload, I wanted to use GZip (or deflate) to compress the payload, this way if you generated a patch for a 1MB file, the patch file wouldn&#8217;t be +1MB (5 bytes per change).</p>
<p>I did some quick tests to compare compression ratios on a simulated data payload:</p>
<p><strong>Original Uncompressed Payload:</strong> 288k<strong><br />
GZipped using System.IO.Compression:</strong> 171k<br />
<strong>Zipped using WinZip with &#8220;Super Fast&#8221; Compression:</strong> 105k<br />
<strong>7-Zipped using 7-Zip with &#8220;Maximum&#8221; Compression:</strong> 61k</p>
<p>I had to do some research and dig into the MSDN  libraries before the answer was finally revealed from the horses mouth:</p>
<p><em>&#8220;The compression functionality in DeflateStream and GZipStream is exposed as a stream. <strong>Data is read in on a byte-by-byte basis, so it is not possible to perform multiple passes to determine the best method for compressing entire files or large blocks of data.</strong> The DeflateStream and GZipStream classes are best used on uncompressed sources of data. If the source data is already compressed, using these classes may actually increase the size of the stream.&#8221;</em></p>
<p>It&#8217;s a bit disappointing that Microsoft itself would not recommend it&#8217;s own built in compression routine, and even go so far to say that it may &#8220;actually increase the size of the stream&#8221;. I hope in the upcoming version of .NET 3.0, they address some shortcomings of this area of the framework. Perhaps an exposed method where you can pass a byte[] which can be compressed using multiple passes and Adaptive Huffman Encoding.</p>
<p>I know there are several alternatives out there, such as assemblies which support the ZIP compression format, but wasn&#8217;t the goal of .NET to move away from DLL hell? <img src='http://www.enusbaum.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>In the meantime, I&#8217;ll probably be sticking with the Microsoft implementation of GZip until I take some time to create my own Huffman encoding routine. I think a good place to start would be <a href="http://www.amazon.com/Data-Compression-Reference-David-Salomon/dp/0387406972/sr=8-2/qid=1169485815/ref=pd_bbs_2/104-6117961-7571952?ie=UTF8&amp;s=books">this</a> book titled <strong class="sans">Data Compression: The Complete Reference</strong><span class="sans">. I remember thumbing through it once at a Borders book store and thinking it was a really great reference. At the time though, I had no use for such a resource.</span></p>
<p><strong>Quick note to my readers:</strong> I understand that there are plenty of online resources for Adaptive Huffman Encoding and other compression methods using C# that have already been created. Personally, I learn by doing. So when I do my own research and create something from the ground up it really helps me understand how it all works.</p>
<div class="su-linkbox" id="post-11-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2007/01/why-does-the-microsoft-net-implementation-of-gzip-compression-suck/&quot;&gt;Why does the Microsoft .NET implementation of GZip compression suck?&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/01/why-does-the-microsoft-net-implementation-of-gzip-compression-suck/feed/</wfw:commentRss>
		<slash:comments>-43</slash:comments>
		</item>
		<item>
		<title>Embedded Resources in C#</title>
		<link>http://www.enusbaum.com/blog/2007/01/embedded-resources-in-c/</link>
		<comments>http://www.enusbaum.com/blog/2007/01/embedded-resources-in-c/#comments</comments>
		<pubDate>Fri, 19 Jan 2007 18:50:39 +0000</pubDate>
		<dc:creator>eric</dc:creator>
				<category><![CDATA[C# Programming]]></category>
		<category><![CDATA[General Programming]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C# Assembly]]></category>
		<category><![CDATA[Embedded Resources]]></category>
		<category><![CDATA[Example]]></category>

		<guid isPermaLink="false">http://www.enusbaum.com/blog/?p=3</guid>
		<description><![CDATA[So last night I was trying to think of the best way to compile images into the WWWinamp EXE. One way I had considered was converting the images into a byte[] and then just putting this into the source code. The images I was working with were only about 200 bytes long, so this could [...]]]></description>
			<content:encoded><![CDATA[<p>So last night I was trying to think of the best way to compile images into the WWWinamp EXE.</p>
<p>One way I had considered was converting the images into a byte[] and then just putting this into the source code. The images I was working with were only about 200 bytes long, so this could be do-able.</p>
<p>Upon further investigation I came across <a href="http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75">this</a> tutorial over at DevHood.com. It seemed a better idea to just roll with Embedded Resources in the EXE, since it would give me a few more options in the long run.</p>
<p>Below is the code I used to retrieve an embedded resource. You pass in the file name of the resource you want, and it&#8217;ll return a byte[] with the content:</p>
<p>[csharp]</p>
<p>public byte[] Server_ReadEmbeddedResource(string sFileName)<br />
{<br />
Assembly asResources = Assembly.GetExecutingAssembly(); //Referrence to the current assembly<br />
string[] sResNames = asResources.GetManifestResourceNames(); //Store list of resources for the current assembly in an array</p>
<p>foreach (string sResourceName in sResNames)<br />
{<br />
if(sResourceName.EndsWith(sFileName))<br />
{<br />
BinaryReader oBR = new BinaryReader(asResources.GetManifestResourceStream(sResourceName));<br />
return oBR.ReadBytes((int)asResources.GetManifestResourceStream(sResourceName).Length);<br />
}<br />
}<br />
return new byte[] { 0 };<br />
}</p>
<p>[/csharp]</p>
<div class="su-linkbox" id="post-3-linkbox"><div class="su-linkbox-label">Link to this post!</div><div class="su-linkbox-field"><input type="text" value="&lt;a href=&quot;http://www.enusbaum.com/blog/2007/01/embedded-resources-in-c/&quot;&gt;Embedded Resources in C#&lt;/a&gt;" onclick="javascript:this.select()" readonly="readonly" style="width: 100%;" /></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/01/embedded-resources-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

