0

次のように、別のIInputStreamに委任し、読み取ったデータを処理してからユーザーに返す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;
    }
}

私はインターネットの広大さを検索することによって答えることができなかった2つの異なる問題を抱えています:

  • 委任されたReadAsync()の後に実行する別の操作をチェーンする最良の方法は何ですか(「await」を使用し、AsyncInfoを使用して新しいIAsyncOperationを作成することもできますが、progress reporterなどを接続する方法がわかりません)
  • 「IBuffer」の背後にあるデータにアクセスするにはどうすればよいですか?
4

1 に答える 1

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

于 2013-02-14T06:01:29.673 に答える