15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

788 ❘ ChaPTer 29 mAnipulAtinG files And the reGistry<br />

System.Object<br />

System.MarshalByRefObject BinaryReader BinaryWriter<br />

Stream TextReader TextWriter<br />

BufferedStream StringReader StringWriter<br />

MemoryStream StreamReader StreamWriter<br />

figure 29-8<br />

FileStream<br />

You might also find the BinaryReader <strong>and</strong> BinaryWriter classes useful, although they are not used in the<br />

examples here. These classes do not actually implement streams themselves, but they are able to provide<br />

wrappers around other stream objects. BinaryReader <strong>and</strong> BinaryWriter provide extra formatting of<br />

binary data, which allows you to directly read or write the contents of <strong>C#</strong> variables to or from the relevant<br />

stream. Think of the BinaryReader <strong>and</strong> BinaryWriter as sitting between the stream <strong>and</strong> your code,<br />

providing extra formatting (see Figure 29-9).<br />

BinaryReader<br />

Your code<br />

figure 29-9<br />

BinaryWriter<br />

underlying<br />

Stream object<br />

Data source<br />

(file, network etc.)<br />

The difference between using these classes <strong>and</strong> directly using the underlying stream objects is that a basic<br />

stream works in bytes. For example, suppose that as part of the process of saving some document you want<br />

to write the contents of a variable of type long to a binary file. Each long occupies 8 bytes, <strong>and</strong> if you used<br />

an ordinary binary stream, you would have to explicitly write each of those 8 bytes of memory.<br />

In <strong>C#</strong> code, you would have to perform some bitwise operations to extract each of those 8 bytes from the<br />

long value. Using a BinaryWriter instance, you can encapsulate the entire operation in an overload of the<br />

BinaryWriter.Write() method, which takes a long as a parameter, <strong>and</strong> which will place those 8 bytes<br />

into the stream (<strong>and</strong> if the stream is directed to a file, into the file). A corresponding BinaryReader.Read()<br />

method will extract 8 bytes from the stream <strong>and</strong> recover the value of the long. For more information on the<br />

BinaryReader <strong>and</strong> BinaryWriter classes, refer to the SDK documentation.<br />

buffered streams<br />

For performance reasons, when you read or write to or from a file, the output is buffered. This means that<br />

if your program asks for the next 2 bytes of a file stream, <strong>and</strong> the stream passes the request on to Windows,<br />

then Windows will not connect to the file system <strong>and</strong> then locate <strong>and</strong> read the file off the disk, just to get<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!