2013-02-12 24 views
0

我想要實現委託給另一個IInputStream的IInputStream並返回給用戶,這樣的前處理讀取的數據:如何在IInputStream接口中包裝Windows.Storage.Streams.IInputStream?

using System; 

using Windows.Storage.Streams; 

using Org.BouncyCastle.Crypto; 
using Org.BouncyCastle.Crypto.Engines; 
using Org.BouncyCastle.Crypto.Parameters; 

namespace Core.Crypto { 
    public class RC4InputStream : IInputStream { 

     public RC4InputStream(IInputStream stream, byte[] readKey) { 
      _stream = stream; 

      _cipher = new RC4Engine(); 
      _cipher.Init(false, new KeyParameter(readKey)); 
     } 

     public Windows.Foundation.IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) 
     { 
      var op = _stream.ReadAsync(buffer, count, options); 
      // Somehow magically hook up something so that I can call _cipher.ProcessBytes(...) 
      return op; 
     } 

     private readonly IInputStream _stream; 
     private readonly IStreamCipher _cipher; 
    } 
} 

我有我一直沒能回答通過搜索兩個不同的問題在互聯網絡的浩瀚:什麼是連鎖的最佳途徑另一個操作運行下放後ReadAsync()(我可以用「等待」,也許

  • 創建一個新的IAsyncOperation使用AsyncInfo,但我不知道該怎麼連接進度記者等)
  • 如何訪問「IBuffer」背後的數據?

回答

1

您需要歸還您自己的IAsyncOperationWithProgress。您可以使用AsyncInfo.Run做到這一點:

public IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) 
{ 
    return AsyncInfo.Run<IBuffer, uint>(async (token, progress) => 
     { 
      progress.Report(0); 
      await _stream.ReadAsync(buffer, count, options); 
      progress.Report(50); 
      // call _cipher.ProcessBytes(...) 
      progress.Report(100); 
      return buffer; 
     }); 
} 

當然,你可以使自己的進度報告更爲細化取決於你在做什麼。

要訪問IBuffer中的數據,您可以使用ToArrayAsStream擴展方法。

相關問題