11.07.2015 Views

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

Beginning C# 2008-from Novice-to-professional - A2Z Dotnet

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 10 ■ LEARNING ABOUT PERSISTENCE 281IBinary2TextProcessor would be responsible for reading the data <strong>from</strong> the binary stream andsaving data <strong>to</strong> the text stream.The text-<strong>to</strong>-binary interface, IText2BinaryProcessor, follows a similar interface design,except that the input and output stream types are reversed....using System.IO;namespace Text2Binary{public interface IText2BinaryProcessor {void Process(TextReader input, Stream output);}}The idea of both interface declarations is <strong>to</strong> focus on transferring data <strong>from</strong> one stream <strong>to</strong>another.The shell for both data stream interfaces is very similar <strong>to</strong> the shell of the text-processingstream, except that binary and text streams are involved. The TextProcessor shell was responsiblefor knowing if the data stream was <strong>from</strong> the console or <strong>from</strong> a file. When manipulatingbinary streams, the source still needs <strong>to</strong> be ascertained, but instead of creating text streams, binarystreams are created. If you understand the mechanics of using a text stream, you will understandthe mechanics of using a binary stream.The following is the complete implementation of the text-<strong>to</strong>-binary bootstrap class (youneed <strong>to</strong> add a reference <strong>to</strong> the Text2Binary project in the ReaderWriter project)....using System.IO;using Text2Binary;namespace ReaderWriter {public static class Text2BinaryBootstrap {public static void DisplayHelp() {Console.WriteLine("You need help? Right now?");}public static void Process(string[] args,IText2BinaryProcessor processor) {TextReader reader = null;Stream writer = null;if (args.Length == 0) {reader = Console.In;writer = Console.OpenStandardOutput();}else if (args.Length == 1) {if (args[0] == "-help") {DisplayHelp();return;

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

Saved successfully!

Ooh no, something went wrong!