为什么我的Inject方法不会将数据注入流?

时间:2013-08-23 09:46:53

标签: c# stream

我为C#Stream编写了一个包装类,因为我希望它能够将数据注入到流中,并在通过ReadAsync()读取数据时调用函数。然而,“注入”部分不起作用,我不知道为什么。

class ExtendedStream : Stream
{
    private readonly Stream _originalStream;
    private readonly Action<byte[]> _readCallback;

    private ManualResetEvent dataInjected = new ManualResetEvent(false);
    private List<byte> data = new List<byte>();
    private int pos = 0;

    public ExtendedStream(Stream originalStream, Action<byte[]> readCallback)
    {
        _originalStream = originalStream;
        _readCallback = readCallback;
    }

    public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken)
    {
        var read = await _originalStream.ReadAsync(buffer, offset, count, cancellationToken);

        _readCallback(buffer);

        return read;
    }

    public void Inject(string text)
    {
        data.AddRange(new UTF8Encoding(false).GetBytes(text));
        dataInjected.Set();
    }

    private IEnumerable<byte> GetBytes(int count)
    {
        int returned = 0;

        while (returned == 0)
        {
            if (pos < data.Count)
            {
                while (pos < data.Count && returned < count)
                {
                    yield return data[pos];

                    pos += 1; returned += 1;
                }
            }
            else
            {
                dataInjected.Reset();
                dataInjected.WaitOne();
            }
        }
    }       

    public override int Read(byte[] buffer, int offset, int count)
    {
        var bytes = GetBytes(count).ToArray();

        for (int i = 0; offset + i < buffer.Length && i < bytes.Length; i++)
        {
            buffer[offset + i] = bytes[i];
        }

        return bytes.Length;
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        _originalStream.Write(buffer, offset, count);
    }

    public override bool CanRead
    {
        get { return _originalStream.CanRead; }
    }

    public override bool CanSeek
    {
        get { return _originalStream.CanSeek; }
    }

    public override bool CanWrite
    {
        get { return _originalStream.CanWrite; }
    }

    public override void Flush() 
    {
        _originalStream.Flush();
    }

    public override long Length
    {
        get { return _originalStream.Length; }
    }

    public override long Position
    {
        get { return _originalStream.Position; }
        set { _originalStream.Position = value; }
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        return _originalStream.Seek(offset, origin);
    }

    public override void SetLength(long value)
    {
        _originalStream.SetLength(value);
    }        
}

然后我正在使用XmlReader读取流,如下所示。

using (XmlReader xmlReader = XmlReader.Create(_extendedStream, new XmlReaderSettings() { Async = true }))
{
                while (await xmlReader.ReadAsync())
                {                        
                    switch (xmlReader.NodeType)
                    {                            
                        case XmlNodeType.EndElement:
                            if (xmlReader.LocalName.Equals("test"))
                            {
                                _log.Debug("</test> injected!");                            
                            }
                            break;
                        default:                              
                            break;
                    }
}

如果我拨打_extendedStream.Inject("</test>"),则不会注入数据。有谁知道为什么?

1 个答案:

答案 0 :(得分:0)

您的ReadAsync方法委托给_originalStream.ReadAsync,因此我不认为您的被覆盖的Read方法(注入魔法发生的地方)会在您的示例代码中被调用。< / p>

您可以将原始流的读取移动到ReadAsync方法,而不是覆盖Read(可能会变得复杂),而ReadAsync方法将由基本// UNTESTED CODE, MAY CONTAIN OFF BY ONE ERRORS public override int Read(byte[] buffer, int offset, int count) { int totalBytesRead = 0; var bytes = GetBytes(count).ToArray(); // write injected bytes into buffer for (int i = 0; offset + i < buffer.Length && i < bytes.Length; i++) { buffer[offset + i] = bytes[i]; } if(bytes.Length < count) { // we reached the end of the custom bytes, so read the rest from the original stream count = count - bytes.Length; offset += bytes.Length; totalBytesRead = _originalStream.Read(buffer, offset, count); } totalBytesRead += bytes.Length; _readCallback(buffer); return totalBytesRead; } 方法异步调用

{{1}}

可能需要进行一些调整以防止注入的数据被反复注入,但至少注入逻辑将会运行。