What are the implications for Async I / O (BeginWrite / BeginRead) with a thread chain?

Suppose I have a thread chain, which does Compression β†’ Encryption β†’ File I / O.

In C #, using synchronous I / O, it will 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 asynchronous I / O offered by FileStream, I suppose I can't just use the BeginWrite () method for the compressor stream.

In this example, in order to use the asynchronous I / O in FileStream, I think EncryptingStream will need to implement Write by calling BeginWrite / EndWrite on the wrapped stream. If the wrapped stream is a FileStream, then I get asynchronous I / O. It is right?

+3
1

BeginWrite ( compressor) , . , , , . , BeginWrite FileStream, -. BeginWrite, FileStream BeginWrite API- Windows, .

, -. , . - , BeginWrite , API- .

, , . , Stream, , BeginWrite, .

+2

Source: https://habr.com/ru/post/1721852/


All Articles