Suppose I have a chain of streams, that does Compression -> Encryption -> File I/O.
In C#, using synchronous I/O, it would look something like this:
int n=0;
byte[] buffer= new byte[2048];
string inputFileName = "input.txt";
string outputFileName = inputFileName + ".compressed.encrypted";
using (FileStream inputFileStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read))
{
using (FileStream outputFileStream = File.Open(outputFileName, FileMode.Create, FileAccess.ReadWrite))
{
using (Stream encryptor = new EncryptingStream(fs))
{
using (Stream compressor = new CompressorStream(encryptor))
{
while ((n = inputFileStream.Read(buffer, 0, buffer.Length)) > 0)
{
compressor.Write(buffer, 0, n);
}
}
}
}
}
To take advantage of the async I/O offered by the FileStream, I suppose I cannot simply use the BeginWrite() method on the compressor stream.
In this example, in order to take advantage of async I/O on FileStream, I think that the EncryptingStream would need to implement Write by calling BeginWrite/EndWrite on the wrapped Stream. If the wrapped Stream is a FileStream, then I'd get the asynch I/O. Is that correct?