c#继承自Stream,Binarywriter Works,Streamwriter没有

时间:2014-03-12 13:53:31

标签: c# silverlight sockets

我创建了这个对象,以便使用TcpClient从.net到Silverlight移植API。它是使用流上的套接字和读写器编写的。碰巧API正在使用二进制读取器/写入器,所以一切正常。但是在测试时,看起来StreamWriter在写入时不会调用OnWrite委托。我很好奇是否有人可以对此有所了解。

这个课程唯一发生的事情是OnRead& amp;在对流进行读取或写入时调用的OnWrite。在我的情况下,他们将工作传递给Socket。

public class NotifyStream:Stream
{
    public delegate int ReadDelegate (byte[] buffer, int offset, int count);
    public delegate void WriteDelegate(byte[] buffer, int offset, int count);

    public ReadDelegate OnRead;
    public WriteDelegate OnWrite;

    public override void Flush()
    {
        // TODO: Implement this method
        //throw new NotImplementedException();
    }

    public override long Seek(long offset, SeekOrigin origin)
    {
        // TODO: Implement this method
        return 0;
    }

    public override void SetLength(long value)
    {
        // TODO: Implement this method
        // throw new NotImplementedException();
    }

    public override int Read(byte[] buffer, int offset, int count)
    {
        // TODO: Implement this method
        if (OnRead != null)
        {
            return OnRead(buffer, offset, count);
        }
        else
        {
            return 0;
        }
    }

    public override void Write(byte[] buffer, int offset, int count)
    {
        // TODO: Implement this method
        if (OnWrite != null)
        {
            OnWrite(buffer, offset, count);
        }
    }

    public override bool CanRead
    {
        get
        {
            // TODO: Implement this property getter
            return true;
        }
    }

    public override bool CanSeek
    {
        get
        {
            // TODO: Implement this property getter
            return false;
        }
    }

    public override bool CanWrite
    {
        get
        {
            // TODO: Implement this property getter
            return true;
        }
    }

    public override long Length
    {
        get
        {
            // TODO: Implement this property getter
            return 1024;
        }
    }

    public override long Position
    {
        get
        {
            // TODO: Implement this property getter
            return 0;
        }
        set
        {
            // TODO: Implement this property setter
            //throw new NotImplementedException();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

StreamWriter有一个内部缓冲区,在写入流之前它会填充。

在作家上调用Flush()