<?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; .NET</title>
	<atom:link href="http://www.enusbaum.com/blog/tag/net/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>Thu, 02 Sep 2010 01:43:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>.NET StringBuilder &#8212; Fast, but not as fast as you think!</title>
		<link>http://www.enusbaum.com/blog/2009/05/28/net-stringbuilder-fast-but-not-as-fast-as-you-think/</link>
		<comments>http://www.enusbaum.com/blog/2009/05/28/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" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.text.stringbuilder.aspx','MSDN+Documentation+--+StringBuilder+Class')" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.text.stringbuilder.aspx','MSDN+Documentation+--+StringBuilder+Class')" 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" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.text.stringbuilder.aspx','MSDN+Documentation+--+StringBuilder+Class')" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.text.stringbuilder.aspx','MSDN+Documentation+--+StringBuilder+Class')" 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" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.string.concat.aspx','MSDN+Documentation+--+String.Concat+Method')" 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" onclick="return TrackClick('http%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fsystem.string.format.aspx','MSDN+Documentation+--+String.Format+Method')" 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/" onclick="return TrackClick('http%3A%2F%2Fwww.jetbrains.com%2Fprofiler%2F','Homepage+--+JetBrains+dotTrace+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>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2009%2F05%2F28%2Fnet-stringbuilder-fast-but-not-as-fast-as-you-think%2F&amp;title=.NET+StringBuilder+%26%238212%3B+Fast%2C+but+not+as+fast+as+you+think%21" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2009%2F05%2F28%2Fnet-stringbuilder-fast-but-not-as-fast-as-you-think%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2009%2F05%2F28%2Fnet-stringbuilder-fast-but-not-as-fast-as-you-think%2F&amp;title=.NET+StringBuilder+%26%238212%3B+Fast%2C+but+not+as+fast+as+you+think%21" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+.NET+StringBuilder+%26%238212%3B+Fast%2C+but+not+as+fast+as+you+think%21+@+http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2009%2F05%2F28%2Fnet-stringbuilder-fast-but-not-as-fast-as-you-think%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2009/05/28/net-stringbuilder-fast-but-not-as-fast-as-you-think/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Reading ID3 Tags using C#</title>
		<link>http://www.enusbaum.com/blog/2007/02/21/reading-id3-tags-using-c/</link>
		<comments>http://www.enusbaum.com/blog/2007/02/21/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'm currently working on an application which will read through a directory full of MP3's downloaded from USENET and then sort them into sub-folders titled after the "Artists - Album" 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'm currently working on an application which will read through a directory full of MP3's downloaded from USENET and then sort them into sub-folders titled after the "Artists - Album" 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>
