<?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>Fremus.co.za &#187; Streams</title>
	<atom:link href="http://www.fremus.co.za/blog/tag/streams/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fremus.co.za/blog</link>
	<description>Demistifying Life and Web Development</description>
	<lastBuildDate>Sun, 27 Nov 2011 15:56:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>C# and streams</title>
		<link>http://www.fremus.co.za/blog/2009/10/513/</link>
		<comments>http://www.fremus.co.za/blog/2009/10/513/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 15:10:38 +0000</pubDate>
		<dc:creator>fr3dr1k</dc:creator>
				<category><![CDATA[Application Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MemoryStream]]></category>
		<category><![CDATA[Streams]]></category>

		<guid isPermaLink="false">http://www.fremus.co.za/blog/?p=513</guid>
		<description><![CDATA[C# uses a lot of streams, or at least it seems so in some of the code examples I have looked at. Having said that I felt the need to read up on the Stream class. The stream class is an abstract class and an abstract acts as a type of base class from which [...]]]></description>
			<content:encoded><![CDATA[<p>C# uses a lot of streams, or at least it seems so in some of the code examples I have looked at.<br />
Having said that I felt the need to read up on the Stream class. The stream class is an abstract class and an abstract acts as a type of base class from which other classes can inherit. And just to recap, abstract methods do not have a method body and must be implemented by the class inheriting from it. A method marked as virtual can be overridden but it does not have to be implemented. Methods that are marked as abstract can be overriden.</p>
<p>So the stream class is an abstract base class for a set of other classes that do various operations such as reading content from files, reading files in a directory, etc. Each of the classes that inherit from the base stream class can be seen as a type of stream. Streams can be read from, written to and they support seeking.</p>
<p>The classes that inherit from the Stream class can be divided into three broad categories:</p>
<ul>
<li>Classes that are used for File I/O</li>
<li>Classes that are used for reading and writing to streams</li>
<li>Common IO stream classes</li>
</ul>
<p>The reason for taking a look at these classes is because I was looking at a PowerPoint presentation recently. More specifically I was looking at PowerPoint 2007 (pptx) and its underlying XML. If you save a Powerpoint 2007 as XML and you look at the source you will note that there are sections called pkg:binaryData. It got me wondering that maybe that same binary data can be processed in C# code. Take a look at the following sample C# code:</p>
<pre name="code" class="csharp">
            Image img = Image.FromFile(@"C:\Users\fredrike\Documents\LEGAL SUITE.bmp");
            byte[] myByte = File.ReadAllBytes(@"C:\Users\fredrike\Documents\LEGAL SUITE.bmp");
            Console.WriteLine(myByte.Length);
            MemoryStream stream = new MemoryStream(myByte);
            int lengthOfByte = Convert.ToInt32(stream.Length);
            byte[] mySecondByte = ReadFully(stream, lengthOfByte);
            Console.WriteLine(mySecondByte.Length);
            Image img2 = Image.FromStream(stream);
            img2.Save(@"C:\Users\fredrike\Documents\LEGAL SUITE3.bmp");
</pre>
<p>Notice a couple of things I have done in the code. I created a Image class instance and read it from an existing bitmap image. I then create a byte array and used the static ReadAllBytes method supplied by the File class. I then just simply wrote the length of the file to the screen because in the next couple of lines I create a MemoryStream instance using the byte array, after which I also get the length property of the MemoryStream instance because I use it in a function called ReadFully:</p>
<pre name="code" class="csharp">
        public static byte[] ReadFully(Stream stream, int initialLength)
        {
            // If we've been passed an unhelpful initial length, just
            // use 32K.
            if (initialLength < 1)
            {
                initialLength = 32768;
            }

            byte[] buffer = new byte[initialLength];
            int read = 0;

            int chunk;
            while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
            {
                read += chunk;

                // If we've reached the end of our buffer, check to see if there's
                // any more information
                if (read == buffer.Length)
                {
                    int nextByte = stream.ReadByte();

                    // End of stream? If so, we're done
                    if (nextByte == -1)
                    {
                        return buffer;
                    }

                    // Nope. Resize the buffer, put in the byte we've just
                    // read, and continue
                    byte[] newBuffer = new byte[buffer.Length * 2];
                    Array.Copy(buffer, newBuffer, buffer.Length);
                    newBuffer[read] = (byte)nextByte;
                    buffer = newBuffer;
                    read++;
                }
            }
            // Buffer is now too big. Shrink it.
            byte[] ret = new byte[read];
            Array.Copy(buffer, ret, read);
            return ret;
        }
</pre>
<p>I got this from<a href="http://www.yoda.arachsys.com/csharp/readbinary.html" onclick="urchinTracker('/outgoing/www.yoda.arachsys.com/csharp/readbinary.html?referer=');"> Jon Skeet&#8217;s website</a>. Be sure to check out his other C# articles. In my code I then created a second byte array which is generated by the ReadFully method. Once the bytes have finished reading I created a second Image instance, img2 and pass it the MemoryStream instance, and I then call img2&#8242;s Save method and create a second image based on the first one.</p>
<p>So my question is, can the binary data from Powerpoint presentations be used in the same way?</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save" onclick="urchinTracker('/outgoing/www.addtoany.com/share_save?referer=');"><img src="http://www.fremus.co.za/blog/wp-content/plugins/add-to-any/favicon.png" width="16" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.fremus.co.za/blog/2009/10/513/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

