简体   繁体   中英

c# inheriting from Stream, Binary Writer Works, Stream Writer does not

I created this object in order to port an API using TcpClient from .net to silverlight. It was written using a socket and a reader/writer on the stream. As it happens the API was using a binary reader/writer, so everything works fine. When testing though, it appears that a StreamWriter just doesn't call the OnWrite delegate when writing. I was curious if anyone could shed some light onto this.

Really the only thing going on with this class is the OnRead & OnWrite which are called when a read or write is made to the stream. In my case, they pass the work off to a 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();
        }
    }
}

StreamWriter has an internal buffer which it fills before writing to the stream.

Call Flush() on the writer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM