Introduction
Sometimes during programming, there are some methods that take a stream and then perform some function on it. Usually, if the stream is fairly large, then the method may not return for an extreme amount of time (e.g. ~20 minutes for SHA1 calculation on 2 GB file across a network). Some applications would simply like to get notified that there was some activity and hopefully the amount of activity. Being notified that a block of bytes is about to, or has been read would solve this problem. Thus, the EventStream
reader attempts to solve this problem. See the example for more information.
Using the code
Below is an example usage of this class, to be notified during a potentially long SHA1 calculation.
class ExampleNotifyClass
{
public int timesNotified = 0 ;
public void SectionWasRead( byte[] buff, int index, int count )
{
Console.WriteLine("Section read: {0}", count ) ;
timesNotified++ ;
}
public static void Main() {
MemoryStream mem = new MemoryStream() ;
StreamWriter writer = new StreamWriter(mem, Encoding.UTF8) ;
for( int i = 0 ; i < 16 * 777 ; i++ )
{
writer.WriteLine( "{0}", i % 10 ) ;
}
mem.Seek( 0, SeekOrigin.Begin ) ;
ExampleNotifyClass toNotify = new ExampleNotifyClass();
EventStream toRead = new EventStream( mem ) ;
toRead.AfterBlockRead += new
EventStream.AfterBlockHandler(toNotify.SectionWasRead ) ;
toRead.BlockEventSize = 8192 ;
SHA1 sha = new SHA1Managed() ;
byte[] hash = sha.ComputeHash( toRead ) ;
Console.WriteLine( "Our class was notified {0} times.",
toNotify.timesNotified ) ;
}
}