<?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>out &#62;&#62; m_Conscientia; &#187; stream</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/stream/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Mon, 02 Aug 2010 22:30:54 +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 Streams</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/07/net-streams/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/07/net-streams/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 21:40:56 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[Application Development Foundation]]></category>
		<category><![CDATA[stream]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=211</guid>
		<description><![CDATA[The Stream base class is an abstraction of a sequence of bytes. A stream may be sequential or random access. .NET defines several derived stream classes, not all of which reside in System.IO. Stream implements IDisposable. Stream overview Stream presents properties to determine whether a stream CanRead, CanWrite, CanSeek or CanTimeout. Streams that support seeking [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://msdn.microsoft.com/en-us/library/system.io.stream.aspx">Stream</a> base class is an abstraction of a sequence of bytes. A stream may be sequential or random access. .NET defines several derived stream classes, not all of which reside in System.IO. Stream implements <a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx">IDisposable</a>.</p>
<p><strong>Stream overview</strong></p>
<p>Stream presents properties to determine whether a stream CanRead, CanWrite, CanSeek or CanTimeout. Streams that support seeking implement the Position and Length properties.</p>
<p>Stream presents virtual methods to sequentially read and write (Read, ReadByte, Write, WriteByte), as well as Flush, Seek, SetLength and Close.</p>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx">FileStream</a></strong></p>
<p>This example illustrates how to write and read to a file, using the static <a href="http://msdn.microsoft.com/en-us/library/system.io.file.aspx">File</a> class to create the required FileStreams.</p>

<div class="wp_codebox"><table><tr id="p2116"><td class="code" id="p211code6"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> path <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;C:\file.txt&quot;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>FileStream fs <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>path<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> info <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UTF8Encoding<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetBytes</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;some text to write&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    fs<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>info, <span style="color: #FF0000;">0</span>, info<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>FileStream fs <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span><span style="color: #008000;">&#40;</span>path, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span>, FileAccess<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> 
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> b <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1024</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
    UTF8Encoding encoding <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UTF8Encoding<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>fs<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#40;</span>b, <span style="color: #FF0000;">0</span>, b<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> 
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>encoding<span style="color: #008000;">.</span><span style="color: #0000FF;">GetString</span><span style="color: #008000;">&#40;</span>b<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><strong><a href="http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx">MemoryStream</a></strong></p>
<p>You could repeat the above example, but substitute the FileStream for a MemoryStream.</p>

<div class="wp_codebox"><table><tr id="p2117"><td class="code" id="p211code7"><pre class="csharp" style="font-family:monospace;">MemoryStream ms <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MemoryStream<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>MemoryStream also provides the facility to write to another stream, like so:</p>

<div class="wp_codebox"><table><tr id="p2118"><td class="code" id="p211code8"><pre class="csharp" style="font-family:monospace;">MemoryStream ms <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MemoryStream<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// write some data to ms</span>
FileStream fs <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;C:\mem.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
ms<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteTo</span><span style="color: #008000;">&#40;</span>fs<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
fs<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
ms<span style="color: #008000;">.</span><span style="color: #0000FF;">Close</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p><strong><a href="http://msdn.microsoft.com/en-us/library/system.io.bufferedstream.aspx">BufferedStream</a></strong></p>
<p>In order to improve performance, BufferedStream adds a buffering layer to read and write operations on another stream. For example:</p>

<div class="wp_codebox"><table><tr id="p2119"><td class="code" id="p211code9"><pre class="csharp" style="font-family:monospace;">FileStream fs <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;C:\buffered.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
BufferedStream bs <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> BufferedStream<span style="color: #008000;">&#40;</span>fs<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// write data to bs like any other stream</span></pre></td></tr></table></div>

<p><strong>Compressed streams</strong></p>
<p>The <a href="http://msdn.microsoft.com/en-us/library/system.io.compression.aspx">System.IO.Compression</a> namespace provides the <a href="http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx">GZipStream</a> and <a href="http://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx">DeflateStream</a> classes. Both use the same compression algorithm (which does not depend on any patents). GZipStream writes data with headers that allow the data to be decompressed with GZip, and therefore has a slightly larger footprint.</p>
<p>Compressed streams may be used in a similar fashion to buffering:</p>

<div class="wp_codebox"><table><tr id="p21110"><td class="code" id="p211code10"><pre class="csharp" style="font-family:monospace;">FileStream destination <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FileStream<span style="color: #008000;">&#40;</span><span style="color: #666666;">@&quot;C:\compressed.txt&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// when writing to gzipStream, we compress data then write it to the destination stream</span>
GZipStream gzipStream <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> GZipStream<span style="color: #008000;">&#40;</span>destination, CompressionMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Compress</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> buffer <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">256</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// put some data in the buffer</span>
gzipStream<span style="color: #008000;">.</span><span style="color: #0000FF;">Write</span><span style="color: #008000;">&#40;</span>buffer<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Decompression is essentially the same, except you perform reads instead of writes.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/07/net-streams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