<div class="igBar"><span id="lcsharp-5"><a href="#" onclick="javascript:showPlainTxt('csharp-5'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-5">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">struct</span> ID3Tag</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #0000FF;">ByValArray</span>, SizeConst = <span style="color: #FF0000;color:#800000;">3</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bTagHeader; <span style="color: #008080; font-style: italic;">//0-2</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #0000FF;">ByValArray</span>, SizeConst = <span style="color: #FF0000;color:#800000;">30</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bTrackName; <span style="color: #008080; font-style: italic;">//3-32</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #0000FF;">ByValArray</span>, SizeConst = <span style="color: #FF0000;color:#800000;">30</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bArtistsName; <span style="color: #008080; font-style: italic;">//33-62</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #0000FF;">ByValArray</span>, SizeConst = <span style="color: #FF0000;color:#800000;">30</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bAlbumName; <span style="color: #008080; font-style: italic;">//63-92</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #0000FF;">ByValArray</span>, SizeConst = <span style="color: #FF0000;color:#800000;">4</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bYear; <span style="color: #008080; font-style: italic;">//93-96</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #0000FF;">ByValArray</span>, SizeConst = <span style="color: #FF0000;color:#800000;">30</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bComment; <span style="color: #008080; font-style: italic;">//97-126</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #0000FF;">ByValArray</span>, SizeConst = <span style="color: #FF0000;color:#800000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bGenres; <span style="color: #008080; font-style: italic;">//127</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Reading the last 128 bytes of the MP3 file into a byte[] array:</p>
<div class="igBar"><span id="lcsharp-6"><a href="#" onclick="javascript:showPlainTxt('csharp-6'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-6">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ReadID3FromFileToByte<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> sLocalFile<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>FileStream oFS = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FileStream<span style="color: #000000;">&#40;</span>sLocalFile, FileMode.<span style="color: #0000FF;">Open</span>, FileAccess.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>BinaryReader oBR = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> BinaryReader<span style="color: #000000;">&#40;</span>oFS<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">oBR.<span style="color: #0000FF;">BaseStream</span>.<span style="color: #0000FF;">Position</span> = <span style="color: #000000;">&#40;</span>oFS.<span style="color: #0000FF;">Length</span> - <span style="color: #FF0000;color:#800000;">128</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">return</span> oBR.<span style="color: #0000FF;">ReadBytes</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>oFS.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>The routine used to convert by the byte[] array into the STRUCT:</p>
<div class="igBar"><span id="lcsharp-7"><a href="#" onclick="javascript:showPlainTxt('csharp-7'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-7">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">private</span> ID3Tag ID3BytesToID3Struct<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bRawID3<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">GCHandle hRawID3 = GCHandle.<span style="color: #0000FF;">Alloc</span><span style="color: #000000;">&#40;</span>bRawID3, GCHandleType.<span style="color: #0000FF;">Pinned</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ID3Tag id3NewTag = <span style="color: #000000;">&#40;</span>ID3Tag<span style="color: #000000;">&#41;</span>Marshal.<span style="color: #0000FF;">PtrToStructure</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>hRawID3.<span style="color: #0000FF;">AddrOfPinnedObject</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>ID3Tag<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">hRawID3.<span style="color: #0000FF;">Free</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">return</span> id3NewTag;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span> </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>And the code which brings it all together:</p>
<div class="igBar"><span id="lcsharp-8"><a href="#" onclick="javascript:showPlainTxt('csharp-8'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-8">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> bFileData = ReadID3FromFileToByte<span style="color: #000000;">&#40;</span>sMyInputFile<span style="color: #000000;">&#41;</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">ID3Tag myID3 = ID3BytesToID3Struct<span style="color: #000000;">&#40;</span>bFileData<span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></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>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2007%2F02%2F21%2Freading-id3-tags-using-c%2F&amp;title=Reading+ID3+Tags+using+C%23" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2007%2F02%2F21%2Freading-id3-tags-using-c%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2007%2F02%2F21%2Freading-id3-tags-using-c%2F&amp;title=Reading+ID3+Tags+using+C%23" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Reading+ID3+Tags+using+C%23+@+http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2007%2F02%2F21%2Freading-id3-tags-using-c%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/02/21/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/22/why-does-the-microsoft-net-implementation-of-gzip-compression-suck/</link>
		<comments>http://www.enusbaum.com/blog/2007/01/22/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'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't be +1MB (5 bytes per change). I did some quick tests to compare [...]]]></description>
			<content:encoded><![CDATA[<p>I'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'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 "Super Fast" Compression:</strong> 105k<br />
<strong>7-Zipped using 7-Zip with "Maximum" 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>"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."</em></p>
<p>It's a bit disappointing that Microsoft itself would not recommend it's own built in compression routine, and even go so far to say that it may "actually increase the size of the stream". 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'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'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" onclick="return TrackClick('http%3A%2F%2Fwww.amazon.com%2FData-Compression-Reference-David-Salomon%2Fdp%2F0387406972%2Fsr%3D8-2%2Fqid%3D1169485815%2Fref%3Dpd_bbs_2%2F104-6117961-7571952%3Fie%3DUTF8%26amp%3Bs%3Dbooks','this')">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>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2007%2F01%2F22%2Fwhy-does-the-microsoft-net-implementation-of-gzip-compression-suck%2F&amp;title=Why+does+the+Microsoft+.NET+implementation+of+GZip+compression+suck%3F" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2007%2F01%2F22%2Fwhy-does-the-microsoft-net-implementation-of-gzip-compression-suck%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2007%2F01%2F22%2Fwhy-does-the-microsoft-net-implementation-of-gzip-compression-suck%2F&amp;title=Why+does+the+Microsoft+.NET+implementation+of+GZip+compression+suck%3F" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Why+does+the+Microsoft+.NET+implementation+of+GZip+compression+suck%3F+@+http%3A%2F%2Fwww.enusbaum.com%2Fblog%2F2007%2F01%2F22%2Fwhy-does-the-microsoft-net-implementation-of-gzip-compression-suck%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.enusbaum.com/blog/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.enusbaum.com/blog/2007/01/22/why-does-the-microsoft-net-implementation-of-gzip-compression-suck/feed/</wfw:commentRss>
		<slash:comments>-43</slash:comments>
		</item>
	</channel>
</rss>
