按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
          process lotto。txt and get a correctly formatted data stream。 
          Piping Binary Data 
          When working with the console; for the most part; you will be transferring text data from one  
          process to another or from one file to another file。 However; when developing in ; working  
…………………………………………………………Page 291……………………………………………………………
                                                            CH A PT E R   1 0   ■    L E A R N I N G   A B O U T  P E R S IS T E N CE 269 
with text is not always the best choice。 A more efficient approach might be to store the data in  
a binary format。  
      The easiest way to understand the difference between text data and binary data is to open  
a binary file using a text editor; as shown in Figure 10…8。 
Figure 10…8。 A binary file in a text editor 
      In Figure 10…8; you see just a bunch of funny characters with some text scattered throughout。  
A binary file is different from a text file in that the format of a binary file is whatever the program  
that reads and writes the binary data decides。 The advantage of using a binary file is that you  
can create plex structures。 The disadvantage is that only the developer of the reader/writer  
program knows what the file contains。 
      Binary files are not always smaller; but they are more efficient because you do not need to  
parse and process the data。 For example; in the text…based lottery data stream reader; we needed  
to parse every single line and then split the line of text into individual fields; which were then  
parsed as integers and assigned to a variable。 Using a binary file; you only need to create a  
binary type; and read or write that type。 
      In this section; we will continue with the example of the lottery…prediction application; but  
this time; create it as a console application that converts a text data stream into a binary data  
stream and then back to a text stream。 You’ll see how you can pipe the data from one stream to  
another by sending it from one application to another application。 When we are finished  
building this application; the following mand line will be valid。 
type lotto。txt | TextProcessor。exe | Text2Binary。exe | Binary2Text。exe 
      The mand line starts with generating a text data stream using the type mand。  
The mand TextProcessor。exe generates a clean data stream that is then piped into  
…………………………………………………………Page 292……………………………………………………………
270       CH AP T E R   1 0   ■    L E A R N I N G   A B OU T   P E R S IS TE N CE 
           Text2Binary。exe; which generates a binary data stream。 Finally; the binary data stream is  
           converted back into a text stream using Binary2Text。exe; which displays the data on the console。  
                The architecture of TextProcessor with its implementation of the  IProcessor interface  
           and a general architecture of grabbing a file or console input/output stream worked。 However;  
           IProcessor is not usable for binary streams; so we need to implement a new interface and  
           architecture。 
           ■Note  The examples that follow illustrate a very mon situation where an architecture worked and the  
           ideas of the architecture could be applied in the new context。 What does not work is the actual implementa
           tion of the original architecture。 You may be tempted to modify the working architecture so that it will work  
           with the new context。 As much as you would like to do that; don’t。 Often; the abstractions you create will  
           plicate the architecture and make the resulting code more plex。 Your challenge is to know when to  
           generalize an architecture and when to keep the ideas of an architecture but create a new implementation;  
           and you’ll learn this with experience。 
           Defining the Interfaces and Implementing the Shell 
           In ; the data streams are split into two types of streams: binary and text。 The TextWriter  
           and TextReader types are used to read text…based data streams。 As was demonstrated with the  
           StringReader type; when dealing with text…based streams; certain assumptions can be made;  
           such as being able to find new lines in the stream。 With binary data streams; no such assump
           tions can be made。 Binary streams have their own format; which is known by only the program  
           doing the reading or writing。 
                The binary stream…based types can be used to process text data streams; but doing so would  
           require knowing the details of the data stream。 Remember that  gives you a text…handling  
           system that understands the different Unicode code pages。 A Unicode code page is a specific  
           translation map。 If you decide to manipulate the text streams using binary stream types; you  
           are telling  that you will manage the details of the Unicode code pages。 Of course; you  
           don’t want to do that; and thus should never mix data streams。 So; for our sample application;  
           we need to design two different interfaces: one to stream from text to binary and one to stream  
           from binary to text。 
           ■Note  For more information about Unicode and other text…related issues; see the MSDN “International Text  
           Display” section (http://msdn2。microsoft。/en…us/library/ms776131。aspx)。 
                The following is the binary…to…text data stream interface; Binary2Text。IBinary2TextProcessor。 
           Imports System。IO 
           Public Interface IBinary2TextProcessor 
               Sub Process(ByVal input As Stream; ByVal output As TextWriter) 
           End Interface 
…………………………………………………………Page 293……………………………………………………………
                                                      CH A PT E R   1 0   ■    L E A R N I N G   A B O U T  P E R S IS T E N CE 271 
     The IBinary2TextPr