0

在我将文件流的引用从客户端传递给服务,并且服务开始将流下载给他之后,我如何从客户端确定到目前为止读取了多少字节(当我使用文件流对象时)?

我的目标是仅计算此文件的客户端上传速度,我能想到的唯一方法就是这样。

4

1 回答 1

4

扩展 FileStream 或为其创建包装器。覆盖读取方法并让计数器计算读取的字节数。

扩展(没有正确实施,但应该足以解释)

   public class CountingStream : System.IO.FileStream {

      // provide appropriate constructors

      // may want to override BeginRead too

      // not thread safe

      private long _Counter = 0;

      public override int ReadByte() {
         _Counter++;
         return base.ReadByte();            
      }

      public override int Read(byte[] array, int offset, int count) {
         // check if not going over the end of the stream
         _Counter += count;
         return base.Read(array, offset, count);             
      }

      public long BytesReadSoFar {
         get {
            return _Counter;
         }
      }
   }
于 2012-05-10T21:31:59.187 回答