Composable Streams


Composable Streams

Composable stream classes are streams that can perform I/O on top of another underlying stream. It s important to understand that the underlying stream can be a base stream or even another composable stream. As we mentioned earlier, composable streams have constructors that accept a stream as a parameter. This is a powerful idea because you can layer I/O techniques for reading data from and writing data to resources. For example, if you want to encrypt some data and save it to a file, you could simply open a base stream ” FileStream ” followed by a composable stream ” CryptoStream ” and then begin writing encrypted data to a file, as shown in Figure 2-4. In the .NET Framework version 1, only two composable streams are available: BufferedStream and CryptoStream .


Figure 2-4: Layering one stream on top of another stream
Note  

In the .NET Framework version 2, there are plans to include a new composable stream to handle Secure Sockets Layer (SSL) communications over a stream. This new stream potentially will allow you to use streams to communicate to an SSL server over a network by layering an SSL-composable stream over a network stream.

BufferedStream Class

BufferedStream is an interesting class that s designed to improve I/O performance when either reading or writing to another stream. This composable stream can be very useful, especially if your application performs I/O by reading or writing data in very small amounts. The class maintains an internal buffer to cache or collect the data, and at some point it will automatically flush data to the next stream or hold data until your application is ready to read from the stream.

By default, the BufferedStream class maintains a buffer of 4096 bytes. You can override the default by specifying any size buffer through the constructor, depending on your application s needs. The buffer can be used only for read or write operations, but not for both at the same time. If your application alternates between Read and Write calls continuously, the buffer will not be used very effectively. So, if you re developing an application that makes a series of either Read or Write calls using small amounts of data, the BufferedStream class can potentially improve I/O performance with operating system resources.

In the BufferedStreamSample directory of the companion material, we revised the C# version of our network stream Sender application to include a BufferedStream . The new buffered stream sample is named Sender. If you run the Sender application to communicate with the network stream Receiver sample application mentioned earlier in this chapter, you ll find that fewer I/O calls are made on the receiver compared to using the non-buffered network stream Sender application. Less calls are made because the buffered stream Sender application gathers the data and transmits the data in one big block over the network.

CryptoStream Class

The CryptoStream class is another composable stream that enables an application to encrypt and decrypt data to and from another stream. This class is located in the System.Security.Cryptography namespace. To use this class effectively, you need to understand cryptography, which is beyond the scope of this book. However, we ll take a moment here to explain how to encrypt data using a cryptographic algorithm. One book that might be helpful in fully understanding cryptography is Applied Cryptography: Protocols, Algorithms, and Source Code in C, Second Edition by Bruce Schneier (John Wiley & Sons, 1995).

In cryptography, two different techniques are used to encrypt and decrypt data: symmetric and asymmetric cryptography. Symmetric methods use a secret key to encode and decode data. Asymmetric methods use a public key to encode data and a private key to decode data. Symmetric cryptography is one of the oldest forms of cryptography, so for our sample code, we ll use the well-known symmetric algorithm Data Encryption Standard (DES). Many other algorithms are available in the .NET Framework to support both symmetric and asymmetric techniques. Table 2-3 describes the available cryptography algorithms.

Table 2-3: Available Cryptography Algorithms

Algorithm

Technique

Digital Signature Algorithm (DSA)

Asymmetric

RSA Security

Asymmetric

Data Encryption Standard (DES)

Symmetric

Rivest s Cipher (RC2)

Symmetric

Rijndael

Symmetric

Triple Data Encryption Standard (TripleDES)

Symmetric

The following code fragment shows how to set up a CryptoStream that can write encrypted data to a FileStream using the DES algorithm. For simplicity, we made up a secret key that s a combination of the variables DESKey and DESInitializationVector to show how stream encryption can be accomplished using the DES algorithm.

C#

 //MakeupasecretkeytobeusedbyDEStoencryptdata byte[]DESKey={200,5,78,232,9,6,0,4}; byte[]DESInitializationVector={9,9,9}; // //LetscreateaSymmetriccryptostreamusingthe //DESalgorithmtoencodeallthebyteswrittento //thefileJim. // CryptoStreamMyStreamEncrypter; try { DESDESAlgorithm=newDESCryptoServiceProvider(); MyStreamEncrypter=newCryptoStream(MyFileStream, DESAlgorithm.CreateEncryptor(DESKey, DESInitializationVector), CryptoStreamMode.Write); } catch(Exceptione) { Console.WriteLine("FailedtocreateDESCryptoStreamwitherror: " +e.Message); } finally { MyStreamEncypter.Close(); } 

Visual Basic .NET

 


Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net