Changes between Version 2 and Version 3 of ICryptoolStreamUsage


Ignore:
Timestamp:
Jan 28, 2010, 12:25:08 PM (12 years ago)
Author:
Matthäus Wander
Comment:

finished usage

Legend:

Unmodified
Added
Removed
Modified
  • ICryptoolStreamUsage

    v2 v3  
    88 * Supports the .NET Dispose pattern, but does not require you to use it -- the garbage collector will clean up for you
    99 * Supports chunked streaming -- receivers can read and process data even if the sender has not finished writing
    10  * Supports POSIX-like Read() and offers also a more convenient ReadFully()
    1110
    1211== How to use CStreamWriter ==
     
    1413[[Image(source:/trunk/Documentation/Developer/CStream/CStreamWriter.png)]]
    1514
    16  * Create a CT2 output property with type CStream
    17  * In Execute() create a new CStreamWriter. Set the output property '''TODO: FINISH'''
     15 * Create an output property (e.g. called "OutputStream") with type CStream
     16 * In Execute() create a new CStreamWriter
     17 * Get the CStreamWriter.CStream property and set it as your OutputStream property
     18 * Announce the OutputStream update with OnPropertyChanged("OutputStream")
     19 * Pass data to the stream via CStreamWriter.Write()
     20 * Call CStreamWriter.Close() when finished
     21 * Best practice is to not care about disposal of a writer.
    1822
    19  * In Execute() either:
    20   # Create a CStreamWriter and pass subsequently one or more byte[] in Write() calls. Finish stream with Close().
    21   # Create a CStreamWriter, pass an existing byte[] in constructor and set autoClose to true.
    22  *
     23{{{
     24public class PluginA
     25{
     26        public CStream OutputStream
     27        {
     28                get;
     29                private set;
     30        }
     31       
     32        public void Execute()
     33        {
     34                CStreamWriter writer = new CStreamWriter();
     35                OutputStream = writer.CStream;
     36                OnPropertyChanged("OutputStream");
     37               
     38                while(condition)
     39                {
     40                        byte[] data = GenerateData();
     41                        writer.Write(data);
     42                }
     43               
     44                writer.Close();
     45        }
     46}
     47}}}
    2348
    2449== How to use CStreamReader ==
    2550
    2651[[Image(source:/trunk/Documentation/Developer/CStream/CStreamReader.png)]]
     52
     53 * Create an input property (e.g. called "InputStream") with type CStream
     54 * In Execute() call CreateReader() on that CStream to get a new CStreamReader
     55 * Retrieve data with CStreamReader.Read() ''in a loop''
     56 * End loop when read attempt returns 0 bytes
     57 * Best practice is to Dispose() the CStreamReader afterwards.
     58
     59{{{
     60public class PluginB
     61{
     62        public CStream InputStream
     63        {
     64                get;
     65                set;
     66        }
     67       
     68        public void Execute()
     69        {
     70                // The "using" keywords asserts that CStreamReader.Dispose() will be called
     71                using (CStreamReader reader = InputStream.CreateReader())
     72                {
     73                        int bytesRead;
     74                        byte[] buffer = new byte[ANY_SIZE];
     75                       
     76                        while ((bytesRead = reader.Read(buffer)) > 0)
     77                        {
     78                                // Note: buffer can contain less data than buffer.Length, therefore consider bytesRead
     79                                processData(buffer, bytesRead);
     80                        }
     81                }
     82        }
     83}
     84}}}